From c0a85c932bdc5466823895ae6c481f2ecb74e57a Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Wed, 4 Feb 2009 18:27:00 +0100 Subject: [PATCH] [FIX] unicodify the logger lp bug: https://launchpad.net/bugs/325259 fixed bzr revid: christophe@tinyerp.com-20090204172700-3ivskfwcofpma74t --- bin/netsvc.py | 22 ++++++++++++++++++++-- bin/tools/translate.py | 10 +++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/bin/netsvc.py b/bin/netsvc.py index b9e4a589fab..656fde1ef22 100644 --- a/bin/netsvc.py +++ b/bin/netsvc.py @@ -87,12 +87,30 @@ LOG_CRITICAL = 'critical' # add new log level below DEBUG logging.DEBUG_RPC = logging.DEBUG - 1 +class UnicodeFormatter(logging.Formatter): + def __init__(self, fmt=None, datefmt=None): + logging.Formatter.__init__(self, fmt, datefmt) + self._fmt = tools.ustr(self._fmt) + if self.datefmt is not None: + self.datefmt = tools.ustr(self.datefmt) + + def formatTime(self, *args, **kwargs): + return tools.ustr(logging.Formatter.formatTime(self, *args, **kwargs)) + + def formatException(self, *args, **kwargs): + return tools.ustr(logging.Formatter.formatException(self, *args, **kwargs)) + + def format(self, *args, **kwargs): + return tools.ustr(logging.Formatter.format(self, *args, **kwargs)) + def init_logger(): import os + from tools.translate import resetlocale + resetlocale() logger = logging.getLogger() # create a format for log messages and dates - formatter = logging.Formatter('[%(asctime)s] %(levelname)s:%(name)s:%(message)s', '%a %b %d %Y %H:%M:%S') + formatter = UnicodeFormatter('[%(asctime)s] %(levelname)s:%(name)s:%(message)s', '%a %b %d %Y %H:%M:%S') logging_to_stdout = False if tools.config['syslog']: @@ -103,7 +121,7 @@ def init_logger(): release.version)) else: handler = logging.handlers.SysLogHandler('/dev/log') - formatter = logging.Formatter("%s %s" % (release.description, release.version) + ':%(levelname)s:%(name)s:%(message)s') + formatter = UnicodeFormatter("%s %s" % (release.description, release.version) + ':%(levelname)s:%(name)s:%(message)s') elif tools.config['logfile']: # LogFile Handler diff --git a/bin/tools/translate.py b/bin/tools/translate.py index 0f84bdc9b45..2505909e81f 100644 --- a/bin/tools/translate.py +++ b/bin/tools/translate.py @@ -564,9 +564,7 @@ def trans_load_data(db_name, fileobj, fileformat, lang, strict=False, lang_name= try: lang_obj.create(cr, uid, lang_info) finally: - # locale.resetlocale is bugged with some locales. - # we need to normalize the result of locale.getdefaultlocale() - locale.setlocale(locale.LC_ALL, locale.normalize(locale._build_localename(locale.getdefaultlocale()))) + resetlocale() # now, the serious things: we read the language file @@ -660,5 +658,11 @@ def trans_load_data(db_name, fileobj, fileformat, lang, strict=False, lang_name= logger.notifyChannel("i18n", netsvc.LOG_ERROR, "couldn't read translation file %s" % (filename,)) +def resetlocale(): + # locale.resetlocale is bugged with some locales. + # we need to normalize the result of locale.getdefaultlocale() + locale.setlocale(locale.LC_ALL, locale.normalize(locale._build_localename(locale.getdefaultlocale()))) + + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: