4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 This module abstracts the logging handling away from the rest of the
25 Ganeti code. It offers some utility functions for easy logging.
28 # pylint: disable-msg=W0603,C0103
34 from ganeti import constants
36 _program = '(unknown)'
45 def _SetDestination(name, filename, stream=None):
46 """Configure the destination for a given logger
48 This function configures the logging destination for a given loger.
50 - name: the logger name
51 - filename: if not empty, log messages will be written (also) to this file
52 - stream: if not none, log messages will be output (also) to this stream
55 - the logger identified by the `name` argument
57 ret = logging.getLogger(name)
60 fmtr = logging.Formatter('%(asctime)s %(message)s')
62 hdlr = logging.FileHandler(filename)
63 hdlr.setFormatter(fmtr)
67 if name in ('error', 'info', 'debug'):
68 fmtr = logging.Formatter('%(asctime)s %(message)s')
70 fmtr = logging.Formatter('%(message)s')
71 hdlr = logging.StreamHandler(stream)
72 hdlr.setFormatter(fmtr)
75 ret.setLevel(logging.INFO)
80 def _GenericSetup(program, errfile, inffile, dbgfile,
81 twisted_workaround=False):
82 """Configure logging based on arguments
89 - twisted_workaround: if true, emit all messages to stderr
99 if twisted_workaround:
100 _errlog = _SetDestination('error', None, sys.stderr)
101 _inflog = _SetDestination('info', None, sys.stderr)
102 _dbglog = _SetDestination('debug', None, sys.stderr)
104 _errlog = _SetDestination('error', errfile)
105 _inflog = _SetDestination('info', inffile)
106 _dbglog = _SetDestination('debug', dbgfile)
108 _stdout = _SetDestination('user', None, sys.stdout)
109 _stderr = _SetDestination('stderr', None, sys.stderr)
112 def SetupLogging(twisted_workaround=False, debug=False, program='ganeti'):
113 """Setup logging for ganeti
115 On failure, a check is made whether process is run by root or not,
116 and an appropriate error message is printed on stderr, then process
119 This function is just a wraper over `_GenericSetup()` using specific
123 twisted_workaround: passed to `_GenericSetup()`
127 _GenericSetup(program,
128 os.path.join(constants.LOG_DIR, "errors"),
129 os.path.join(constants.LOG_DIR, "info"),
130 os.path.join(constants.LOG_DIR, "debug"),
133 # The major reason to end up here is that we're being run as a
134 # non-root user. We might also get here if xen has not been
135 # installed properly. This is not the correct place to enforce
136 # being run by root; nevertheless, here makes sense because here
137 # is where we first notice it.
139 sys.stderr.write('This program must be run by the superuser.\n')
141 sys.stderr.write('Unable to open log files. Incomplete system?\n')
149 def _WriteEntry(log, txt):
151 Write a message to a given log.
152 Splits multi-line messages up into a series of log writes, to
153 keep consistent format on lines in file.
156 - log: the destination log
161 sys.stderr.write("Logging system not initialized while processing"
163 sys.stderr.write("%s\n" % txt)
166 lines = txt.split('\n')
168 spaces = ' ' * len(_program) + '| '
170 lines = ([ _program + ': ' + lines[0] ] +
171 map(lambda a: spaces + a, lines[1:]))
174 log.log(logging.INFO, line)
178 """Write a message to stdout only, bypassing the logging system
184 sys.stdout.write(txt + '\n')
189 """Write a message to stderr only, bypassing the logging system
195 sys.stderr.write(txt + '\n')
200 """Write a message to our error log
203 - dbg: if true, the message will also be output to stderr
204 - txt: the log message
207 _WriteEntry(_errlog, txt)
208 sys.stderr.write(txt + '\n')
212 """Write a message to our general messages log
214 If the global debug flag is true, the log message will also be
218 - txt: the log message
221 _WriteEntry(_inflog, txt)
223 _WriteEntry(_stderr, txt)
227 """Write a message to the debug log
229 If the global debug flag is true, the log message will also be
233 - txt: the log message
236 _WriteEntry(_dbglog, txt)
238 _WriteEntry(_stderr, txt)