1 {-| Implementation of the Ganeti logging functionality.
3 This currently lacks the following (FIXME):
6 - handling of the three-state syslog yes/no/only
9 Note that this requires the hslogger library version 1.1 and above.
15 Copyright (C) 2011 Google Inc.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
46 import System.Log.Logger
47 import System.Log.Handler.Simple
48 import System.Log.Handler (setFormatter)
49 import System.Log.Formatter
52 import qualified Ganeti.Constants as C
54 -- | Builds the log formatter.
55 logFormatter :: String -- ^ Program
56 -> Bool -- ^ Multithreaded
59 logFormatter prog mt syslog =
60 let parts = [ if syslog
62 else "$time: " ++ prog ++ " pid=$pid"
63 , if mt then if syslog then " ($tid)" else "/$tid"
67 in simpleLogFormatter $ concat parts
69 -- | Sets up the logging configuration.
70 setupLogging :: String -- ^ Log file
71 -> String -- ^ Program name
72 -> Bool -- ^ Debug level
73 -> Bool -- ^ Log to stderr
74 -> Bool -- ^ Log to console
76 setupLogging logf program debug stderr_logging console = do
77 let level = if debug then DEBUG else INFO
78 destf = if console then C.devConsole else logf
79 fmt = logFormatter program False False
81 updateGlobalLogger rootLoggerName (setLevel level)
83 stderr_handlers <- if stderr_logging
85 stderr_handler <- streamHandler stderr level
86 return [setFormatter stderr_handler fmt]
88 file_handler <- fileHandler destf level
89 let handlers = setFormatter file_handler fmt:stderr_handlers
90 updateGlobalLogger rootLoggerName $ setHandlers handlers
92 -- * Logging function aliases
94 -- | Log at debug level.
95 logDebug :: String -> IO ()
96 logDebug = debugM rootLoggerName
98 -- | Log at info level.
99 logInfo :: String -> IO ()
100 logInfo = infoM rootLoggerName
102 -- | Log at notice level.
103 logNotice :: String -> IO ()
104 logNotice = noticeM rootLoggerName
106 -- | Log at warning level.
107 logWarning :: String -> IO ()
108 logWarning = warningM rootLoggerName
110 -- | Log at error level.
111 logError :: String -> IO ()
112 logError = errorM rootLoggerName
114 -- | Log at critical level.
115 logCritical :: String -> IO ()
116 logCritical = criticalM rootLoggerName
118 -- | Log at alert level.
119 logAlert :: String -> IO ()
120 logAlert = alertM rootLoggerName
122 -- | Log at emergency level.
123 logEmergency :: String -> IO ()
124 logEmergency = emergencyM rootLoggerName