Revision 82d9caef

b/Makefile.am
72 72
	lib/jqueue.py \
73 73
	lib/jstore.py \
74 74
	lib/locking.py \
75
	lib/logger.py \
76 75
	lib/luxi.py \
77 76
	lib/mcpu.py \
78 77
	lib/objects.py \
b/daemons/ganeti-masterd
50 50
from ganeti import utils
51 51
from ganeti import errors
52 52
from ganeti import ssconf
53
from ganeti import logger
54 53
from ganeti import workerpool
55 54
from ganeti import rpc
56 55
from ganeti import bootstrap
......
444 443

  
445 444
  utils.WritePidFile(constants.MASTERD_PID)
446 445

  
447
  logger.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug,
448
                      stderr_logging=not options.fork)
446
  utils.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug,
447
                     stderr_logging=not options.fork)
449 448

  
450 449
  logging.info("ganeti master daemon startup")
451 450

  
b/daemons/ganeti-noded
35 35
from optparse import OptionParser
36 36

  
37 37
from ganeti import backend
38
from ganeti import logger
39 38
from ganeti import constants
40 39
from ganeti import objects
41 40
from ganeti import errors
......
688 687

  
689 688
  utils.WritePidFile(constants.NODED_PID)
690 689
  try:
691
    logger.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug,
692
                        stderr_logging=not options.fork)
690
    utils.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug,
691
                       stderr_logging=not options.fork)
693 692
    logging.info("ganeti node daemon startup")
694 693

  
695 694
    # Prepare job queue
b/daemons/ganeti-rapi
28 28
import os
29 29
import signal
30 30

  
31
from ganeti import logger
32 31
from ganeti import constants
33 32
from ganeti import errors
34 33
from ganeti import http
......
127 126
  if options.fork:
128 127
    utils.Daemonize(logfile=constants.LOG_RAPISERVER)
129 128

  
130
  logger.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
129
  utils.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
131 130
                     stderr_logging=not options.fork)
132 131

  
133 132
  utils.WritePidFile(constants.RAPI_PID)
b/daemons/ganeti-watcher
38 38
from ganeti import serializer
39 39
from ganeti import errors
40 40
from ganeti import opcodes
41
from ganeti import logger
42 41
from ganeti import cli
43 42

  
44 43

  
......
423 422

  
424 423
  options, args = ParseOptions()
425 424

  
426
  logger.SetupLogging(constants.LOG_WATCHER, debug=options.debug,
427
                      stderr_logging=options.debug)
425
  utils.SetupLogging(constants.LOG_WATCHER, debug=options.debug,
426
                     stderr_logging=options.debug)
428 427

  
429 428
  try:
430 429
    client = cli.GetClient()
b/lib/cli.py
31 31
from cStringIO import StringIO
32 32

  
33 33
from ganeti import utils
34
from ganeti import logger
35 34
from ganeti import errors
36 35
from ganeti import constants
37 36
from ganeti import opcodes
......
714 713
    for key, val in override.iteritems():
715 714
      setattr(options, key, val)
716 715

  
717
  logger.SetupLogging(constants.LOG_COMMANDS, debug=options.debug,
718
                      stderr_logging=True, program=binary)
716
  utils.SetupLogging(constants.LOG_COMMANDS, debug=options.debug,
717
                     stderr_logging=True, program=binary)
719 718

  
720 719
  utils.debug = options.debug
721 720

  
/dev/null
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

  
34

  
35
def SetupLogging(logfile, debug=False, stderr_logging=False, program=""):
36
  """Configures the logging module.
37

  
38
  """
39
  fmt = "%(asctime)s: " + program + " "
40
  if debug:
41
    fmt += ("pid=%(process)d/%(threadName)s %(levelname)s"
42
           " %(module)s:%(lineno)s %(message)s")
43
  else:
44
    fmt += "pid=%(process)d %(levelname)s %(message)s"
45
  formatter = logging.Formatter(fmt)
46

  
47
  root_logger = logging.getLogger("")
48
  root_logger.setLevel(logging.NOTSET)
49

  
50
  if stderr_logging:
51
    stderr_handler = logging.StreamHandler()
52
    stderr_handler.setFormatter(formatter)
53
    if debug:
54
      stderr_handler.setLevel(logging.NOTSET)
55
    else:
56
      stderr_handler.setLevel(logging.CRITICAL)
57
    root_logger.addHandler(stderr_handler)
58

  
59
  # this can fail, if the logging directories are not setup or we have
60
  # a permisssion problem; in this case, it's best to log but ignore
61
  # the error if stderr_logging is True, and if false we re-raise the
62
  # exception since otherwise we could run but without any logs at all
63
  try:
64
    logfile_handler = logging.FileHandler(logfile)
65
    logfile_handler.setFormatter(formatter)
66
    if debug:
67
      logfile_handler.setLevel(logging.DEBUG)
68
    else:
69
      logfile_handler.setLevel(logging.INFO)
70
    root_logger.addHandler(logfile_handler)
71
  except EnvironmentError, err:
72
    if stderr_logging:
73
      logging.exception("Failed to enable logging to file '%s'", logfile)
74
    else:
75
      # we need to re-raise the exception
76
      raise
77

  
78

  
79
# Backwards compatibility
80
Error = logging.error
81
Info = logging.info
82
Debug = logging.debug
83

  
84

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

  
88
  Parameters:
89
    - txt: the message
90

  
91
  """
92
  sys.stdout.write(txt + '\n')
93
  sys.stdout.flush()
94

  
95

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

  
99
  Parameters:
100
    - txt: the message
101

  
102
  """
103
  sys.stderr.write(txt + '\n')
104
  sys.stderr.flush()
b/lib/utils.py
1269 1269
  return ReadFile(constants.CLUSTER_PASSWORD_FILE)
1270 1270

  
1271 1271

  
1272
def SetupLogging(logfile, debug=False, stderr_logging=False, program=""):
1273
  """Configures the logging module.
1274

  
1275
  """
1276
  fmt = "%(asctime)s: " + program + " "
1277
  if debug:
1278
    fmt += ("pid=%(process)d/%(threadName)s %(levelname)s"
1279
           " %(module)s:%(lineno)s %(message)s")
1280
  else:
1281
    fmt += "pid=%(process)d %(levelname)s %(message)s"
1282
  formatter = logging.Formatter(fmt)
1283

  
1284
  root_logger = logging.getLogger("")
1285
  root_logger.setLevel(logging.NOTSET)
1286

  
1287
  if stderr_logging:
1288
    stderr_handler = logging.StreamHandler()
1289
    stderr_handler.setFormatter(formatter)
1290
    if debug:
1291
      stderr_handler.setLevel(logging.NOTSET)
1292
    else:
1293
      stderr_handler.setLevel(logging.CRITICAL)
1294
    root_logger.addHandler(stderr_handler)
1295

  
1296
  # this can fail, if the logging directories are not setup or we have
1297
  # a permisssion problem; in this case, it's best to log but ignore
1298
  # the error if stderr_logging is True, and if false we re-raise the
1299
  # exception since otherwise we could run but without any logs at all
1300
  try:
1301
    logfile_handler = logging.FileHandler(logfile)
1302
    logfile_handler.setFormatter(formatter)
1303
    if debug:
1304
      logfile_handler.setLevel(logging.DEBUG)
1305
    else:
1306
      logfile_handler.setLevel(logging.INFO)
1307
    root_logger.addHandler(logfile_handler)
1308
  except EnvironmentError, err:
1309
    if stderr_logging:
1310
      logging.exception("Failed to enable logging to file '%s'", logfile)
1311
    else:
1312
      # we need to re-raise the exception
1313
      raise
1314

  
1315

  
1272 1316
def LockedMethod(fn):
1273 1317
  """Synchronized object access decorator.
1274 1318

  
b/tools/burnin
34 34
from ganeti import mcpu
35 35
from ganeti import constants
36 36
from ganeti import cli
37
from ganeti import logger
38 37
from ganeti import errors
39 38
from ganeti import utils
40 39

  
......
63 62

  
64 63
  def __init__(self):
65 64
    """Constructor."""
66
    logger.SetupLogging(constants.LOG_BURNIN, debug=False, stderr_logging=True)
65
    utils.SetupLogging(constants.LOG_BURNIN, debug=False, stderr_logging=True)
67 66
    self._feed_buf = StringIO()
68 67
    self.nodes = []
69 68
    self.instances = []

Also available in: Unified diff