Package common :: Module logging_helpers
[hide private]
[frames] | no frames]

Source Code for Module common.logging_helpers

  1  # -*- coding:utf-8 -*- 
  2  ## src/common/logging_helpers.py 
  3  ## 
  4  ## Copyright (C) 2009 Bruno Tarquini <btarquini AT gmail.com> 
  5  ## 
  6  ## This file is part of Gajim. 
  7  ## 
  8  ## Gajim is free software; you can redistribute it and/or modify 
  9  ## it under the terms of the GNU General Public License as published 
 10  ## by the Free Software Foundation; version 3 only. 
 11  ## 
 12  ## Gajim is distributed in the hope that it will be useful, 
 13  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 15  ## GNU General Public License for more details. 
 16  ## 
 17  ## You should have received a copy of the GNU General Public License 
 18  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
 19  ## 
 20   
 21  import logging 
 22  import i18n 
 23   
24 -def parseLogLevel(arg):
25 """ 26 Eiter numeric value or level name from logging module 27 """ 28 if arg.isdigit(): 29 return int(arg) 30 elif arg.isupper(): 31 return getattr(logging, arg) 32 else: 33 raise ValueError(_('%s is not a valid loglevel'), repr(arg))
34
35 -def parseLogTarget(arg):
36 """ 37 [gajim.]c.x.y -> gajim.c.x.y 38 .other_logger -> other_logger 39 <None> -> gajim 40 """ 41 arg = arg.lower() 42 if not arg: 43 return 'gajim' 44 elif arg.startswith('.'): 45 return arg[1:] 46 elif arg.startswith('gajim'): 47 return arg 48 else: 49 return 'gajim.' + arg
50
51 -def parseAndSetLogLevels(arg):
52 """ 53 [=]LOGLEVEL -> gajim=LOGLEVEL 54 gajim=LOGLEVEL -> gajim=LOGLEVEL 55 .other=10 -> other=10 56 .=10 -> <nothing> 57 c.x.y=c.z=20 -> gajim.c.x.y=20 58 gajim.c.z=20 59 gajim=10,c.x=20 -> gajim=10 60 gajim.c.x=20 61 """ 62 for directive in arg.split(','): 63 directive = directive.strip() 64 if not directive: 65 continue 66 if '=' not in directive: 67 directive = '=' + directive 68 targets, level = directive.rsplit('=', 1) 69 level = parseLogLevel(level.strip()) 70 for target in targets.split('='): 71 target = parseLogTarget(target.strip()) 72 if target: 73 logging.getLogger(target).setLevel(level) 74 print "Logger %s level set to %d" % (target, level)
75 76
77 -class colors:
78 NONE = chr(27) + "[0m" 79 BLACk = chr(27) + "[30m" 80 RED = chr(27) + "[31m" 81 GREEN = chr(27) + "[32m" 82 BROWN = chr(27) + "[33m" 83 BLUE = chr(27) + "[34m" 84 MAGENTA = chr(27) + "[35m" 85 CYAN = chr(27) + "[36m" 86 LIGHT_GRAY = chr(27) + "[37m" 87 DARK_GRAY = chr(27) + "[30;1m" 88 BRIGHT_RED = chr(27) + "[31;1m" 89 BRIGHT_GREEN = chr(27) + "[32;1m" 90 YELLOW = chr(27) + "[33;1m" 91 BRIGHT_BLUE = chr(27) + "[34;1m" 92 PURPLE = chr(27) + "[35;1m" 93 BRIGHT_CYAN = chr(27) + "[36;1m" 94 WHITE = chr(27) + "[37;1m"
95
96 -def colorize(text, color):
97 return color + text + colors.NONE
98
99 -class FancyFormatter(logging.Formatter):
100 """ 101 An eye-candy formatter with colors 102 """ 103 colors_mapping = { 104 'DEBUG': colors.BLUE, 105 'INFO': colors.GREEN, 106 'WARNING': colors.BROWN, 107 'ERROR': colors.RED, 108 'CRITICAL': colors.BRIGHT_RED, 109 } 110
111 - def __init__(self, fmt, datefmt=None, use_color=False):
112 logging.Formatter.__init__(self, fmt, datefmt) 113 self.use_color = use_color
114
115 - def formatTime(self, record, datefmt=None):
116 f = logging.Formatter.formatTime(self, record, datefmt) 117 if self.use_color: 118 f = colorize(f, colors.DARK_GRAY) 119 return f
120
121 - def format(self, record):
122 level = record.levelname 123 record.levelname = '(%s)' % level[0] 124 125 if self.use_color: 126 c = FancyFormatter.colors_mapping.get(level, '') 127 record.levelname = colorize(record.levelname, c) 128 record.name = colorize(record.name, colors.CYAN) 129 else: 130 record.name += ':' 131 132 return logging.Formatter.format(self, record)
133 134
135 -def init(use_color=False):
136 """ 137 Iinitialize the logging system 138 """ 139 consoleloghandler = logging.StreamHandler() 140 consoleloghandler.setFormatter( 141 FancyFormatter( 142 '%(asctime)s %(levelname)s %(name)s %(message)s', 143 '%H:%M:%S', 144 use_color 145 ) 146 ) 147 148 # fake the root logger so we have 'gajim' root name instead of 'root' 149 root_log = logging.getLogger('gajim') 150 root_log.setLevel(logging.WARNING) 151 root_log.addHandler(consoleloghandler) 152 root_log.propagate = False
153
154 -def set_loglevels(loglevels_string):
155 parseAndSetLogLevels(loglevels_string)
156
157 -def set_verbose():
158 parseAndSetLogLevels('gajim=1')
159
160 -def set_quiet():
161 parseAndSetLogLevels('gajim=CRITICAL')
162 163 164 # tests 165 if __name__ == '__main__': 166 init(use_color=True) 167 168 set_loglevels('gajim.c=DEBUG,INFO') 169 170 log = logging.getLogger('gajim') 171 log.debug('debug') 172 log.info('info') 173 log.warn('warn') 174 log.error('error') 175 log.critical('critical') 176 177 log = logging.getLogger('gajim.c.x.dispatcher') 178 log.debug('debug') 179 log.info('info') 180 log.warn('warn') 181 log.error('error') 182 log.critical('critical') 183