logger.py 2.65 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
#
Iustin Pop's avatar
Iustin Pop committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Logging for Ganeti

This module abstracts the logging handling away from the rest of the
Ganeti code. It offers some utility functions for easy logging.
26

Iustin Pop's avatar
Iustin Pop committed
27
28
29
30
31
32
33
34
35
36
37
"""

# pylint: disable-msg=W0603,C0103

import sys
import logging
import os, os.path

from ganeti import constants


38
39
def _CreateFileHandler(name):
  return logging.FileHandler(os.path.join(constants.LOG_DIR, name))
Iustin Pop's avatar
Iustin Pop committed
40
41


42
def SetupLogging(program='ganeti', debug=False):
Iustin Pop's avatar
Iustin Pop committed
43
44
45
46
47
48
  """Setup logging for ganeti

  On failure, a check is made whether process is run by root or not,
  and an appropriate error message is printed on stderr, then process
  exits.

49
50
51
  Args:
    debug: Whether to enable verbose logging
    program: Program name
Iustin Pop's avatar
Iustin Pop committed
52
53

  """
54
55
  fmt = "%(asctime)s " + program + ": %(message)s"
  formatter = logging.Formatter(fmt)
Iustin Pop's avatar
Iustin Pop committed
56

57
58
59
  info_file = _CreateFileHandler("info")
  info_file.setLevel(logging.INFO)
  info_file.setFormatter(formatter)
Iustin Pop's avatar
Iustin Pop committed
60

61
62
63
  errors_file = _CreateFileHandler("errors")
  errors_file.setLevel(logging.ERROR)
  errors_file.setFormatter(formatter)
Iustin Pop's avatar
Iustin Pop committed
64

65
66
67
  debug_file = _CreateFileHandler("debug")
  debug_file.setLevel(logging.DEBUG)
  debug_file.setFormatter(formatter)
Iustin Pop's avatar
Iustin Pop committed
68

69
70
71
72
73
  stderr_file = logging.StreamHandler()
  if debug:
    stderr_file.setLevel(logging.NOTSET)
  else:
    stderr_file.setLevel(logging.ERROR)
Iustin Pop's avatar
Iustin Pop committed
74

75
76
77
78
79
80
  root_logger = logging.getLogger("")
  root_logger.setLevel(logging.NOTSET)
  root_logger.addHandler(info_file)
  root_logger.addHandler(errors_file)
  root_logger.addHandler(debug_file)
  root_logger.addHandler(stderr_file)
Iustin Pop's avatar
Iustin Pop committed
81

82
83
84
85
86

# Backwards compatibility
Error = logging.error
Info = logging.info
Debug = logging.debug
Iustin Pop's avatar
Iustin Pop committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108


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

  Parameters:
    - txt: the message

  """
  sys.stdout.write(txt + '\n')
  sys.stdout.flush()


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

  Parameters:
    - txt: the message

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