def initialize_logging(syslog_tag, syslog_facility, loggers, log_level=logging.INFO, use_syslog=False): if os.path.exists('/dev/log'): syslog_device = '/dev/log' elif os.path.exists('/var/run/syslog'): syslog_device = '/var/run/syslog' base_fmt = ('%(name)s:%(levelname)s %(message)s:%(pathname)s:%(lineno)s') cfg = { 'version': 1, 'filters': {}, 'formatters': { 'debug': { '()': UTF8SafeFormatter, 'datefmt': '%H:%M:%s', 'format': '%(asctime)s ' + base_fmt, }, 'prod': { '()': UTF8SafeFormatter, 'datefmt': '%H:%M:%s', 'format': '%s: [%%(REMOTE_ADDR)s] %s' % (syslog_tag, base_fmt), }, 'tornado': { '()': TornadoLogFormatter, 'color': True }, }, 'handlers': { 'console': { '()': logging.StreamHandler, 'formatter': 'tornado' }, 'null': { '()': NullHandler, }, 'syslog': { '()': logging.handlers.SysLogHandler, 'facility': syslog_facility, 'address': syslog_device, 'formatter': 'prod', }, }, 'loggers': { } } for key, value in loggers.items(): cfg[key].update(value) # Set the level and handlers for all loggers. for logger in cfg['loggers'].values(): if 'handlers' not in logger: logger['handlers'] = ['syslog' if use_syslog else 'console'] if 'level' not in logger: logger['level'] = log_level if 'propagate' not in logger: logger['propagate'] = False dictconfig.dictConfig(cfg)
def log_configure(): """You have to explicitly call this to configure logging.""" for key, value in settings.LOGGING.items(): cfg[key].update(value) USE_SYSLOG = settings.HAS_SYSLOG and not settings.DEBUG if USE_SYSLOG: cfg['loggers']['z.timer'] = {'handlers': ['syslog2']} # Set the level and handlers for all loggers. for logger in cfg['loggers'].values() + [cfg['root']]: if 'handlers' not in logger: logger['handlers'] = ['syslog' if USE_SYSLOG else 'console'] if 'level' not in logger: logger['level'] = settings.LOG_LEVEL if logger is not cfg['root'] and 'propagate' not in logger: logger['propagate'] = False dictconfig.dictConfig(cfg) # logging.getLogger() accesses a singleton, this just binds # in the SentryHandler to error level messages tastypie = logging.getLogger('django.request.tastypie') if settings.USE_METLOG_FOR_RAVEN: tastypie.addHandler(MetlogTastypieHandler(settings.METLOG)) else: tastypie.addHandler(SentryHandler())
def configure_logging(config_dict=DEFAULT_LOGGING): """Configures logging with the specified configuration dictionary. :param config_dict: (optional) Standard Python logging configuration dictionary """ dictconfig.dictConfig(config_dict)
def log_configure(): """You have to explicitly call this to configure logging.""" for key, value in settings.LOGGING.items(): cfg[key].update(value) USE_SYSLOG = settings.HAS_SYSLOG and not settings.DEBUG if USE_SYSLOG: cfg['loggers']['z.timer'] = {'handlers': ['syslog2']} # Set the level and handlers for all loggers. for logger in cfg['loggers'].values() + [cfg['root']]: if 'handlers' not in logger: logger['handlers'] = ['syslog' if USE_SYSLOG else 'console'] if 'level' not in logger: logger['level'] = settings.LOG_LEVEL if logger is not cfg['root'] and 'propagate' not in logger: logger['propagate'] = False dictconfig.dictConfig(cfg) if not settings.DEBUG: task_log = logging.getLogger('z.celery') task_proxy = celery.log.LoggingProxy(task_log) celery.conf.CELERYD_LOG_FILE = task_proxy celery.conf.CELERYD_LOG_COLOR = False
def setLog( config , logfilenm='/tmp/python.log', facil = 'LOG_USER'): if config == 'file': cfg = { 'version': 1, 'root': { 'level': 'NOTSET', 'handlers': ['console', 'file'] }, 'formatters': { 'std': { 'format': '%(name)s[%(process)d]: %(funcName)s: %(levelname)s: %(message)s' } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'formatter': 'std', 'level': 'INFO', 'stream': 'ext://sys.stdout' }, 'file': { 'class': 'logging.FileHandler', 'formatter': 'std', 'level': 'DEBUG', 'filename': logfilenm } } } elif config == 'system': cfg = { 'version': 1, 'root': { 'level': 'NOTSET', 'handlers': ['console', 'syslog'] }, 'formatters': { 'std': { 'format': '%(name)s[%(process)d]: %(funcName)s: %(levelname)s: %(message)s' } }, 'handlers': { 'console': { 'class' : 'logging.StreamHandler', 'formatter': 'std', 'level': 'INFO', 'stream': 'ext://sys.stdout' }, 'syslog': { 'class': 'logging.handlers.SysLogHandler', 'formatter': 'std', 'level': 'DEBUG', 'facility': eval('logging.handlers.SysLogHandler.%s' % facil) } } } dictconfig.dictConfig( cfg ) return logging
def log_configure(): """You have to call this to explicity configure logging.""" cfg = { "version": 1, "filters": {}, "formatters": dict(prod=formatters["prod"]), "handlers": dict(syslog=handlers["syslog"]), "loggers": {"z": {"handlers": ["syslog"]}}, "root": {}, } dictconfig.dictConfig(cfg)
def log_configure(): """You have to call this to explicity configure logging.""" cfg = { 'version': 1, 'filters': {}, 'formatters': dict(prod2=formatters['prod2']), 'handlers': dict(syslog2=handlers['syslog2']), 'loggers': {}, 'root': {}, } dictconfig.dictConfig(cfg)
def log_configure(): """You have to call this to explicity configure logging.""" cfg = { 'version': 1, 'filters': {}, 'formatters': dict(prod=formatters['prod']), 'handlers': dict(syslog=handlers['syslog']), 'loggers': { 'z': {'handlers': ['syslog'], 'level': logging.INFO}, }, 'root': {}, } dictconfig.dictConfig(cfg)
def log_configure(): """You have to call this to explicity configure logging.""" cfg = { 'version': 1, 'filters': {}, 'formatters': dict(prod=formatters['prod']), 'handlers': dict(syslog=handlers['syslog']), 'loggers': { 'z': { 'handlers': ['syslog'], 'level': logging.INFO }, }, 'root': {}, } dictconfig.dictConfig(cfg)
def initialize_logging(syslog_tag, syslog_facility, loggers, log_level=logging.INFO, use_syslog=False): if os.path.exists("/dev/log"): syslog_device = "/dev/log" elif os.path.exists("/var/run/syslog"): syslog_device = "/var/run/syslog" base_fmt = "%(name)s:%(levelname)s %(message)s:%(pathname)s:%(lineno)s" cfg = { "version": 1, "filters": {}, "formatters": { "debug": {"()": UTF8SafeFormatter, "datefmt": "%H:%M:%s", "format": "%(asctime)s " + base_fmt}, "prod": { "()": UTF8SafeFormatter, "datefmt": "%H:%M:%s", "format": "%s: [%%(REMOTE_ADDR)s] %s" % (syslog_tag, base_fmt), }, "tornado": {"()": TornadoLogFormatter, "color": True}, }, "handlers": { "console": {"()": logging.StreamHandler, "formatter": "tornado"}, "null": {"()": NullHandler}, "syslog": { "()": logging.handlers.SysLogHandler, "facility": syslog_facility, "address": syslog_device, "formatter": "prod", }, }, "loggers": {}, } for key, value in loggers.items(): cfg[key].update(value) # Set the level and handlers for all loggers. for logger in cfg["loggers"].values(): if "handlers" not in logger: logger["handlers"] = ["syslog" if use_syslog else "console"] if "level" not in logger: logger["level"] = log_level if "propagate" not in logger: logger["propagate"] = False dictconfig.dictConfig(cfg)
def log_configure(): """You have to call this to explicity configure logging.""" cfg = { 'version': 1, 'filters': {}, 'formatters': dict(prod=formatters['prod']), 'handlers': dict(syslog=handlers['syslog']), 'loggers': { 'z': {'handlers': ['syslog'], 'level': logging.INFO}, }, 'root': {}, # Since this configuration is applied at import time # in verify.py we don't want it to clobber other logs # when imported into the marketplace Django app. 'disable_existing_loggers': False, } dictconfig.dictConfig(cfg)
def setup_logging( default_path='logging.json', default_level=logging.DEBUG, env_key='LOG_CFG' ): """Setup logging configuration """ path = default_path value = os.getenv(env_key, None) if value: path = value if os.path.exists(path): with open(path, 'rt') as f: config = json.load(f) #logging.config.dictConfig(config) dictconfig.dictConfig(config) else: logging.basicConfig(level=default_level)
def log_configure(): """You have to explicitly call this to configure logging.""" for key, value in settings.LOGGING.items(): cfg[key].update(value) USE_SYSLOG = settings.HAS_SYSLOG and not settings.DEBUG if USE_SYSLOG: cfg['loggers']['z.timer'] = {'handlers': ['syslog2']} # Set the level and handlers for all loggers. for logger in cfg['loggers'].values() + [cfg['root']]: if 'handlers' not in logger: logger['handlers'] = ['syslog' if USE_SYSLOG else 'console'] if 'level' not in logger: logger['level'] = settings.LOG_LEVEL if logger is not cfg['root'] and 'propagate' not in logger: logger['propagate'] = False dictconfig.dictConfig(cfg)
def init(base_level=DEFAULT_BASE_LOGGING_LEVEL, verbose_level=DEFAULT_VERBOSE_LOGGING_LEVEL, logging_config=None): """initializes a base logger you can use this to init a logger in any of your files. this will use config.py's LOGGER param and logging.dictConfig to configure the logger for you. :param int|logging.LEVEL base_level: desired base logging level :param int|logging.LEVEL verbose_level: desired verbose logging level :param dict logging_dict: dictConfig based configuration. used to override the default configuration from config.py :rtype: `python logger` """ if logging_config is None: logging_config = {} logging_config = logging_config or LOGGER # TODO: (IMPRV) only perform file related actions if file handler is # TODO: (IMPRV) defined. log_file = LOGGER['handlers']['file']['filename'] log_dir = os.path.dirname(os.path.expanduser(log_file)) if os.path.isfile(log_dir): sys.exit('file {0} exists - log directory cannot be created ' 'there. please remove the file and try again.' .format(log_dir)) try: if not os.path.exists(log_dir) and not len(log_dir) == 0: os.makedirs(log_dir) dictconfig.dictConfig(logging_config) lgr = logging.getLogger('user') lgr.setLevel(base_level) return lgr except ValueError as e: sys.exit('could not initialize logger.' ' verify your logger config' ' and permissions to write to {0} ({1})' .format(log_file, e))
def configure(): """Configures the logger using the default configuration. """ try: log_file = LOGGER['handlers']['file']['filename'] except KeyError as ex: sys.exit('Failed retrieving log file path ({0}).'.format(str(ex))) log_dir = os.path.dirname(os.path.expanduser(log_file)) if os.path.isfile(log_dir): sys.exit( 'File {0} exists - log directory cannot be created ' 'there. please remove the file and try again.'.format(log_dir)) try: if not os.path.exists(log_dir) and not len(log_dir) == 0: os.makedirs(log_dir) dictconfig.dictConfig(LOGGER) except ValueError as ex: sys.exit('Could not configure logger.' ' verify your logger config' ' and permissions to write to {0} ({1})'.format( log_file, str(ex)))
def configure(): """Configures the logger using the default configuration. """ try: log_file = LOGGER['handlers']['file']['filename'] except KeyError as ex: sys.exit('Failed retrieving log file path ({0}).'.format(str(ex))) log_dir = os.path.dirname(os.path.expanduser(log_file)) if os.path.isfile(log_dir): sys.exit('File {0} exists - log directory cannot be created ' 'there. please remove the file and try again.'.format( log_dir)) try: if not os.path.exists(log_dir) and not len(log_dir) == 0: os.makedirs(log_dir) dictconfig.dictConfig(LOGGER) except ValueError as ex: sys.exit('Could not configure logger.' ' verify your logger config' ' and permissions to write to {0} ({1})'.format( log_file, str(ex)))
def init(base_level=DEFAULT_BASE_LOGGING_LEVEL, verbose_level=DEFAULT_VERBOSE_LOGGING_LEVEL, logging_config=None): """initializes a base logger you can use this to init a logger in any of your files. this will use config.py's LOGGER param and logging.dictConfig to configure the logger for you. :param int|logging.LEVEL base_level: desired base logging level :param int|logging.LEVEL verbose_level: desired verbose logging level :param dict logging_dict: dictConfig based configuration. used to override the default configuration from config.py :rtype: `python logger` """ if logging_config is None: logging_config = {} logging_config = logging_config or LOGGER # TODO: (IMPRV) only perform file related actions if file handler is # TODO: (IMPRV) defined. log_file = LOGGER['handlers']['file']['filename'] log_dir = os.path.dirname(os.path.expanduser(log_file)) if os.path.isfile(log_dir): sys.exit( 'file {0} exists - log directory cannot be created ' 'there. please remove the file and try again.'.format(log_dir)) try: if not os.path.exists(log_dir) and not len(log_dir) == 0: os.makedirs(log_dir) dictconfig.dictConfig(logging_config) lgr = logging.getLogger('user') lgr.setLevel(base_level) return lgr except ValueError as e: sys.exit('could not initialize logger.' ' verify your logger config' ' and permissions to write to {0} ({1})'.format(log_file, e))
def log_configure(): """You have to explicitly call this to configure logging.""" for key, value in settings.LOGGING.items(): cfg[key].update(value) USE_SYSLOG = settings.HAS_SYSLOG and not settings.DEBUG if USE_SYSLOG: cfg["loggers"]["z.timer"] = {"handlers": ["syslog2"]} # Set the level and handlers for all loggers. for logger in cfg["loggers"].values() + [cfg["root"]]: if "handlers" not in logger: logger["handlers"] = ["syslog" if USE_SYSLOG else "console"] if "level" not in logger: logger["level"] = settings.LOG_LEVEL if logger is not cfg["root"] and "propagate" not in logger: logger["propagate"] = False dictconfig.dictConfig(cfg) tastypie = logging.getLogger("django.request.tastypie") tastypie.addHandler(SentryHandler())
def log_configure(): """You have to explicitly call this to configure logging.""" for key, value in settings.LOGGING.items(): if isinstance(cfg[key], dict): cfg[key].update(value) else: cfg[key] = value USE_SYSLOG = settings.HAS_SYSLOG and not settings.DEBUG if USE_SYSLOG: cfg["loggers"]["z.timer"] = {"handlers": ["syslog2"]} # Set the level and handlers for all loggers. for logger in cfg["loggers"].values() + [cfg["root"]]: if "handlers" not in logger: logger["handlers"] = ["syslog" if USE_SYSLOG else "console"] if "level" not in logger: logger["level"] = settings.LOG_LEVEL if logger is not cfg["root"] and "propagate" not in logger: logger["propagate"] = False dictconfig.dictConfig(cfg)
'syslog': { '()': logging.handlers.SysLogHandler, 'facility': logging.handlers.SysLogHandler.LOG_LOCAL7, 'formatter': 'prod', }, 'null': { '()': NullHandler, }, }, 'loggers': { 'mdn': {}, }, 'root': {}, } for key, value in settings.LOGGING.items(): cfg[key].update(value) # Set the level and handlers for all loggers. for logger in cfg['loggers'].values() + [cfg['root']]: syslog = settings.HAS_SYSLOG and not settings.DEBUG if 'handlers' not in logger: logger['handlers'] = ['syslog' if syslog else 'console'] if 'level' not in logger: logger['level'] = settings.LOG_LEVEL if logger is not cfg['root'] and 'propagate' not in logger: logger['propagate'] = False dictconfig.dictConfig(cfg)
'console':{ 'level':'DEBUG', 'class':'logging.StreamHandler', 'formatter': 'simple' }, 'file': { 'class': 'logging.handlers.RotatingFileHandler', 'level': 'ERROR', 'formatter': 'detailed', 'filename': BASE_DIR + '/logs/standalone.log', 'mode': 'a', 'maxBytes': 10485760, 'backupCount': 5, }, }, 'loggers': { 'standalone': { 'handlers':['console','file'], 'propagate': True, 'level':'DEBUG', }, } } # assign it to logging dictconfig dictconfig.dictConfig(LOGGING) # get root logger LOGGER = logging.getLogger('standalone')
} }, 'loggers': { 'cocreate': { 'handlers': ['default', 'file'], 'level': 'DEBUG', 'propagate': True }, 'django.request': { 'handlers': ['default'], 'level': 'WARN', 'propagate': False }, } } dictconfig.dictConfig(conf) logger = logging.getLogger('cocreate') # Initialize Rabbitmq connection connection = pika.BlockingConnection(pika.ConnectionParameters('screenbird.com')) cocreate_queue = connection.channel() cocreate_queue.queue_declare(queue=settings.COCREATE_QUEUE_NAME, durable=True) # Initialize Amazon S3 connection s3 = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) bucket = s3.get_bucket(settings.AWS_VIDEO_BUCKET_NAME) def download_from_s3(slug): """ Downloads a given slug to a file from s3. """
def apply_config(self, conf): dictConfig(conf)
def initialize_logging(log_config): use_syslog = log_config.pop('use_syslog', False) log_level = log_config.pop('log_level', logging.INFO) syslog_tag = log_config.pop('syslog_tag') syslog_facility = log_config.pop('syslog_facility', logging.handlers.SysLogHandler.LOG_LOCAL0) if os.path.exists('/dev/log'): syslog_device = '/dev/log' elif os.path.exists('/var/run/syslog'): syslog_device = '/var/run/syslog' base_fmt = ('%(name)s:%(levelname)s %(message)s') cfg = { 'version': 1, 'filters': {}, 'formatters': { 'debug': { '()': UTF8SafeFormatter, 'datefmt': '%H:%M:%s', 'format': '%(asctime)s ' + base_fmt, }, 'prod': { '()': UTF8SafeFormatter, 'datefmt': '%H:%M:%s', 'format': '%s: [%%(REMOTE_ADDR)s] %s' % (syslog_tag, base_fmt), }, 'tornado': { '()': TornadoLogFormatter, 'color': True }, }, 'handlers': { 'console': { '()': logging.StreamHandler, 'formatter': 'tornado' }, 'null': { '()': NullHandler, }, 'syslog': { '()': logging.handlers.SysLogHandler, 'facility': syslog_facility, 'address': syslog_device, 'formatter': 'prod', }, }, 'loggers': { # root logger '': {} } } for key, value in log_config.items(): cfg[key].update(value) # Set the level and handlers for all loggers. for logger in cfg['loggers'].values(): if 'handlers' not in logger: logger['handlers'] = ['syslog' if use_syslog else 'console'] if 'level' not in logger: logger['level'] = log_level if 'propagate' not in logger: logger['propagate'] = False # Set up color if we are in a tty and curses is installed cfg['formatters']['tornado']['color'] = False if curses and sys.stderr.isatty(): try: curses.setupterm() if curses.tigetnum("colors") > 0: cfg['formatters']['tornado']['color'] = True except: pass dictconfig.dictConfig(cfg)
import logging #import cloghandler import logging.config #formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(levelname)s - %(message)s') #logger = logging.getLogger("compress_log"); #logger.setLevel(logging.DEBUG) #ch = logging.FileHandler("/var/log/z-agent.log") #ch.setFormatter(formatter) #logger.addHandler(ch) #ch1=logging.StreamHandler() #ch1.setFormatter(formatter) #console = logging.getLogger(__name__) #console.setLevel(logging.DEBUG) #console.addHandler() import os import yaml import sys basepath = os.path.dirname(__file__).rstrip(__name__) yaml_cfg = os.path.join(basepath, "conf", "log.yaml") if sys.version_info[0] == 2 and sys.version_info[1] < 7: import dictconfig dictconfig.dictConfig(yaml.load(open(yaml_cfg, "r"))) else: logging.config.dictConfig(yaml.load(open(yaml_cfg, "r")))
}, 'loggers': { 'django.request': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': False, }, 'cef': { 'handlers': ['cef_syslog' if use_syslog else 'cef_console'], } }, 'root': {}, } for key, value in settings.LOGGING.items(): if hasattr(cfg[key], 'update'): cfg[key].update(value) else: cfg[key] = value # Set the level and handlers for all loggers. for logger in cfg['loggers'].values() + [cfg['root']]: if 'handlers' not in logger: logger['handlers'] = ['syslog' if use_syslog else 'console'] if 'level' not in logger: logger['level'] = settings.LOG_LEVEL if logger is not cfg['root'] and 'propagate' not in logger: logger['propagate'] = False dictconfig.dictConfig(cfg)
def initialize_logging(log_config): use_syslog = log_config.pop('use_syslog', False) log_level = log_config.pop('log_level', logging.INFO) syslog_tag = log_config.pop('syslog_tag') syslog_facility = log_config.pop('syslog_facility', logging.handlers.SysLogHandler.LOG_LOCAL0) if os.path.exists('/dev/log'): syslog_device = '/dev/log' elif os.path.exists('/var/run/syslog'): syslog_device = '/var/run/syslog' base_fmt = ('%(name)s:%(levelname)s %(message)s') cfg = { 'version': 1, 'filters': {}, 'formatters': { 'debug': { '()': UTF8SafeFormatter, 'datefmt': '%H:%M:%s', 'format': '%(asctime)s ' + base_fmt, }, 'prod': { '()': UTF8SafeFormatter, 'datefmt': '%H:%M:%s', 'format': '%s: [%%(REMOTE_ADDR)s] %s' % (syslog_tag, base_fmt), }, #'tornado': { # '()': TornadoLogFormatter, # 'color': True #}, }, 'handlers': { 'console': { '()': logging.StreamHandler, 'formatter': 'debug' }, 'null': { '()': NullHandler, }, 'syslog': { '()': logging.handlers.SysLogHandler, 'facility': syslog_facility, 'address': syslog_device, 'formatter': 'prod', }, }, 'loggers': { # root logger '': {} } } for key, value in log_config.items(): cfg[key].update(value) # Set the level and handlers for all loggers. for logger in cfg['loggers'].values(): if 'handlers' not in logger: logger['handlers'] = ['syslog' if use_syslog else 'console'] if 'level' not in logger: logger['level'] = log_level if 'propagate' not in logger: logger['propagate'] = False # Set up color if we are in a tty and curses is installed #cfg['formatters']['tornado']['color'] = False #if curses and sys.stderr.isatty(): # try: # curses.setupterm() # if curses.tigetnum("colors") > 0: # cfg['formatters']['tornado']['color'] = True # except: # pass dictconfig.dictConfig(cfg)
def setUp(self): dictconfig.dictConfig(cfg) self.log = logging.getLogger('test.lib.misc.logging') self.request = RequestFactory().get('http://foo.com/blargh')
def setUp(self): dictconfig.dictConfig(cfg) self.log = logging.getLogger('test.lib.misc.logging')
} }, 'loggers': { 'cocreate': { 'handlers': ['default', 'file'], 'level': 'DEBUG', 'propagate': True }, 'django.request': { 'handlers': ['default'], 'level': 'WARN', 'propagate': False }, } } dictconfig.dictConfig(conf) logger = logging.getLogger('cocreate') # Initialize Rabbitmq connection connection = pika.BlockingConnection( pika.ConnectionParameters('screenbird.com')) cocreate_queue = connection.channel() cocreate_queue.queue_declare(queue=settings.COCREATE_QUEUE_NAME, durable=True) # Initialize Amazon S3 connection s3 = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) bucket = s3.get_bucket(settings.AWS_VIDEO_BUCKET_NAME) def download_from_s3(slug):