Commit ef692021 authored by Julien Muchembled's avatar Julien Muchembled

logs: make sure every line is prefixed for easy merge of several log files

Merging several log files becomes as easy as:

$ sort -sk 1,2 *.log |less

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2702 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 38bdc708
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
import neo.lib.python import neo.lib.python
import logging as logging_std import logging as logging_std
PREFIX = '%(asctime)s %(levelname)-9s %(name)-10s' FMT = ('%(asctime)s %(levelname)-9s %(name)-10s'
SUFFIX = ' [%(module)14s:%(lineno)3d] %(message)s' ' [%(module)14s:%(lineno)3d] \n%(message)s')
class Formatter(logging_std.Formatter): class Formatter(logging_std.Formatter):
...@@ -27,13 +27,17 @@ class Formatter(logging_std.Formatter): ...@@ -27,13 +27,17 @@ class Formatter(logging_std.Formatter):
return logging_std.Formatter.formatTime(self, record, return logging_std.Formatter.formatTime(self, record,
'%Y-%m-%d %H:%M:%S') + '.%04d' % (record.msecs * 10) '%Y-%m-%d %H:%M:%S') + '.%04d' % (record.msecs * 10)
def format(self, record):
lines = iter(logging_std.Formatter.format(self, record).splitlines())
prefix = lines.next()
return '\n'.join(prefix + line for line in lines)
def setupLog(name='NEO', filename=None, verbose=False): def setupLog(name='NEO', filename=None, verbose=False):
global logging global logging
if verbose: if verbose:
level = logging_std.DEBUG level = logging_std.DEBUG
else: else:
level = logging_std.INFO level = logging_std.INFO
fmt = PREFIX + SUFFIX
logging = logging_std.getLogger(name.upper()) logging = logging_std.getLogger(name.upper())
for handler in logging.handlers[:]: for handler in logging.handlers[:]:
logging.removeHandler(handler) logging.removeHandler(handler)
...@@ -41,7 +45,7 @@ def setupLog(name='NEO', filename=None, verbose=False): ...@@ -41,7 +45,7 @@ def setupLog(name='NEO', filename=None, verbose=False):
handler = logging_std.StreamHandler() handler = logging_std.StreamHandler()
else: else:
handler = logging_std.FileHandler(filename) handler = logging_std.FileHandler(filename)
handler.setFormatter(Formatter(fmt)) handler.setFormatter(Formatter(FMT))
logging.setLevel(level) logging.setLevel(level)
logging.addHandler(handler) logging.addHandler(handler)
logging.propagate = 0 logging.propagate = 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment