root / lib / logger.py @ f6bd6e98
History | View | Annotate | Download (2.8 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | a8083063 | Iustin Pop | # Copyright (C) 2006, 2007 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | """Logging for Ganeti
|
23 | a8083063 | Iustin Pop |
|
24 | a8083063 | Iustin Pop | This module abstracts the logging handling away from the rest of the
|
25 | a8083063 | Iustin Pop | Ganeti code. It offers some utility functions for easy logging.
|
26 | 2a2060ff | Michael Hanselmann |
|
27 | a8083063 | Iustin Pop | """
|
28 | a8083063 | Iustin Pop | |
29 | a8083063 | Iustin Pop | # pylint: disable-msg=W0603,C0103
|
30 | a8083063 | Iustin Pop | |
31 | a8083063 | Iustin Pop | import sys |
32 | a8083063 | Iustin Pop | import logging |
33 | a8083063 | Iustin Pop | |
34 | a8083063 | Iustin Pop | |
35 | 59f187eb | Iustin Pop | def SetupLogging(logfile, debug=False, stderr_logging=False, program=""): |
36 | 59f187eb | Iustin Pop | """Configures the logging module.
|
37 | a8083063 | Iustin Pop |
|
38 | a8083063 | Iustin Pop | """
|
39 | 59f187eb | Iustin Pop | fmt = "%(asctime)s: " + program + " " |
40 | 2a2060ff | Michael Hanselmann | if debug:
|
41 | 59f187eb | Iustin Pop | fmt += ("pid=%(process)d/%(threadName)s %(levelname)s"
|
42 | a237d0a8 | Iustin Pop | " %(module)s:%(lineno)s %(message)s")
|
43 | 3b316acb | Iustin Pop | else:
|
44 | 59f187eb | Iustin Pop | fmt += "pid=%(process)d %(levelname)s %(message)s"
|
45 | 3b316acb | Iustin Pop | formatter = logging.Formatter(fmt) |
46 | 3b316acb | Iustin Pop | |
47 | 3b316acb | Iustin Pop | root_logger = logging.getLogger("")
|
48 | 3b316acb | Iustin Pop | root_logger.setLevel(logging.NOTSET) |
49 | 59f187eb | Iustin Pop | |
50 | ff5fac04 | Iustin Pop | if stderr_logging:
|
51 | 59f187eb | Iustin Pop | stderr_handler = logging.StreamHandler() |
52 | 59f187eb | Iustin Pop | stderr_handler.setFormatter(formatter) |
53 | 59f187eb | Iustin Pop | if debug:
|
54 | 59f187eb | Iustin Pop | stderr_handler.setLevel(logging.NOTSET) |
55 | 59f187eb | Iustin Pop | else:
|
56 | 59f187eb | Iustin Pop | stderr_handler.setLevel(logging.CRITICAL) |
57 | ff5fac04 | Iustin Pop | root_logger.addHandler(stderr_handler) |
58 | 3b316acb | Iustin Pop | |
59 | 59f187eb | Iustin Pop | # this can fail, if the logging directories are not setup or we have
|
60 | 59f187eb | Iustin Pop | # a permisssion problem; in this case, it's best to log but ignore
|
61 | 59f187eb | Iustin Pop | # the error if stderr_logging is True, and if false we re-raise the
|
62 | 59f187eb | Iustin Pop | # exception since otherwise we could run but without any logs at all
|
63 | 59f187eb | Iustin Pop | try:
|
64 | 59f187eb | Iustin Pop | logfile_handler = logging.FileHandler(logfile) |
65 | 59f187eb | Iustin Pop | logfile_handler.setFormatter(formatter) |
66 | 59f187eb | Iustin Pop | if debug:
|
67 | 59f187eb | Iustin Pop | logfile_handler.setLevel(logging.DEBUG) |
68 | 59f187eb | Iustin Pop | else:
|
69 | 59f187eb | Iustin Pop | logfile_handler.setLevel(logging.INFO) |
70 | 59f187eb | Iustin Pop | root_logger.addHandler(logfile_handler) |
71 | 59f187eb | Iustin Pop | except EnvironmentError, err: |
72 | 59f187eb | Iustin Pop | if stderr_logging:
|
73 | 59f187eb | Iustin Pop | logging.exception("Failed to enable logging to file '%s'", logfile)
|
74 | 59f187eb | Iustin Pop | else:
|
75 | 59f187eb | Iustin Pop | # we need to re-raise the exception
|
76 | 59f187eb | Iustin Pop | raise
|
77 | 59f187eb | Iustin Pop | |
78 | 3b316acb | Iustin Pop | |
79 | 2a2060ff | Michael Hanselmann | # Backwards compatibility
|
80 | 2a2060ff | Michael Hanselmann | Error = logging.error |
81 | 2a2060ff | Michael Hanselmann | Info = logging.info |
82 | 2a2060ff | Michael Hanselmann | Debug = logging.debug |
83 | a8083063 | Iustin Pop | |
84 | a8083063 | Iustin Pop | |
85 | a8083063 | Iustin Pop | def ToStdout(txt): |
86 | a8083063 | Iustin Pop | """Write a message to stdout only, bypassing the logging system
|
87 | a8083063 | Iustin Pop |
|
88 | a8083063 | Iustin Pop | Parameters:
|
89 | a8083063 | Iustin Pop | - txt: the message
|
90 | a8083063 | Iustin Pop |
|
91 | a8083063 | Iustin Pop | """
|
92 | a8083063 | Iustin Pop | sys.stdout.write(txt + '\n')
|
93 | a8083063 | Iustin Pop | sys.stdout.flush() |
94 | a8083063 | Iustin Pop | |
95 | a8083063 | Iustin Pop | |
96 | a8083063 | Iustin Pop | def ToStderr(txt): |
97 | a8083063 | Iustin Pop | """Write a message to stderr only, bypassing the logging system
|
98 | a8083063 | Iustin Pop |
|
99 | a8083063 | Iustin Pop | Parameters:
|
100 | a8083063 | Iustin Pop | - txt: the message
|
101 | a8083063 | Iustin Pop |
|
102 | a8083063 | Iustin Pop | """
|
103 | a8083063 | Iustin Pop | sys.stderr.write(txt + '\n')
|
104 | a8083063 | Iustin Pop | sys.stderr.flush() |