mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-23 11:12:47 +01:00
Re-added colored stdout logging.
This commit is contained in:
parent
f24011559b
commit
b6dd266549
@ -487,7 +487,7 @@ def main():
|
||||
logged to stderr. By default, though, we turn the colors off for
|
||||
Windows machines and leave it on for *nix machines.""")
|
||||
if yn('Would you like to turn this colorization off?') == 'y':
|
||||
confVariables['colorterm'] = False
|
||||
confVariables['colorizedStdoutLogging'] = False
|
||||
|
||||
## # conf.minimumLogPriority
|
||||
## myPrint("""Your bot can handle debug messages at several priorities,
|
||||
|
@ -33,6 +33,7 @@ __revision__ = "$Id$"
|
||||
|
||||
import fix
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
import sets
|
||||
@ -72,6 +73,12 @@ minimumLogPriority = logging.INFO
|
||||
###
|
||||
stdoutLogging = True
|
||||
|
||||
###
|
||||
# colorizedStdoutLogging: Determines whether or not the bot logs colored logs
|
||||
# to stdout.
|
||||
###
|
||||
colorizedStdoutLogging = True
|
||||
|
||||
###
|
||||
# logTimestampFormat: A format string defining how timestamps should be. Check
|
||||
# the Python library reference for the "time" module to see
|
||||
@ -327,5 +334,7 @@ types = {
|
||||
'showOnlySyntax': mybool,
|
||||
}
|
||||
|
||||
if os.name == 'nt':
|
||||
colorizedStdoutLogging = False
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||
|
36
src/log.py
36
src/log.py
@ -38,6 +38,7 @@ import sys
|
||||
import atexit
|
||||
import logging
|
||||
|
||||
import ansi
|
||||
import conf
|
||||
|
||||
deadlyExceptions = [KeyboardInterrupt, SystemExit]
|
||||
@ -63,6 +64,7 @@ class Formatter(logging.Formatter):
|
||||
### TODO: formatException should use cgitb.
|
||||
return logging.Formatter.formatException(self, (E, e, tb))
|
||||
|
||||
|
||||
class DailyRotatingHandler(logging.FileHandler):
|
||||
def __init__(self, *args):
|
||||
self.lastRollover = time.localtime()
|
||||
@ -81,7 +83,36 @@ class DailyRotatingHandler(logging.FileHandler):
|
||||
os.rename(self.baseFilename, '%s.%s' % (self.baseFilename, extension))
|
||||
self.stream = file(self.baseFilename, 'w')
|
||||
|
||||
# This is available publically.
|
||||
|
||||
class ColorizedFormatter(Formatter):
|
||||
def formatException(self, (E, e, tb)):
|
||||
if conf.colorizedStdoutLogging:
|
||||
return ''.join([ansi.BOLD, ansi.RED,
|
||||
Formatter.formatException(self, (E, e, tb)),
|
||||
ansi.RESET])
|
||||
else:
|
||||
return Formatter.formatException(self, (E, e, tb))
|
||||
|
||||
def format(self, record, *args, **kwargs):
|
||||
if conf.colorizedStdoutLogging:
|
||||
color = ''
|
||||
if record.levelno == logging.CRITICAL:
|
||||
color = ansi.WHITE + ansi.BOLD
|
||||
elif record.levelno == logging.ERROR:
|
||||
color = ansi.RED
|
||||
elif record.levelno == logging.WARNING:
|
||||
color = ansi.YELLOW
|
||||
elif record.levelno == logging.VERBOSE:
|
||||
color = ansi.BLUE
|
||||
elif record.levelno == logging.PRINTF:
|
||||
color = ansi.CYAN
|
||||
return ''.join([color,
|
||||
Formatter.format(self, record, *args, **kwargs),
|
||||
ansi.RESET])
|
||||
else:
|
||||
return Formatter.format(self, record, *args, **kwargs)
|
||||
|
||||
# These are public.
|
||||
formatter = Formatter('%(levelname)s %(asctime)s %(message)s')
|
||||
pluginFormatter = Formatter('%(levelname)s %(asctime)s %(name)s %(message)s')
|
||||
|
||||
@ -95,6 +126,9 @@ _logger.setLevel(-1)
|
||||
|
||||
if conf.stdoutLogging:
|
||||
_stdoutHandler = logging.StreamHandler(sys.stdout)
|
||||
_formatString = '%(name)s: %(levelname)s %(asctime)s %(message)s'
|
||||
_stdoutFormatter = ColorizedFormatter(_formatString)
|
||||
_stdoutHandler.setFormatter(_stdoutFormatter)
|
||||
_stdoutHandler.setLevel(conf.minimumLogPriority)
|
||||
_logger.addHandler(_stdoutHandler)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user