Statistics
| Branch: | Tag: | Revision:

root / lib / logger.py @ 04864530

History | View | Annotate | Download (2.8 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
  stderr_fmt = "%(asctime)s: %(message)s"
58
  stderr_formatter = logging.Formatter(stderr_fmt)
59

    
60
  info_file = _CreateFileHandler("info")
61
  info_file.setLevel(logging.INFO)
62
  info_file.setFormatter(formatter)
63

    
64
  errors_file = _CreateFileHandler("errors")
65
  errors_file.setLevel(logging.ERROR)
66
  errors_file.setFormatter(formatter)
67

    
68
  debug_file = _CreateFileHandler("debug")
69
  debug_file.setLevel(logging.DEBUG)
70
  debug_file.setFormatter(formatter)
71

    
72
  stderr_file = logging.StreamHandler()
73
  stderr_file.setFormatter(stderr_formatter)
74
  if debug:
75
    stderr_file.setLevel(logging.NOTSET)
76
  else:
77
    stderr_file.setLevel(logging.ERROR)
78

    
79
  root_logger = logging.getLogger("")
80
  root_logger.setLevel(logging.NOTSET)
81
  root_logger.addHandler(info_file)
82
  root_logger.addHandler(errors_file)
83
  root_logger.addHandler(debug_file)
84
  root_logger.addHandler(stderr_file)
85

    
86

    
87
# Backwards compatibility
88
Error = logging.error
89
Info = logging.info
90
Debug = logging.debug
91

    
92

    
93
def ToStdout(txt):
94
  """Write a message to stdout only, bypassing the logging system
95

96
  Parameters:
97
    - txt: the message
98

99
  """
100
  sys.stdout.write(txt + '\n')
101
  sys.stdout.flush()
102

    
103

    
104
def ToStderr(txt):
105
  """Write a message to stderr only, bypassing the logging system
106

107
  Parameters:
108
    - txt: the message
109

110
  """
111
  sys.stderr.write(txt + '\n')
112
  sys.stderr.flush()