mirror of https://github.com/r4sas/py-i2phosts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
14 years ago
|
import sys
|
||
14 years ago
|
import logging
|
||
14 years ago
|
import configobj
|
||
|
import validate
|
||
|
|
||
14 years ago
|
from logging import handlers
|
||
14 years ago
|
|
||
|
def get_logger(filename=None, log_level='debug'):
|
||
|
""" Prepare logger instance for our scripts """
|
||
|
|
||
14 years ago
|
# workaround for django
|
||
|
if hasattr(logging, "web_logger"):
|
||
|
return logging.web_logger
|
||
|
|
||
14 years ago
|
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:
|
||
14 years ago
|
handler = logging.handlers.WatchedFileHandler(filename)
|
||
14 years ago
|
else:
|
||
|
handler = logging.StreamHandler()
|
||
|
handler.setFormatter(formatter)
|
||
|
logger.addHandler(handler)
|
||
|
|
||
14 years ago
|
# workaround for django
|
||
|
logging.web_logger = logger
|
||
|
|
||
14 years ago
|
return logger
|
||
|
|
||
14 years ago
|
|
||
14 years ago
|
def validate_config(config):
|
||
|
""" Validate configobj config """
|
||
|
validator = validate.Validator()
|
||
|
results = config.validate(validator)
|
||
|
if results != True:
|
||
|
for (section_list, key, _) in configobj.flatten_errors(config, results):
|
||
|
if key is not None:
|
||
|
sys.stderr.write('The "%s" key in the section "%s" failed validation' %
|
||
|
(key, ', '.join(section_list)))
|
||
|
else:
|
||
|
sys.stderr.write('The following section was missing:%s ' %
|
||
|
', '.join(section_list))
|
||
|
sys.exit(1)
|