root / lib / logger.py @ 59f187eb
History | View | Annotate | Download (2.9 kB)
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 SetupLogging(logfile, debug=False, stderr_logging=False, program=""): |
39 |
"""Configures the logging module.
|
40 |
|
41 |
"""
|
42 |
fmt = "%(asctime)s: " + program + " " |
43 |
if debug:
|
44 |
fmt += ("pid=%(process)d/%(threadName)s %(levelname)s"
|
45 |
" %(module)s:%(lineno)s %(message)s")
|
46 |
else:
|
47 |
fmt += "pid=%(process)d %(levelname)s %(message)s"
|
48 |
formatter = logging.Formatter(fmt) |
49 |
|
50 |
root_logger = logging.getLogger("")
|
51 |
root_logger.setLevel(logging.NOTSET) |
52 |
|
53 |
if stderr_logging:
|
54 |
stderr_handler = logging.StreamHandler() |
55 |
stderr_handler.setFormatter(formatter) |
56 |
if debug:
|
57 |
stderr_handler.setLevel(logging.NOTSET) |
58 |
else:
|
59 |
stderr_handler.setLevel(logging.CRITICAL) |
60 |
root_logger.addHandler(stderr_handler) |
61 |
|
62 |
# this can fail, if the logging directories are not setup or we have
|
63 |
# a permisssion problem; in this case, it's best to log but ignore
|
64 |
# the error if stderr_logging is True, and if false we re-raise the
|
65 |
# exception since otherwise we could run but without any logs at all
|
66 |
try:
|
67 |
logfile_handler = logging.FileHandler(logfile) |
68 |
logfile_handler.setFormatter(formatter) |
69 |
if debug:
|
70 |
logfile_handler.setLevel(logging.DEBUG) |
71 |
else:
|
72 |
logfile_handler.setLevel(logging.INFO) |
73 |
root_logger.addHandler(logfile_handler) |
74 |
except EnvironmentError, err: |
75 |
if stderr_logging:
|
76 |
logging.exception("Failed to enable logging to file '%s'", logfile)
|
77 |
else:
|
78 |
# we need to re-raise the exception
|
79 |
raise
|
80 |
|
81 |
|
82 |
# Backwards compatibility
|
83 |
Error = logging.error |
84 |
Info = logging.info |
85 |
Debug = logging.debug |
86 |
|
87 |
|
88 |
def ToStdout(txt): |
89 |
"""Write a message to stdout only, bypassing the logging system
|
90 |
|
91 |
Parameters:
|
92 |
- txt: the message
|
93 |
|
94 |
"""
|
95 |
sys.stdout.write(txt + '\n')
|
96 |
sys.stdout.flush() |
97 |
|
98 |
|
99 |
def ToStderr(txt): |
100 |
"""Write a message to stderr only, bypassing the logging system
|
101 |
|
102 |
Parameters:
|
103 |
- txt: the message
|
104 |
|
105 |
"""
|
106 |
sys.stderr.write(txt + '\n')
|
107 |
sys.stderr.flush() |