| Home | Trees | Indices | Help |
|
|---|
|
|
1 # Licensed under the MIT license
2 # http://opensource.org/licenses/mit-license.php or see LICENSE file.
3 # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org>
4
5 """ Log module with colored logging feature. Common usage of this module can
6 be only importing it and calling one of the available functions: debug,
7 warning, info, critical, error.
8 """
9
10 import os
11 import logging
12
13 from logging import getLogger
14
15 from brisa import __enable_logging__
16 from brisa.core import config
17
18
19 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
20 RESET_SEQ = '\033[0m'
21 COLOR_SEQ = '\033[1;%dm'
22 BOLD_SEQ = '\033[1m'
23
24 COLORS = {
25 'WARNING': YELLOW,
26 'INFO': WHITE,
27 'DEBUG': BLUE,
28 'CRITICAL': YELLOW,
29 'ERROR': RED}
30
31
33 """ Method to format the pattern in which the log messages will be
34 displayed.
35
36 @param message: message log to be displayed
37 @param use_color: Flag to indicates the use of colors or not
38
39 @type message: str
40 @type use_color: boolean
41
42 @return: the new formatted message
43 @rtype: str
44 """
45 if use_color:
46 message = message.replace('$RESET', RESET_SEQ).replace('$BOLD',
47 BOLD_SEQ)
48 else:
49 message = message.replace('$RESET', '').replace('$BOLD', '')
50 return message
51
52
54 """ ColoredFormatter class, which wrappers logging.Formatter. """
55
57 """ Constructor of the ColoredFormatter class.
58
59 @param msg: message to be displayed
60 @param use_color: Flag to indicate the use of color or not
61
62 @type msg: str
63 @type use_color: boolean
64 """
65 logging.Formatter.__init__(self, msg)
66 self.use_color = use_color
67
69 """ format method to the ColoredFormatter class that organizes the log
70 message.
71
72 @parameter record: information about the logger
73 @type record: Instance of Logger, either its RootLogger or not
74 """
75 levelname = record.levelname
76 if self.use_color and levelname in COLORS:
77 levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname\
78 + RESET_SEQ
79 record.levelname = levelname_color
80 return logging.Formatter.format(self, record)
81
82
84
85 FORMAT = '%(created)f $BOLD%(levelname)s$RESET $BOLD%(module)s:%(lineno)d'\
86 ':%(funcName)s()$RESET %(message)s'
87 COLOR_FORMAT = formatter_message(FORMAT, True)
88
90 """ Constructor for the ColoredLogger class.
91
92 @param name: name of the Logger.
93 @type name: str
94 """
95 global level
96 logging.Logger.__init__(self, name, level)
97 color_formatter = ColoredFormatter(self.COLOR_FORMAT)
98 console = logging.StreamHandler()
99 console.setFormatter(color_formatter)
100 self.addHandler(console)
101
102
103 log_dict = {'WARNING': logging.WARNING,
104 'DEBUG': logging.DEBUG,
105 'INFO': logging.INFO,
106 'CRITICAL': logging.CRITICAL,
107 'ERROR': logging.ERROR}
108
109
111 """ Method to setup the logging options. """
112 global debug, info, warning, critical, error, root_logger, set_level,\
113 setLevel, filename, level
114
115 level = log_dict.get(config.get_parameter('brisa', 'logging'),
116 logging.DEBUG)
117 filename = config.get_parameter('brisa', 'logging_output')
118
119 if filename == 'file':
120 filename = os.path.join(config.brisa_home, 'brisa.log')
121 logging.basicConfig(level=level, filename=filename,
122 format='%(created)f %(levelname)s %(module)s:'\
123 '%(lineno)d:%(funcName)s() %(message)s')
124 root_logger = logging.getLogger('RootLogger')
125 else:
126 logging.setLoggerClass(ColoredLogger)
127 root_logger = getLogger('RootLogger')
128 root_logger.setLevel(level)
129
130 def set_level(level):
131 """ Real implementation of the set level function. """
132 root_logger.setLevel(log_dict.get(level))
133
134 def setLevel(level):
135 """ Method to set the log level. """
136 set_level(level)
137
138
139 root_logger = getLogger()
140
141 if __enable_logging__:
142 setup_logging()
143
144 debug = root_logger.debug
145 info = root_logger.info
146 warning = root_logger.warning
147 critical = root_logger.critical
148 error = root_logger.error
149
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Tue Jun 9 22:24:59 2009 | http://epydoc.sourceforge.net |