1
0
Fork 0
forked from github/pelican
pelican-theme/pelican/log.py

115 lines
2.9 KiB
Python
Raw Normal View History

import os
import sys
2011-05-06 19:23:37 +02:00
from logging import CRITICAL, ERROR, WARN, INFO, DEBUG
from logging import critical, error, info, warning, warn, debug
from logging import Formatter, getLogger, StreamHandler
RESET_TERM = u'\033[0;m'
def term_color(code):
return lambda text: code + unicode(text) + RESET_TERM
COLOR_CODES = {
'gray': u'\033[1;30m',
'red': u'\033[1;31m',
'green': u'\033[1;32m',
'yellow': u'\033[1;33m',
'blue': u'\033[1;34m',
'magenta': u'\033[1;35m',
'cyan': u'\033[1;36m',
'white': u'\033[1;37m',
'bgred': u'\033[1;41m',
'bggreen': u'\033[1;42m',
'bgbrown': u'\033[1;43m',
'bgblue': u'\033[1;44m',
'bgmagenta': u'\033[1;45m',
'bgcyan': u'\033[1;46m',
'bggray': u'\033[1;47m',
'bgyellow': u'\033[1;43m',
'bggrey': u'\033[1;100m',
2011-04-18 20:14:07 +02:00
}
ANSI = dict((col, term_color(code)) for col, code in COLOR_CODES.items())
class ANSIFormatter(Formatter):
2011-04-18 20:14:07 +02:00
"""
Convert a `logging.LogReport' object into colored text, using ANSI escape sequences.
"""
## colors:
def format(self, record):
if record.levelname is 'INFO':
return ANSI['cyan']('-> ') + unicode(record.msg)
elif record.levelname is 'WARNING':
return ANSI['yellow'](record.levelname) + ': ' + unicode(record.msg)
elif record.levelname is 'ERROR':
return ANSI['red'](record.levelname) + ': ' + unicode(record.msg)
elif record.levelname is 'CRITICAL':
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)
2011-04-18 20:14:07 +02:00
class TextFormatter(Formatter):
2011-04-18 20:14:07 +02:00
"""
Convert a `logging.LogReport' object into text.
"""
def format(self, record):
if not record.levelname or record.levelname is 'INFO':
return record.msg
else:
return record.levelname + ': ' + record.msg
class DummyFormatter(object):
2011-04-18 20:14:07 +02:00
"""
A dummy class.
Return an instance of the appropriate formatter (ANSIFormatter if
sys.stdout.isatty() is True, else TextFormatter)
2011-04-18 20:14:07 +02:00
"""
def __new__(cls, *args, **kwargs):
if os.isatty(sys.stdout.fileno())\
and not sys.platform.startswith('win'):
2011-04-18 20:14:07 +02:00
return ANSIFormatter(*args, **kwargs)
else:
return TextFormatter( *args, **kwargs)
def init(level=None, logger=getLogger(), handler=StreamHandler()):
fmt = DummyFormatter()
handler.setFormatter(fmt)
logger.addHandler(handler)
2011-04-20 14:44:25 +02:00
if level:
logger.setLevel(level)
2011-04-18 20:14:07 +02:00
if __name__ == '__main__':
init(level=DEBUG)
debug('debug')
info('info')
warning('warning')
error('error')
critical('critical')
__all__ = [
"debug",
"info",
"warn",
"warning",
"error",
"critical",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"CRITICAL"
]