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.
29 # pylint: disable-msg=W0603,C0103
35 from ganeti import constants
38 def _CreateFileHandler(name):
39 return logging.FileHandler(os.path.join(constants.LOG_DIR, name))
42 def SetupLogging(program='ganeti', debug=False):
43 """Setup logging for ganeti
45 On failure, a check is made whether process is run by root or not,
46 and an appropriate error message is printed on stderr, then process
50 debug: Whether to enable verbose logging
54 fmt = "%(asctime)s " + program + ": %(message)s"
55 formatter = logging.Formatter(fmt)
57 stderr_fmt = "%(asctime)s: %(message)s"
58 stderr_formatter = logging.Formatter(stderr_fmt)
60 info_file = _CreateFileHandler("info")
61 info_file.setLevel(logging.INFO)
62 info_file.setFormatter(formatter)
64 errors_file = _CreateFileHandler("errors")
65 errors_file.setLevel(logging.ERROR)
66 errors_file.setFormatter(formatter)
68 debug_file = _CreateFileHandler("debug")
69 debug_file.setLevel(logging.DEBUG)
70 debug_file.setFormatter(formatter)
72 stderr_file = logging.StreamHandler()
73 stderr_file.setFormatter(stderr_formatter)
75 stderr_file.setLevel(logging.NOTSET)
77 stderr_file.setLevel(logging.ERROR)
79 root_logger = logging.getLogger("")
80 root_logger.setLevel(logging.NOTSET)
81 root_logger.addHandler(info_file)
82 root_logger.addHandler(errors_file)
83 root_logger.addHandler(debug_file)
84 root_logger.addHandler(stderr_file)
87 def SetupDaemon(logfile, debug=False, stderr_logging=False):
88 """Configures the logging module for daemons
92 fmt = ("%(asctime)s: pid=%(process)d %(levelname)s"
93 " %(module)s:%(lineno)s %(message)s")
95 fmt = "%(asctime)s: pid=%(process)d %(levelname)s %(message)s"
96 formatter = logging.Formatter(fmt)
98 logfile_handler = logging.FileHandler(logfile)
99 logfile_handler.setFormatter(formatter)
101 stderr_handler = logging.StreamHandler()
102 stderr_handler.setFormatter(formatter)
104 logfile_handler.setLevel(logging.DEBUG)
105 stderr_handler.setLevel(logging.NOTSET)
107 logfile_handler.setLevel(logging.INFO)
108 stderr_handler.setLevel(logging.CRITICAL)
110 root_logger = logging.getLogger("")
111 root_logger.setLevel(logging.NOTSET)
112 root_logger.addHandler(logfile_handler)
114 root_logger.addHandler(stderr_handler)
117 # Backwards compatibility
118 Error = logging.error
120 Debug = logging.debug
124 """Write a message to stdout only, bypassing the logging system
130 sys.stdout.write(txt + '\n')
135 """Write a message to stderr only, bypassing the logging system
141 sys.stderr.write(txt + '\n')