comparison roundup/rlog.py @ 2397:fa50e1347397

added basic logging support
author Richard Jones <richard@users.sourceforge.net>
date Tue, 08 Jun 2004 05:29:18 +0000
parents
children 15f3a6211a2d
comparison
equal deleted inserted replaced
2396:39cdddd313ca 2397:fa50e1347397
1 '''Roundup basic logging support.
2
3 This module will use the standard Python logging implementation when
4 available. If not, then a basic logging implementation, BasicLogging and
5 BasicLoggingChannel will be used.
6
7 Configuration for "logging" module:
8 - tracker configuration file specifies the location of a logging
9 configration file as LOGGING_CONFIG
10 - roundup-server and roundup-mailgw specify the location of a logging
11 configuration file on the command line
12 Configuration for "BasicLogging" implementation:
13 - tracker configuration file specifies the location of a log file
14 LOGGING_FILENAME
15 - tracker configuration file specifies the level to log to as
16 LOGGING_LEVEL
17 - roundup-server and roundup-mailgw specify the location of a log
18 file on the command line
19 - roundup-server and roundup-mailgw specify the level to log to on
20 the command line
21
22 In both cases, if no logfile is specified then logging will simply be sent
23 to sys.stderr with only logging of ERROR messages.
24
25 In terms of the Roundup code using the logging implementation:
26 - a "logging" object will be available on the "config" object for each
27 tracker
28 - the roundup-server and roundup-mailgw code will have a global "logging"
29 object.
30
31 It is intended that the logger API implementation here be the same as (or
32 close enough to) that of the standard Python library "logging" module.
33 '''
34 import sys, time, traceback
35
36 class BasicLogging:
37 LVL_DEBUG = 4
38 LVL_INFO = 3
39 LVL_WARNING= 2
40 LVL_ERROR= 1
41 LVL_NONE = 0
42 NAMES = {
43 LVL_DEBUG: 'DEBUG',
44 LVL_INFO: 'INFO',
45 LVL_WARNING: 'WARNING',
46 LVL_ERROR: 'ERROR',
47 }
48 level = LVL_INFO
49 loggers = {}
50 file = None
51 def getLogger(self, name):
52 return self.loggers.setdefault(name, BasicLogger(self.file, self.level))
53 def fileConfig(self, filename):
54 '''User is attempting to use a config file, but the basic logger
55 doesn't support that.'''
56 raise RuntimeError, "File-based logging configuration requires "\
57 "the logging package."
58 def setFile(self, file):
59 '''Set the file to log to. "file" is either an open file object or
60 a string filename to append entries to.
61 '''
62 if isinstance(file, type('')):
63 file = open(file, 'a')
64 self.file = file
65 def setLevel(self, level):
66 '''Set the maximum logging level. "level" is either a level number
67 (one of the LVL_ values) or a string level name.
68 '''
69 if isinstance(level, type('')):
70 for num, name in self.NAMES:
71 if name == level:
72 level = num
73 self.level = level
74
75 class BasicLogger:
76 '''Used when the standard Python library logging module isn't available.
77
78 Supports basic configuration through the tracker config file vars
79 LOGGING_LEVEL and LOGGING_FILENAME.'''
80 def __init__(self, file, level):
81 self.file = file
82 self.level = level
83
84 def setFile(self, file):
85 '''Set the file to log to. "file" is either an open file object or
86 a string filename to append entries to.
87 '''
88 if isinstance(file, type('')):
89 file = open(file, 'a')
90 self.file = file
91 def setLevel(self, level):
92 '''Set the maximum logging level. "level" is either a level number
93 (one of the LVL_ values) or a string level name.
94 '''
95 if isinstance(level, type('')):
96 for num, name in BasicLogging.NAMES:
97 if name == level:
98 level = num
99 self.level = level
100 def write(self, level, message):
101 message = '%s %s %s'%(time.strftime('%Y-%m-%d %H:%M:%D'),
102 BasicLogging.NAMES[level], message)
103 self._write(message)
104 def _write(self, text):
105 file = self.file or sys.stderr
106 file.write(text)
107 def debug(self, message):
108 if self.level < BasicLogging.LVL_DEBUG: return
109 self.write(BasicLogging.LVL_DEBUG, message)
110 def info(self, message):
111 if self.level < BasicLogging.LVL_INFO: return
112 self.write(BasicLogging.LVL_INFO, message)
113 def warning(self, message):
114 if self.level < BasicLogging.LVL_WARNING: return
115 self.write(BasicLogging.LVL_WARNING, message)
116 def error(self, message):
117 if self.level < BasicLogging.LVL_ERROR: return
118 self.write(BasicLogging.LVL_ERROR, message)
119 def exception(self, message):
120 if self.level < BasicLogging.LVL_ERROR: return
121 self.write(BasicLogging.LVL_ERROR, message)
122 self._write(traceback.format_exception(*(sys.exc_info())))
123

Roundup Issue Tracker: http://roundup-tracker.org/