root / lib / logger.py @ 04864530
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 | import os, os.path |
34 | a8083063 | Iustin Pop | |
35 | a8083063 | Iustin Pop | from ganeti import constants |
36 | a8083063 | Iustin Pop | |
37 | a8083063 | Iustin Pop | |
38 | 2a2060ff | Michael Hanselmann | def _CreateFileHandler(name): |
39 | 2a2060ff | Michael Hanselmann | return logging.FileHandler(os.path.join(constants.LOG_DIR, name))
|
40 | a8083063 | Iustin Pop | |
41 | a8083063 | Iustin Pop | |
42 | 2a2060ff | Michael Hanselmann | def SetupLogging(program='ganeti', debug=False): |
43 | a8083063 | Iustin Pop | """Setup logging for ganeti
|
44 | a8083063 | Iustin Pop |
|
45 | a8083063 | Iustin Pop | On failure, a check is made whether process is run by root or not,
|
46 | a8083063 | Iustin Pop | and an appropriate error message is printed on stderr, then process
|
47 | a8083063 | Iustin Pop | exits.
|
48 | a8083063 | Iustin Pop |
|
49 | 2a2060ff | Michael Hanselmann | Args:
|
50 | 2a2060ff | Michael Hanselmann | debug: Whether to enable verbose logging
|
51 | 2a2060ff | Michael Hanselmann | program: Program name
|
52 | a8083063 | Iustin Pop |
|
53 | a8083063 | Iustin Pop | """
|
54 | 2a2060ff | Michael Hanselmann | fmt = "%(asctime)s " + program + ": %(message)s" |
55 | 2a2060ff | Michael Hanselmann | formatter = logging.Formatter(fmt) |
56 | a8083063 | Iustin Pop | |
57 | 5023934a | Michael Hanselmann | stderr_fmt = "%(asctime)s: %(message)s"
|
58 | 5023934a | Michael Hanselmann | stderr_formatter = logging.Formatter(stderr_fmt) |
59 | 5023934a | Michael Hanselmann | |
60 | 2a2060ff | Michael Hanselmann | info_file = _CreateFileHandler("info")
|
61 | 2a2060ff | Michael Hanselmann | info_file.setLevel(logging.INFO) |
62 | 2a2060ff | Michael Hanselmann | info_file.setFormatter(formatter) |
63 | a8083063 | Iustin Pop | |
64 | 2a2060ff | Michael Hanselmann | errors_file = _CreateFileHandler("errors")
|
65 | 2a2060ff | Michael Hanselmann | errors_file.setLevel(logging.ERROR) |
66 | 2a2060ff | Michael Hanselmann | errors_file.setFormatter(formatter) |
67 | a8083063 | Iustin Pop | |
68 | 2a2060ff | Michael Hanselmann | debug_file = _CreateFileHandler("debug")
|
69 | 2a2060ff | Michael Hanselmann | debug_file.setLevel(logging.DEBUG) |
70 | 2a2060ff | Michael Hanselmann | debug_file.setFormatter(formatter) |
71 | a8083063 | Iustin Pop | |
72 | 2a2060ff | Michael Hanselmann | stderr_file = logging.StreamHandler() |
73 | 5023934a | Michael Hanselmann | stderr_file.setFormatter(stderr_formatter) |
74 | 2a2060ff | Michael Hanselmann | if debug:
|
75 | 2a2060ff | Michael Hanselmann | stderr_file.setLevel(logging.NOTSET) |
76 | 2a2060ff | Michael Hanselmann | else:
|
77 | 2a2060ff | Michael Hanselmann | stderr_file.setLevel(logging.ERROR) |
78 | a8083063 | Iustin Pop | |
79 | 2a2060ff | Michael Hanselmann | root_logger = logging.getLogger("")
|
80 | 2a2060ff | Michael Hanselmann | root_logger.setLevel(logging.NOTSET) |
81 | 2a2060ff | Michael Hanselmann | root_logger.addHandler(info_file) |
82 | 2a2060ff | Michael Hanselmann | root_logger.addHandler(errors_file) |
83 | 2a2060ff | Michael Hanselmann | root_logger.addHandler(debug_file) |
84 | 2a2060ff | Michael Hanselmann | root_logger.addHandler(stderr_file) |
85 | a8083063 | Iustin Pop | |
86 | 2a2060ff | Michael Hanselmann | |
87 | 2a2060ff | Michael Hanselmann | # Backwards compatibility
|
88 | 2a2060ff | Michael Hanselmann | Error = logging.error |
89 | 2a2060ff | Michael Hanselmann | Info = logging.info |
90 | 2a2060ff | Michael Hanselmann | Debug = logging.debug |
91 | a8083063 | Iustin Pop | |
92 | a8083063 | Iustin Pop | |
93 | a8083063 | Iustin Pop | def ToStdout(txt): |
94 | a8083063 | Iustin Pop | """Write a message to stdout only, bypassing the logging system
|
95 | a8083063 | Iustin Pop |
|
96 | a8083063 | Iustin Pop | Parameters:
|
97 | a8083063 | Iustin Pop | - txt: the message
|
98 | a8083063 | Iustin Pop |
|
99 | a8083063 | Iustin Pop | """
|
100 | a8083063 | Iustin Pop | sys.stdout.write(txt + '\n')
|
101 | a8083063 | Iustin Pop | sys.stdout.flush() |
102 | a8083063 | Iustin Pop | |
103 | a8083063 | Iustin Pop | |
104 | a8083063 | Iustin Pop | def ToStderr(txt): |
105 | a8083063 | Iustin Pop | """Write a message to stderr only, bypassing the logging system
|
106 | a8083063 | Iustin Pop |
|
107 | a8083063 | Iustin Pop | Parameters:
|
108 | a8083063 | Iustin Pop | - txt: the message
|
109 | a8083063 | Iustin Pop |
|
110 | a8083063 | Iustin Pop | """
|
111 | a8083063 | Iustin Pop | sys.stderr.write(txt + '\n')
|
112 | a8083063 | Iustin Pop | sys.stderr.flush() |