def init(dsn=None): """Redirect Scrapy log messages to standard Python logger""" observer = log.PythonLoggingObserver() observer.start() dict_config = settings.get("LOGGING") if dict_config is not None: assert isinstance(dict_config, dict) logging.dictConfig(dict_config) handler = SentryHandler(dsn) setup_logging(handler)
def loadFromConfig(config): if sys.version_info[1] > 6: logging.dictConfig(config) return global _defaults, _config if not config: return False _config = config _defaults['level'] = config.get('default_level', 'info') _defaults['logger'] = config.get('default_logger', 'root') _defaults['handler'] = config.get('default_handler', 'console') _defaults['formatter'] = config.get('default_formatter', 'generic') root_logger = logging.getLogger('root') root_conf = config.get('root') level = LEVELS[_defaults['level']] if root_conf.has_key('level'): level = LEVELS[root_conf.get('level')] root_logger.setLevel(level) if root_conf.has_key('handlers'): for hname in root_conf.get('handlers'): root_logger.addHandler(_getHandler(hname, _getHandlerConfig(hname, config))) else: root_logger.addHandler(_getHandler(_defaults['handler'], _getHandlerConfig(_defaults['handler'], config))) for name, conf in config.get('loggers').iteritems(): level = LEVELS[_defaults['level']] override_level = None if conf.has_key('level'): level = LEVELS[conf.get('level')] override_level = level logger = logging.getLogger(name) logger.setLevel(level) if conf.has_key('handlers'): for hname in conf.get('handlers'): logger.addHandler(_getHandler(hname, _getHandlerConfig(hname, config), override_level)) else: logger.addHandler(_getHandler(_defaults['handler'], _getHandlerConfig(_defaults['handler'], config)))
def logging_setup(name=None, logger=None, logging_config=None, ch=None, fh=None, formatter='', level=logging.DEBUG, logdir=LOGDIR, extrakeys=[]): if not logger: logger = logging.getLogger(name or None) removehandlers(logger) if logging_config: logging.dictConfig(logging_config) return logger fmtstring = "%(levelname)s - %(message)s - %(asctime)s" if extrakeys: fmtstring = (fmtstring + ' - ') + ' - '.join("%" + "(%s)s" % k for k in extrakeys) if formatter: fmtstring = fmtstring + formatter _formatter = logging.Formatter(fmtstring) if not fh: if not os.path.exists(logdir): os.newfolder(logdir) logfile = os.path.join(logdir, logger.name + '.log') fh = logging.handlers.RotatingFileHandler(logfile, encoding='utf-8') fh.setLevel(logging.DEBUG) fh.setFormatter(_formatter) if not ch: ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(_formatter) logger.setLevel(level) logger.addHandler(fh) logger.addHandler(ch) return logger
from logging import dictConfig dictConfig(LOGGING)
if 'CONFIG' in os.environ: import runpy ctx = runpy.run_path(os.environ['CONFIG']) update_globals(ctx) # Local settings if os.path.exists(jroot('settings', 'local.py')): from .local import * # Check required settings invalid_opts = [k for k, v in globals().items() if v == 'UNDEFINED'] if invalid_opts: raise RuntimeError('Following options must be defined: {}'.format( sorted(invalid_opts))) # Configure logging if LOGGING: logging.dictConfig(LOGGING) else: logging.basicConfig(level=LOGGING_LEVEL) # Configure Sentry if SENTRY: from raven import Client from raven.handlers.logging import SentryHandler sentry_client = Client(SENTRY) sentry_handler = SentryHandler(sentry_client) sentry_handler.setLevel('ERROR') logging.getLogger().addHandler(sentry_handler)