root / lib / logger.py @ 2a2060ff
History | View | Annotate | Download (2.7 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 _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 |
info_file = _CreateFileHandler("info")
|
58 |
info_file.setLevel(logging.INFO) |
59 |
info_file.setFormatter(formatter) |
60 |
|
61 |
errors_file = _CreateFileHandler("errors")
|
62 |
errors_file.setLevel(logging.ERROR) |
63 |
errors_file.setFormatter(formatter) |
64 |
|
65 |
debug_file = _CreateFileHandler("debug")
|
66 |
debug_file.setLevel(logging.DEBUG) |
67 |
debug_file.setFormatter(formatter) |
68 |
|
69 |
stderr_file = logging.StreamHandler() |
70 |
if debug:
|
71 |
stderr_file.setLevel(logging.NOTSET) |
72 |
else:
|
73 |
stderr_file.setLevel(logging.ERROR) |
74 |
|
75 |
root_logger = logging.getLogger("")
|
76 |
root_logger.setLevel(logging.NOTSET) |
77 |
root_logger.addHandler(info_file) |
78 |
root_logger.addHandler(errors_file) |
79 |
root_logger.addHandler(debug_file) |
80 |
root_logger.addHandler(stderr_file) |
81 |
|
82 |
|
83 |
# Backwards compatibility
|
84 |
Error = logging.error |
85 |
Info = logging.info |
86 |
Debug = logging.debug |
87 |
|
88 |
|
89 |
def ToStdout(txt): |
90 |
"""Write a message to stdout only, bypassing the logging system
|
91 |
|
92 |
Parameters:
|
93 |
- txt: the message
|
94 |
|
95 |
"""
|
96 |
sys.stdout.write(txt + '\n')
|
97 |
sys.stdout.flush() |
98 |
|
99 |
|
100 |
def ToStderr(txt): |
101 |
"""Write a message to stderr only, bypassing the logging system
|
102 |
|
103 |
Parameters:
|
104 |
- txt: the message
|
105 |
|
106 |
"""
|
107 |
sys.stderr.write(txt + '\n')
|
108 |
sys.stderr.flush() |