summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2010-08-25 11:16:49 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2010-08-25 11:16:49 +0200
commit5760d13377818f1687f174c46732703ba681ab73 (patch)
tree635a1d5cb5b5b82eccfdac8e4df0f4dc510a121d
parent052a1069f0778792c8c142549a4e6780024f1c8d (diff)
downloadlogilab-common-5760d13377818f1687f174c46732703ba681ab73.tar.gz
init_log now takes optional handler
-rw-r--r--logging_ext.py55
1 files changed, 28 insertions, 27 deletions
diff --git a/logging_ext.py b/logging_ext.py
index cfefc4c..171632b 100644
--- a/logging_ext.py
+++ b/logging_ext.py
@@ -99,39 +99,40 @@ LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
def init_log(debug=False, syslog=False, logthreshold=None, logfile=None,
logformat=LOG_FORMAT, logdateformat=LOG_DATE_FORMAT, fmt=None,
- rotation_parameters=None):
+ rotation_parameters=None, handler=None):
"""init the log service"""
if os.environ.get('APYCOT_ROOT'):
logthreshold = logging.CRITICAL
# redirect logs to stdout to avoid apycot output parsing failure
- handler = logging.StreamHandler(sys.stdout)
- else:
- if debug:
- handler = logging.StreamHandler()
- elif logfile is None:
- if syslog:
- from logging import handlers
- handler = handlers.SysLogHandler()
- else:
- handler = logging.StreamHandler()
- else:
- try:
- if rotation_parameters is None:
- handler = logging.FileHandler(logfile)
- else:
- from logging.handlers import TimedRotatingFileHandler
- handler = TimedRotatingFileHandler(logfile,
- **rotation_parameters)
- except IOError:
- handler = logging.StreamHandler()
- if logthreshold is None:
+ if handler is None:
+ handler = logging.StreamHandler(sys.stdout)
+ elif handler is None:
if debug:
- logthreshold = logging.DEBUG
+ handler = logging.StreamHandler()
+ elif logfile is None:
+ if syslog:
+ from logging import handlers
+ handler = handlers.SysLogHandler()
+ else:
+ handler = logging.StreamHandler()
else:
- logthreshold = logging.ERROR
- elif isinstance(logthreshold, basestring):
- logthreshold = getattr(logging, THRESHOLD_MAP.get(logthreshold,
- logthreshold))
+ try:
+ if rotation_parameters is None:
+ handler = logging.FileHandler(logfile)
+ else:
+ from logging.handlers import TimedRotatingFileHandler
+ handler = TimedRotatingFileHandler(
+ logfile, **rotation_parameters)
+ except IOError:
+ handler = logging.StreamHandler()
+ if logthreshold is None:
+ if debug:
+ logthreshold = logging.DEBUG
+ else:
+ logthreshold = logging.ERROR
+ elif isinstance(logthreshold, basestring):
+ logthreshold = getattr(logging, THRESHOLD_MAP.get(logthreshold,
+ logthreshold))
# configure the root logger
logger = logging.getLogger()
logger.setLevel(logthreshold)