Revision: 6644 Author: nicdumz Date: 2009-04-21 06:33:00 +0000 (Tue, 21 Apr 2009)
Log Message: ----------- Adding an exception formatter to avoid Unicode/String mix ups in the underlying logging module.
Modified Paths: -------------- branches/rewrite/pywikibot/bot.py
Modified: branches/rewrite/pywikibot/bot.py =================================================================== --- branches/rewrite/pywikibot/bot.py 2009-04-20 17:37:31 UTC (rev 6643) +++ branches/rewrite/pywikibot/bot.py 2009-04-21 06:33:00 UTC (rev 6644) @@ -63,7 +63,25 @@ text = logging.handlers.RotatingFileHandler.format(self, record) return text.rstrip("\r\n")
+class LoggingFormatter(logging.Formatter): + def formatException(self, ei): + """ + Make sure that the exception trace is converted to unicode: + * our pywikibot.Error traces are encoded in our + console encoding, which is needed for plainly printing them. + * but when it comes to logging using logging.exception, + the Python logging module will try to use these traces, + and it will fail if they are console encoded strings.
+ Formatter.formatException also strips the trailing \n, which we need. + """ + strExc = logging.Formatter.formatException(self, ei) + + if isinstance(strExc, str): + return strExc.decode(config.console_encoding) + '\n' + else: + return strExc + '\n' + def output(text, decoder=None, newline=True, toStdout=False, level=INFO): """Output a message to the user via the userinterface.
@@ -207,7 +225,7 @@ else: default_handler.setLevel(INFO) default_handler.addFilter(MaxLevelFilter(INPUT)) - default_handler.setFormatter(logging.Formatter(fmt="%(message)s")) + default_handler.setFormatter(LoggingFormatter(fmt="%(message)s")) root_logger.addHandler(default_handler)
# if user has enabled file logging, configure file handler @@ -220,7 +238,7 @@ filename=logfile, maxBytes=2 << 20, backupCount=5)
file_handler.setLevel(DEBUG) - form = logging.Formatter( + form = LoggingFormatter( fmt="%(asctime)s %(filename)18s, %(lineno)d: " "%(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S" @@ -236,14 +254,14 @@ output_handler = TerminalHandler(strm=sys.stdout) output_handler.setLevel(STDOUT) output_handler.addFilter(MaxLevelFilter(STDOUT)) - output_handler.setFormatter(logging.Formatter(fmt="%(message)s")) + output_handler.setFormatter(LoggingFormatter(fmt="%(message)s")) root_logger.addHandler(output_handler)
# handler for levels WARNING and higher warning_handler = TerminalHandler(strm=sys.stderr) warning_handler.setLevel(logging.WARNING) warning_handler.setFormatter( - logging.Formatter(fmt="%(levelname)s: %(message)s")) + LoggingFormatter(fmt="%(levelname)s: %(message)s")) root_logger.addHandler(warning_handler)