Initial copy of RAPI filebase to the trunk
[ganeti-local] / lib / logger.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Logging for Ganeti
23
24 This module abstracts the logging handling away from the rest of the
25 Ganeti code. It offers some utility functions for easy logging.
26
27 """
28
29 # pylint: disable-msg=W0603,C0103
30
31 import sys
32 import logging
33 import os, os.path
34
35 from ganeti import constants
36
37
38 def _CreateFileHandler(name):
39   return logging.FileHandler(os.path.join(constants.LOG_DIR, name))
40
41
42 def SetupLogging(program='ganeti', debug=False):
43   """Setup logging for ganeti
44
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
47   exits.
48
49   Args:
50     debug: Whether to enable verbose logging
51     program: Program name
52
53   """
54   fmt = "%(asctime)s " + program + ": %(message)s"
55   formatter = logging.Formatter(fmt)
56
57   stderr_fmt = "%(asctime)s: %(message)s"
58   stderr_formatter = logging.Formatter(stderr_fmt)
59
60   info_file = _CreateFileHandler("info")
61   info_file.setLevel(logging.INFO)
62   info_file.setFormatter(formatter)
63
64   errors_file = _CreateFileHandler("errors")
65   errors_file.setLevel(logging.ERROR)
66   errors_file.setFormatter(formatter)
67
68   debug_file = _CreateFileHandler("debug")
69   debug_file.setLevel(logging.DEBUG)
70   debug_file.setFormatter(formatter)
71
72   stderr_file = logging.StreamHandler()
73   stderr_file.setFormatter(stderr_formatter)
74   if debug:
75     stderr_file.setLevel(logging.NOTSET)
76   else:
77     stderr_file.setLevel(logging.ERROR)
78
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)
85
86
87 def SetupDaemon(logfile, debug=False, stderr_logging=False):
88   """Configures the logging module for daemons
89
90   """
91   if debug:
92     fmt = "%(asctime)s: %(levelname)s %(module)s:%(lineno)s %(message)s"
93   else:
94     fmt = "%(asctime)s: %(levelname)s %(message)s"
95   formatter = logging.Formatter(fmt)
96
97   logfile_handler = logging.FileHandler(logfile)
98   logfile_handler.setFormatter(formatter)
99
100   stderr_handler = logging.StreamHandler()
101   stderr_handler.setFormatter(formatter)
102   if debug:
103     logfile_handler.setLevel(logging.DEBUG)
104     stderr_handler.setLevel(logging.NOTSET)
105   else:
106     logfile_handler.setLevel(logging.INFO)
107     stderr_handler.setLevel(logging.CRITICAL)
108
109   root_logger = logging.getLogger("")
110   root_logger.setLevel(logging.NOTSET)
111   root_logger.addHandler(logfile_handler)
112   if stderr_logging:
113     root_logger.addHandler(stderr_handler)
114
115
116 # Backwards compatibility
117 Error = logging.error
118 Info = logging.info
119 Debug = logging.debug
120
121
122 def ToStdout(txt):
123   """Write a message to stdout only, bypassing the logging system
124
125   Parameters:
126     - txt: the message
127
128   """
129   sys.stdout.write(txt + '\n')
130   sys.stdout.flush()
131
132
133 def ToStderr(txt):
134   """Write a message to stderr only, bypassing the logging system
135
136   Parameters:
137     - txt: the message
138
139   """
140   sys.stderr.write(txt + '\n')
141   sys.stderr.flush()