mirror of https://github.com/r4sas/py-i2phosts
Browse Source
And firstly write get_logger() function. It provides common mechanism for creating logger instances inside scripts.pull/1/head
Hidden Z
14 years ago
1 changed files with 26 additions and 0 deletions
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
import logging |
||||
|
||||
def get_logger(filename=None, log_level='debug'): |
||||
""" Prepare logger instance for our scripts """ |
||||
|
||||
LEVELS = { |
||||
'debug': logging.DEBUG, |
||||
'info': logging.INFO, |
||||
'warning': logging.WARNING, |
||||
'error': logging.ERROR, |
||||
'critical': logging.CRITICAL |
||||
} |
||||
level = LEVELS.get(log_level, logging.NOTSET) |
||||
format = '%(asctime)s %(module)s:%(lineno)d[%(process)d] %(levelname)s: %(message)s' |
||||
formatter = logging.Formatter(format) |
||||
logger = logging.getLogger(__name__) |
||||
logger.setLevel(level) |
||||
if filename: |
||||
handler = logging.FileHandler(filename) |
||||
else: |
||||
handler = logging.StreamHandler() |
||||
handler.setFormatter(formatter) |
||||
logger.addHandler(handler) |
||||
|
||||
return logger |
||||
|
Loading…
Reference in new issue