Update default loggers to console and file and configure them to run out
of the box.
Change-Id: I93a31a5527bbf16ee5f2340dad670900c5285107
diff --git a/multistructlog.py b/multistructlog.py
index 960b5bf..6d2b724 100644
--- a/multistructlog.py
+++ b/multistructlog.py
@@ -42,12 +42,14 @@
import structlog
import sys
import copy
+import inspect
PROCESSOR_MAP = {
'StreamHandler': structlog.dev.ConsoleRenderer(),
- 'LogstashHandler': structlog.processors.JSONRenderer(),
+ 'LogstashHandler': structlog.processors.JSONRenderer()
}
+
class FormatterFactory:
def __init__(self, handler_name):
self.handler_name = handler_name
@@ -80,7 +82,7 @@
""" We expose the Structlog logging interface directly. This should allow callers to
- bind contexts incrementally and configure and use other features of structlog directly
+ bind contexts incrementally and configure and use other features of structlog directly
The use of structlog in Chameleon was used for reference when writing this code.
"""
@@ -88,12 +90,14 @@
CURRENT_LOGGER = None
CURRENT_LOGGER_PARMS = None
-def create_logger(_config=None, extra_processors=[], force_create=False, level=None):
+
+def create_logger(_config=None, extra_processors=[],
+ force_create=False, level=None):
"""
Args:
_config (dict): The standard config for Python's logging module
extra_processors(dict): Custom structlog processors
- force_create(bool): Forces creation of the logger
+ force_create(bool): Forces creation of the logger
level(logging.loglevel): Overrides logging level
Returns:
@@ -109,16 +113,17 @@
first_entry_elts.append('Config is empty')
logging_config = {'version': 1}
- """Check if a logger with this configuration has already been created, if so, return that logger
+ """Check if a logger with this configuration has already been created, if so, return that logger
instead of creating a new one"""
global CURRENT_LOGGER
global CURRENT_LOGGER_PARMS
- if CURRENT_LOGGER and CURRENT_LOGGER_PARMS == (logging_config, extra_processors, level) and not force_create:
+ if CURRENT_LOGGER and CURRENT_LOGGER_PARMS == (
+ logging_config, extra_processors, level) and not force_create:
return CURRENT_LOGGER
-
+
if level:
try:
- for k,v in logging_config['loggers'].iteritems():
+ for k, v in logging_config['loggers'].iteritems():
v['level'] = level
except KeyError:
first_entry_elts.append('Level override failed')
@@ -131,10 +136,16 @@
structlog.processors.format_exc_info,
structlog.stdlib.ProcessorFormatter.wrap_for_formatter
])
-
+
+ caller = inspect.stack()[1]
+ filename = inspect.getmodule(caller[0]).__name__
+
default_handlers = [
logging.StreamHandler(sys.stdout),
- logstash.LogstashHandler('localhost', 5617, version=1)
+ logging.handlers.RotatingFileHandler(
+ filename=filename,
+ maxBytes=10485760,
+ backupCount=1)
]
configured_handlers = logging.getLogger().handlers
@@ -148,12 +159,14 @@
log = structlog.get_logger()
first_entry = '. '.join(first_entry_elts)
- log.info(first_entry, level_override=level,**logging_config)
+ log.info(first_entry, level_override=level, **logging_config)
CURRENT_LOGGER = log
CURRENT_LOGGER_PARMS = (logging_config, extra_processors, level)
return log
+
if __name__ == '__main__':
- l = create_logger({'version': 2, 'loggers':{'':{'level': 'INFO'}}}, level="INFO")
+ l = create_logger(
+ {'version': 2, 'loggers': {'': {'level': 'INFO'}}}, level="INFO")
l.info("Test OK")
diff --git a/setup.py b/setup.py
index 2d54b3b..09bb895 100644
--- a/setup.py
+++ b/setup.py
@@ -17,7 +17,7 @@
from setuptools import setup
setup(name='multistructlog',
- version='0.9',
+ version='1.0',
description='structlog with multiple simultaneous logging backends',
author='Varun Belur, Sapan Bhatia',
author_email='varun@opennetworking.org,sapan@opennetworking.org',
diff --git a/tests/test_logger.py b/tests/test_logger.py
index 5897fb6..9e40ca7 100644
--- a/tests/test_logger.py
+++ b/tests/test_logger.py
@@ -72,7 +72,6 @@
logger.info('Test 1')
logger.debug('Test 2')
- pdb.set_trace()
self.assertEqual(mock_logging.StreamHandler.call_count, 2)