direct-io.hg

view tools/python/xen/xend/XendLogging.py @ 7398:446aa56ca4fe

Added a TRACE log level, for those versions of Python that do not have it, and
moved XendDomainInfo.update's debugging onto that level, as it has become
overly verbose.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Oct 17 13:50:28 2005 +0100 (2005-10-17)
parents 90f4f4dc3f62
children 69dc687f0364 83a882b3d807
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 # Copyright (C) 2005 XenSource Ltd
17 #============================================================================
20 import tempfile
21 import types
22 import logging
23 import logging.handlers
26 __all__ = [ 'log', 'init', 'getLogFilename', 'addLogStderr',
27 'removeLogStderr' ]
30 if not 'TRACE' in logging.__dict__:
31 logging.TRACE = logging.DEBUG - 1
32 logging.addLevelName(logging.TRACE,'TRACE')
33 def trace(self, *args, **kwargs):
34 self.log(logging.TRACE, *args, **kwargs)
35 logging.Logger.trace = trace
38 log = logging.getLogger("xend")
41 DEFAULT_MAX_BYTES = 1 << 20 # 1MB
42 DEFAULT_BACKUP_COUNT = 5
44 STDERR_FORMAT = "[%(name)s] %(levelname)s (%(module)s:%(lineno)d) %(message)s"
45 LOGFILE_FORMAT = "[%(asctime)s %(name)s] %(levelname)s (%(module)s:%(lineno)d) %(message)s"
46 DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
49 stderrHandler = logging.StreamHandler()
50 stderrHandler.setFormatter(logging.Formatter(STDERR_FORMAT, DATE_FORMAT))
52 logfilename = None
55 def init(filename, level=logging.INFO, maxBytes=None, backupCount=None):
56 """Initialise logging. Logs to 'filename' by default, but does not log to
57 stderr unless addLogStderr() is called.
58 """
60 global logfilename
62 def openFileHandler(fname):
63 return logging.handlers.RotatingFileHandler(fname,
64 mode='a',
65 maxBytes=maxBytes,
66 backupCount=backupCount)
68 if not maxBytes:
69 maxBytes = DEFAULT_MAX_BYTES
70 if not backupCount:
71 backupCount = DEFAULT_BACKUP_COUNT
73 # Rather unintuitively, getLevelName will get the number corresponding to
74 # a level name, as well as getting the name corresponding to a level
75 # number. setLevel seems to take the number only though, so convert if we
76 # are given a string.
77 if isinstance(level, types.StringType):
78 level = logging.getLevelName(level)
80 log.setLevel(level)
82 try:
83 fileHandler = openFileHandler(filename)
84 logfilename = filename
85 except IOError:
86 logfilename = tempfile.mkstemp("-xend.log")[1]
87 fileHandler = openFileHandler(logfilename)
89 fileHandler.setFormatter(logging.Formatter(LOGFILE_FORMAT, DATE_FORMAT))
90 log.addHandler(fileHandler)
93 def getLogFilename():
94 return logfilename
97 def addLogStderr():
98 """Add logging to stderr."""
99 log.addHandler(stderrHandler)
102 def removeLogStderr():
103 """Remove logging to stderr."""
104 log.removeHandler(stderrHandler)