Introduced config file and fixed traceback support in exception logs
diff --git a/voltha/main.py b/voltha/main.py
index 7c2aacf..9f03cf0 100755
--- a/voltha/main.py
+++ b/voltha/main.py
@@ -18,6 +18,9 @@
"""Virtual OLT Hardware Abstraction main entry point"""
import argparse
+import os
+
+import yaml
from structlog_setup import setup_logging, DEFAULT_FLUENT_SERVER
@@ -26,19 +29,15 @@
parser = argparse.ArgumentParser()
+ # generic parameters
parser.add_argument('--no-banner', dest='no_banner', action='store_true', default=False,
help='omit startup banner log lines')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False,
help='enable verbose logging')
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False,
help="suppress debug and info logs")
-
- # fluentd logging related options
- parser.add_argument('--enable-fluent', dest='enable_fluent', action='store_true', default=False,
- help='enable log stream emission to fluent(d) agent')
- parser.add_argument('--fluent-server', dest='fluent_server', action='store', default=DEFAULT_FLUENT_SERVER,
- # variable='<fluent-host>:<fluent-port>',
- help="<fluent-host>:<fluent-port>: host name (or ip address) and tcp port of fluent agent")
+ parser.add_argument('-c', '--config', dest='config', action='store', default='./voltha.yml',
+ help='Path to voltha.yml config file. If relative, it is relative to main.py of voltha')
# placeholder
parser.add_argument('-i', '--interface', dest='interface', action='store', default='eth0',
@@ -47,6 +46,17 @@
return parser.parse_args()
+def load_config(args):
+ path = args.config
+ if path.startswith('.'):
+ dir = os.path.dirname(os.path.abspath(__file__))
+ path = os.path.join(dir, path)
+ path = os.path.abspath(path)
+ with open(path) as fd:
+ config = yaml.load(fd)
+ return config
+
+
def print_banner(args, log):
log.info(' _ ______ __ ________ _____ ')
log.info('| | / / __ \/ / /_ __/ / / / |')
@@ -70,7 +80,8 @@
def main():
args = parse_args()
- log = setup_logging(args)
+ config = load_config(args)
+ log = setup_logging(config.get('logging', {}))
if not args.no_banner:
print_banner(args, log)
start_reactor(args, log) # will not return except Keyboard interrupt
diff --git a/voltha/structlog_setup.py b/voltha/structlog_setup.py
index 01fa973..2382c0e 100644
--- a/voltha/structlog_setup.py
+++ b/voltha/structlog_setup.py
@@ -21,8 +21,7 @@
from collections import OrderedDict
import structlog
-import sys
-import yaml
+from structlog.stdlib import BoundLogger
try:
from thread import get_ident as _get_ident
@@ -61,44 +60,7 @@
del _repr_running[call_key]
-def setup_fluent_logging(args):
- """Setup structlog pipeline and hook it into the fluent sender"""
-
- # Configure standard logging
- with open('logging.yaml') as fd:
- conf = yaml.load(fd)
- logging.config.dictConfig(conf['logging'])
-
- processors = [
- structlog.processors.StackInfoRenderer(),
- structlog.processors.format_exc_info,
- FluentRenderer(),
- ]
- structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(),
- context_class=PlainRenderedOrderedDict,
- processors=processors)
-
-
-def setup_standard_logging(args):
- """Setup structlog pipeline and hook it into the standard logging framework of Python"""
-
- # Configure standard logging
- DATEFMT = '%Y%m%dT%H%M%S'
- FORMAT = '%(asctime)-11s.%(msecs)03d %(levelname)-8s %(msg)s'
- level = logging.DEBUG if args.verbose else (logging.WARN if args.quiet else logging.INFO)
- logging.basicConfig(stream=sys.stdout, level=level, format=FORMAT, datefmt=DATEFMT)
-
- # Hook up structlog to standard logging
- processors = [
- structlog.processors.StackInfoRenderer(),
- structlog.processors.format_exc_info,
- structlog.processors.KeyValueRenderer(), # structlog.processorsJSONRenderer(),
-
- ]
- structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(), processors=processors)
-
-
-def setup_logging(args):
+def setup_logging(log_config):
"""
Set up logging such that:
- The primary logging entry method is structlog (see http://structlog.readthedocs.io/en/stable/index.html)
@@ -107,10 +69,24 @@
bridge to a fluent logging agent.
"""
- if args.enable_fluent:
- setup_fluent_logging(args)
- else:
- setup_standard_logging(args)
+ def add_exc_info_flag_for_exception(logger, name, event_dict):
+ if name == 'exception':
+ event_dict['exc_info'] = True
+ return event_dict
+
+ # Configure standard logging
+ logging.config.dictConfig(log_config)
+
+ processors = [
+ add_exc_info_flag_for_exception,
+ structlog.processors.StackInfoRenderer(),
+ structlog.processors.format_exc_info,
+ FluentRenderer(),
+ ]
+ structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(),
+ context_class=PlainRenderedOrderedDict,
+ wrapper_class=BoundLogger,
+ processors=processors)
# Mark first line of log
log = structlog.get_logger()
diff --git a/logging.yaml b/voltha/voltha.yml
similarity index 100%
rename from logging.yaml
rename to voltha/voltha.yml