Removed a bug (debug messages was not showed) and improved readability

This commit is contained in:
Skami18 2011-04-25 12:13:44 +02:00 committed by Nicolas Duhamel
commit 8de525fa28

View file

@ -1,46 +1,51 @@
import logging from logging import *
import sys import sys
import os import os
global ANSI global ANSI
ANSI = { ANSI = {
'gray' : lambda(text) : '\033[1;30m' + unicode(text) + '\033[1;m', 'gray' : lambda(text) : u'\033[1;30m' + unicode(text) + u'\033[1;m',
'red' : lambda(text) : '\033[1;31m' + unicode(text) + '\033[1;m', 'red' : lambda(text) : u'\033[1;31m' + unicode(text) + u'\033[1;m',
'green' : lambda(text) : '\033[1;32m' + unicode(text) + '\033[1;m', 'green' : lambda(text) : u'\033[1;32m' + unicode(text) + u'\033[1;m',
'yellow' : lambda(text) : '\033[1;33m' + unicode(text) + '\033[1;m', 'yellow' : lambda(text) : u'\033[1;33m' + unicode(text) + u'\033[1;m',
'blue' : lambda(text) : '\033[1;34m' + unicode(text) + '\033[1;m', 'blue' : lambda(text) : u'\033[1;34m' + unicode(text) + u'\033[1;m',
'magenta' : lambda(text) : '\033[1;35m' + unicode(text) + '\033[1;m', 'magenta' : lambda(text) : u'\033[1;35m' + unicode(text) + u'\033[1;m',
'cyan' : lambda(text) : '\033[1;36m' + unicode(text) + '\033[1;m', 'cyan' : lambda(text) : u'\033[1;36m' + unicode(text) + u'\033[1;m',
'white' : lambda(text) : '\033[1;37m' + unicode(text) + '\033[1;m', 'white' : lambda(text) : u'\033[1;37m' + unicode(text) + u'\033[1;m',
'crimson' : lambda(text) : '\033[1;38m' + unicode(text) + '\033[1;m', 'bgred' : lambda(text) : u'\033[1;41m' + unicode(text) + u'\033[1;m',
'bgred' : lambda(text) : '\033[1;41m' + unicode(text) + '\033[1;m', 'bggreen' : lambda(text) : u'\033[1;42m' + unicode(text) + u'\033[1;m',
'bggreen' : lambda(text) : '\033[1;42m' + unicode(text) + '\033[1;m', 'bgbrown' : lambda(text) : u'\033[1;43m' + unicode(text) + u'\033[1;m',
'bgbrown' : lambda(text) : '\033[1;43m' + unicode(text) + '\033[1;m', 'bgblue' : lambda(text) : u'\033[1;44m' + unicode(text) + u'\033[1;m',
'bgblue' : lambda(text) : '\033[1;44m' + unicode(text) + '\033[1;m', 'bgmagenta' : lambda(text) : u'\033[1;45m' + unicode(text) + u'\033[1;m',
'bgmagenta' : lambda(text) : '\033[1;45m' + unicode(text) + '\033[1;m', 'bgcyan' : lambda(text) : u'\033[1;46m' + unicode(text) + u'\033[1;m',
'bgcyan' : lambda(text) : '\033[1;46m' + unicode(text) + '\033[1;m', 'bggray' : lambda(text) : u'\033[1;47m' + unicode(text) + u'\033[1;m',
'bggray' : lambda(text) : '\033[1;47m' + unicode(text) + '\033[1;m', 'bgyellow' : lambda(text) : u'\033[1;43m' + unicode(text) + u'\033[1;m',
'bgcrimson' : lambda(text) : '\033[1;48m' + unicode(text) + '\033[1;m' 'bggrey' : lambda(text) : u'\033[1;100m' + unicode(text) + u'\033[1;m'
} }
class ANSIFormatter(logging.Formatter):
class ANSIFormatter(Formatter):
""" """
Convert a `logging.LogReport' object into colored text, using ANSI escape sequences. Convert a `logging.LogReport' object into colored text, using ANSI escape sequences.
""" """
## colors: ## colors:
def format(self, record): def format(self, record):
if not record.levelname or record.levelname is 'INFO': if record.levelname is 'INFO':
return ANSI['white'](record.msg) return ANSI['cyan']('-> ') + unicode(record.msg)
elif record.levelname is 'WARNING': elif record.levelname is 'WARNING':
return ANSI['yellow'](record.levelname) + ': ' + record.msg return ANSI['yellow'](record.levelname) + ': ' + unicode(record.msg)
elif record.levelname is 'ERROR': elif record.levelname is 'ERROR':
return ANSI['red'](record.levelname) + ': ' + record.msg return ANSI['red'](record.levelname) + ': ' + unicode(record.msg)
elif record.levelname is 'CRITICAL': elif record.levelname is 'CRITICAL':
return ANSI['bgred'](record.levelname) + ': ' + record.msg return ANSI['bgred'](record.levelname) + ': ' + unicode(record.msg)
elif record.levelname is 'DEBUG':
return ANSI['bggrey'](record.levelname) + ': ' + unicode(record.msg)
else:
return ANSI['white'](record.levelname) + ': ' + unicode(record.msg)
class TextFormatter(logging.Formatter): class TextFormatter(Formatter):
""" """
Convert a `logging.LogReport' object into text. Convert a `logging.LogReport' object into text.
""" """
@ -52,7 +57,7 @@ class TextFormatter(logging.Formatter):
return record.levelname + ': ' + record.msg return record.levelname + ': ' + record.msg
class Formatter(object): class DummyFormatter(object):
""" """
A dummy class. A dummy class.
Return an instance of the appropriate formatter (ANSIFormatter if sys.stdout.isatty() is True, else TextFormatter) Return an instance of the appropriate formatter (ANSIFormatter if sys.stdout.isatty() is True, else TextFormatter)
@ -66,21 +71,10 @@ class Formatter(object):
# shortcuts
debug, info, warn, error, critical = (logging.debug,
logging.info,
logging.warn,
logging.error,
logging.critical)
DEBUG, INFO, WARN, ERROR, CRITICAL = (logging.DEBUG,
logging.INFO,
logging.WARN,
logging.ERROR,
logging.CRITICAL)
def init(level=None, logger=logging.getLogger(), handler=logging.StreamHandler()): def init(level=None, logger=getLogger(), handler=StreamHandler()):
fmt = Formatter() fmt = DummyFormatter()
handler.setFormatter(fmt) handler.setFormatter(fmt)
logger.addHandler(handler) logger.addHandler(handler)
if level: if level:
@ -88,11 +82,23 @@ def init(level=None, logger=logging.getLogger(), handler=logging.StreamHandler()
if __name__ == '__main__': if __name__ == '__main__':
init() init(level=DEBUG)
logging.basicConfig(filename='example.log',level=logging.DEBUG) debug('debug')
logging.debug('Logging test') info('info')
logging.info('info') warning('warning')
logging.warning('warning') error('error')
logging.error('error') critical('critical')
logging.critical('critical')
__all__ = [
"debug",
"info",
"warn",
"error",
"critical",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"CRITICAL"
]