Change JobStorage to work with ids not filenames
[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: pid=%(process)d %(levelname)s"
93            " %(module)s:%(lineno)s %(message)s")
94   else:
95     fmt = "%(asctime)s: pid=%(process)d %(levelname)s %(message)s"
96   formatter = logging.Formatter(fmt)
97
98   logfile_handler = logging.FileHandler(logfile)
99   logfile_handler.setFormatter(formatter)
100
101   stderr_handler = logging.StreamHandler()
102   stderr_handler.setFormatter(formatter)
103   if debug:
104     logfile_handler.setLevel(logging.DEBUG)
105     stderr_handler.setLevel(logging.NOTSET)
106   else:
107     logfile_handler.setLevel(logging.INFO)
108     stderr_handler.setLevel(logging.CRITICAL)
109
110   root_logger = logging.getLogger("")
111   root_logger.setLevel(logging.NOTSET)
112   root_logger.addHandler(logfile_handler)
113   if stderr_logging:
114     root_logger.addHandler(stderr_handler)
115
116
117 # Backwards compatibility
118 Error = logging.error
119 Info = logging.info
120 Debug = logging.debug
121
122
123 def ToStdout(txt):
124   """Write a message to stdout only, bypassing the logging system
125
126   Parameters:
127     - txt: the message
128
129   """
130   sys.stdout.write(txt + '\n')
131   sys.stdout.flush()
132
133
134 def ToStderr(txt):
135   """Write a message to stderr only, bypassing the logging system
136
137   Parameters:
138     - txt: the message
139
140   """
141   sys.stderr.write(txt + '\n')
142   sys.stderr.flush()