module Ganeti.Logging
( setupLogging
, logDebug
, logInfo
, logNotice
, logWarning
, logError
, logCritical
, logAlert
, logEmergency
, SyslogUsage(..)
, syslogUsageToRaw
, syslogUsageFromRaw
) where
import Control.Monad (when)
import System.Log.Logger
import System.Log.Handler.Simple
import System.Log.Handler.Syslog
import System.Log.Handler (setFormatter, LogHandler)
import System.Log.Formatter
import System.IO
import Ganeti.THH
import qualified Ganeti.Constants as C
$(declareSADT "SyslogUsage"
[ ("SyslogNo", 'C.syslogNo)
, ("SyslogYes", 'C.syslogYes)
, ("SyslogOnly", 'C.syslogOnly)
])
logFormatter :: String
-> Bool
-> Bool
-> LogFormatter a
logFormatter prog mt syslog =
let parts = [ if syslog
then "[$pid]:"
else "$time: " ++ prog ++ " pid=$pid"
, if mt then if syslog then " ($tid)" else "/$tid"
else ""
, " $prio $msg"
]
in tfLogFormatter "%F %X,%q %Z" $ concat parts
openFormattedHandler :: (LogHandler a) => Bool
-> LogFormatter a -> IO a -> IO [a]
openFormattedHandler False _ _ = return []
openFormattedHandler True fmt opener = do
handler <- opener
return [setFormatter handler fmt]
setupLogging :: Maybe String
-> String
-> Bool
-> Bool
-> Bool
-> SyslogUsage
-> IO ()
setupLogging logf program debug stderr_logging console syslog = do
let level = if debug then DEBUG else INFO
destf = if console then Just C.devConsole else logf
fmt = logFormatter program False False
file_logging = syslog /= SyslogOnly
updateGlobalLogger rootLoggerName (setLevel level)
stderr_handlers <- openFormattedHandler stderr_logging fmt $
streamHandler stderr level
file_handlers <- case destf of
Nothing -> return []
Just path -> openFormattedHandler file_logging fmt $
fileHandler path level
let handlers = file_handlers ++ stderr_handlers
updateGlobalLogger rootLoggerName $ setHandlers handlers
when (syslog /= SyslogNo) $ do
syslog_handler <- openlog program [PID] DAEMON INFO
updateGlobalLogger rootLoggerName $ addHandler syslog_handler
logDebug :: String -> IO ()
logDebug = debugM rootLoggerName
logInfo :: String -> IO ()
logInfo = infoM rootLoggerName
logNotice :: String -> IO ()
logNotice = noticeM rootLoggerName
logWarning :: String -> IO ()
logWarning = warningM rootLoggerName
logError :: String -> IO ()
logError = errorM rootLoggerName
logCritical :: String -> IO ()
logCritical = criticalM rootLoggerName
logAlert :: String -> IO ()
logAlert = alertM rootLoggerName
logEmergency :: String -> IO ()
logEmergency = emergencyM rootLoggerName