Statistics
| Branch: | Tag: | Revision:

root / lib / logger.py @ e2212007

History | View | Annotate | Download (3.7 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 ff5fac04 Iustin Pop
def SetupDaemon(logfile, debug=False, stderr_logging=False):
88 3b316acb Iustin Pop
  """Configures the logging module for daemons
89 3b316acb Iustin Pop

90 3b316acb Iustin Pop
  """
91 3b316acb Iustin Pop
  if debug:
92 a237d0a8 Iustin Pop
    fmt = ("%(asctime)s: pid=%(process)d %(levelname)s"
93 a237d0a8 Iustin Pop
           " %(module)s:%(lineno)s %(message)s")
94 3b316acb Iustin Pop
  else:
95 a237d0a8 Iustin Pop
    fmt = "%(asctime)s: pid=%(process)d %(levelname)s %(message)s"
96 3b316acb Iustin Pop
  formatter = logging.Formatter(fmt)
97 3b316acb Iustin Pop
98 3b316acb Iustin Pop
  logfile_handler = logging.FileHandler(logfile)
99 3b316acb Iustin Pop
  logfile_handler.setFormatter(formatter)
100 3b316acb Iustin Pop
101 3b316acb Iustin Pop
  stderr_handler = logging.StreamHandler()
102 3b316acb Iustin Pop
  stderr_handler.setFormatter(formatter)
103 3b316acb Iustin Pop
  if debug:
104 3b316acb Iustin Pop
    logfile_handler.setLevel(logging.DEBUG)
105 3b316acb Iustin Pop
    stderr_handler.setLevel(logging.NOTSET)
106 3b316acb Iustin Pop
  else:
107 3b316acb Iustin Pop
    logfile_handler.setLevel(logging.INFO)
108 3b316acb Iustin Pop
    stderr_handler.setLevel(logging.CRITICAL)
109 3b316acb Iustin Pop
110 3b316acb Iustin Pop
  root_logger = logging.getLogger("")
111 3b316acb Iustin Pop
  root_logger.setLevel(logging.NOTSET)
112 3b316acb Iustin Pop
  root_logger.addHandler(logfile_handler)
113 ff5fac04 Iustin Pop
  if stderr_logging:
114 ff5fac04 Iustin Pop
    root_logger.addHandler(stderr_handler)
115 3b316acb Iustin Pop
116 3b316acb Iustin Pop
117 2a2060ff Michael Hanselmann
# Backwards compatibility
118 2a2060ff Michael Hanselmann
Error = logging.error
119 2a2060ff Michael Hanselmann
Info = logging.info
120 2a2060ff Michael Hanselmann
Debug = logging.debug
121 a8083063 Iustin Pop
122 a8083063 Iustin Pop
123 a8083063 Iustin Pop
def ToStdout(txt):
124 a8083063 Iustin Pop
  """Write a message to stdout only, bypassing the logging system
125 a8083063 Iustin Pop

126 a8083063 Iustin Pop
  Parameters:
127 a8083063 Iustin Pop
    - txt: the message
128 a8083063 Iustin Pop

129 a8083063 Iustin Pop
  """
130 a8083063 Iustin Pop
  sys.stdout.write(txt + '\n')
131 a8083063 Iustin Pop
  sys.stdout.flush()
132 a8083063 Iustin Pop
133 a8083063 Iustin Pop
134 a8083063 Iustin Pop
def ToStderr(txt):
135 a8083063 Iustin Pop
  """Write a message to stderr only, bypassing the logging system
136 a8083063 Iustin Pop

137 a8083063 Iustin Pop
  Parameters:
138 a8083063 Iustin Pop
    - txt: the message
139 a8083063 Iustin Pop

140 a8083063 Iustin Pop
  """
141 a8083063 Iustin Pop
  sys.stderr.write(txt + '\n')
142 a8083063 Iustin Pop
  sys.stderr.flush()