Commit 82d9caef authored by Iustin Pop's avatar Iustin Pop
Browse files

Remove the logger.py module

Since now we use only one function from the logger module
(SetupLogging), we move it to utils.py (which is already imported by all
users of this function), and we remove the module.

Reviewed-by: imsnah
parent 263ab7cf
......@@ -72,7 +72,6 @@ pkgpython_PYTHON = \
lib/jqueue.py \
lib/jstore.py \
lib/locking.py \
lib/logger.py \
lib/luxi.py \
lib/mcpu.py \
lib/objects.py \
......
......@@ -50,7 +50,6 @@ from ganeti import luxi
from ganeti import utils
from ganeti import errors
from ganeti import ssconf
from ganeti import logger
from ganeti import workerpool
from ganeti import rpc
from ganeti import bootstrap
......@@ -444,8 +443,8 @@ def main():
utils.WritePidFile(constants.MASTERD_PID)
logger.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug,
stderr_logging=not options.fork)
utils.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug,
stderr_logging=not options.fork)
logging.info("ganeti master daemon startup")
......
......@@ -35,7 +35,6 @@ import signal
from optparse import OptionParser
from ganeti import backend
from ganeti import logger
from ganeti import constants
from ganeti import objects
from ganeti import errors
......@@ -688,8 +687,8 @@ def main():
utils.WritePidFile(constants.NODED_PID)
try:
logger.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug,
stderr_logging=not options.fork)
utils.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug,
stderr_logging=not options.fork)
logging.info("ganeti node daemon startup")
# Prepare job queue
......
......@@ -28,7 +28,6 @@ import sys
import os
import signal
from ganeti import logger
from ganeti import constants
from ganeti import errors
from ganeti import http
......@@ -127,7 +126,7 @@ def main():
if options.fork:
utils.Daemonize(logfile=constants.LOG_RAPISERVER)
logger.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
utils.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
stderr_logging=not options.fork)
utils.WritePidFile(constants.RAPI_PID)
......
......@@ -38,7 +38,6 @@ from ganeti import constants
from ganeti import serializer
from ganeti import errors
from ganeti import opcodes
from ganeti import logger
from ganeti import cli
......@@ -423,8 +422,8 @@ def main():
options, args = ParseOptions()
logger.SetupLogging(constants.LOG_WATCHER, debug=options.debug,
stderr_logging=options.debug)
utils.SetupLogging(constants.LOG_WATCHER, debug=options.debug,
stderr_logging=options.debug)
try:
client = cli.GetClient()
......
......@@ -31,7 +31,6 @@ import logging
from cStringIO import StringIO
from ganeti import utils
from ganeti import logger
from ganeti import errors
from ganeti import constants
from ganeti import opcodes
......@@ -714,8 +713,8 @@ def GenericMain(commands, override=None, aliases=None):
for key, val in override.iteritems():
setattr(options, key, val)
logger.SetupLogging(constants.LOG_COMMANDS, debug=options.debug,
stderr_logging=True, program=binary)
utils.SetupLogging(constants.LOG_COMMANDS, debug=options.debug,
stderr_logging=True, program=binary)
utils.debug = options.debug
......
#
#
# 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.
"""
# pylint: disable-msg=W0603,C0103
import sys
import logging
def SetupLogging(logfile, debug=False, stderr_logging=False, program=""):
"""Configures the logging module.
"""
fmt = "%(asctime)s: " + program + " "
if debug:
fmt += ("pid=%(process)d/%(threadName)s %(levelname)s"
" %(module)s:%(lineno)s %(message)s")
else:
fmt += "pid=%(process)d %(levelname)s %(message)s"
formatter = logging.Formatter(fmt)
root_logger = logging.getLogger("")
root_logger.setLevel(logging.NOTSET)
if stderr_logging:
stderr_handler = logging.StreamHandler()
stderr_handler.setFormatter(formatter)
if debug:
stderr_handler.setLevel(logging.NOTSET)
else:
stderr_handler.setLevel(logging.CRITICAL)
root_logger.addHandler(stderr_handler)
# this can fail, if the logging directories are not setup or we have
# a permisssion problem; in this case, it's best to log but ignore
# the error if stderr_logging is True, and if false we re-raise the
# exception since otherwise we could run but without any logs at all
try:
logfile_handler = logging.FileHandler(logfile)
logfile_handler.setFormatter(formatter)
if debug:
logfile_handler.setLevel(logging.DEBUG)
else:
logfile_handler.setLevel(logging.INFO)
root_logger.addHandler(logfile_handler)
except EnvironmentError, err:
if stderr_logging:
logging.exception("Failed to enable logging to file '%s'", logfile)
else:
# we need to re-raise the exception
raise
# Backwards compatibility
Error = logging.error
Info = logging.info
Debug = logging.debug
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()
......@@ -1269,6 +1269,50 @@ def GetNodeDaemonPassword():
return ReadFile(constants.CLUSTER_PASSWORD_FILE)
def SetupLogging(logfile, debug=False, stderr_logging=False, program=""):
"""Configures the logging module.
"""
fmt = "%(asctime)s: " + program + " "
if debug:
fmt += ("pid=%(process)d/%(threadName)s %(levelname)s"
" %(module)s:%(lineno)s %(message)s")
else:
fmt += "pid=%(process)d %(levelname)s %(message)s"
formatter = logging.Formatter(fmt)
root_logger = logging.getLogger("")
root_logger.setLevel(logging.NOTSET)
if stderr_logging:
stderr_handler = logging.StreamHandler()
stderr_handler.setFormatter(formatter)
if debug:
stderr_handler.setLevel(logging.NOTSET)
else:
stderr_handler.setLevel(logging.CRITICAL)
root_logger.addHandler(stderr_handler)
# this can fail, if the logging directories are not setup or we have
# a permisssion problem; in this case, it's best to log but ignore
# the error if stderr_logging is True, and if false we re-raise the
# exception since otherwise we could run but without any logs at all
try:
logfile_handler = logging.FileHandler(logfile)
logfile_handler.setFormatter(formatter)
if debug:
logfile_handler.setLevel(logging.DEBUG)
else:
logfile_handler.setLevel(logging.INFO)
root_logger.addHandler(logfile_handler)
except EnvironmentError, err:
if stderr_logging:
logging.exception("Failed to enable logging to file '%s'", logfile)
else:
# we need to re-raise the exception
raise
def LockedMethod(fn):
"""Synchronized object access decorator.
......
......@@ -34,7 +34,6 @@ from ganeti import opcodes
from ganeti import mcpu
from ganeti import constants
from ganeti import cli
from ganeti import logger
from ganeti import errors
from ganeti import utils
......@@ -63,7 +62,7 @@ class Burner(object):
def __init__(self):
"""Constructor."""
logger.SetupLogging(constants.LOG_BURNIN, debug=False, stderr_logging=True)
utils.SetupLogging(constants.LOG_BURNIN, debug=False, stderr_logging=True)
self._feed_buf = StringIO()
self.nodes = []
self.instances = []
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment