9 import Control.Monad ( when )
10 import System.Environment ( getProgName )
11 import System.Log.Formatter ( simpleLogFormatter )
12 import System.Log.Handler ( setFormatter )
13 import System.Log.Handler.Simple ( GenericHandler, fileHandler )
14 import System.Log.Handler.Syslog (
17 import System.Log.Logger (
30 -- | Log a message at the DEBUG level.
31 log_debug :: String -> IO ()
32 log_debug = debugM rootLoggerName
34 -- | Log a message at the ERROR level.
35 log_error :: String -> IO ()
36 log_error = errorM rootLoggerName
38 -- | Log a message at the INFO level.
39 log_info :: String -> IO ()
40 log_info = infoM rootLoggerName
42 -- | Log a message at the WARNING level.
43 log_warning :: String -> IO ()
44 log_warning = warningM rootLoggerName
47 -- | Set up the logging. All logs are handled by the global "root"
48 -- logger provided by HSLogger. We remove all of its handlers so
49 -- that it does nothing; then we conditionally add back two handlers
50 -- -- one for syslog, and one for a normal file -- dependent upon
51 -- the 'syslog' and 'log_file' configuration items.
53 -- Why don't we take a Configuration as an argument? Because it
54 -- would create circular imports!
55 init_logging :: Maybe FilePath -> Priority -> Bool -> IO ()
56 init_logging log_file log_level syslog = do
57 -- First set the global log level and clear the default handler.
58 let no_handlers = [] :: [GenericHandler a]
59 updateGlobalLogger rootLoggerName (setLevel log_level .
60 setHandlers no_handlers)
64 let sl_level = if log_level < min_level then min_level else log_level
66 -- The syslog handle gets its own level which will cowardly refuse
67 -- to log all debug info (i.e. the entire feed) to syslog.
68 sl_handler' <- openlog rootLoggerName [] USER sl_level
70 -- Syslog should output the date by itself.
71 program_name <- getProgName
72 let sl_formatter = simpleLogFormatter $
73 program_name ++ "[$pid] $prio: $msg"
74 let sl_handler = setFormatter sl_handler' sl_formatter
76 updateGlobalLogger rootLoggerName (addHandler sl_handler)
81 lf_handler' <- fileHandler lf log_level
82 let lf_formatter = simpleLogFormatter "$time: htsn[$pid] $prio: $msg"
83 let lf_handler = setFormatter lf_handler' lf_formatter
84 updateGlobalLogger rootLoggerName (addHandler lf_handler)