blob: d4b2e8e2192f20fba6cf1383a9b55ee00d731610 [file] [log] [blame]
Zsolt Harasztid7ae9d52016-09-27 09:56:49 -07001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""Setting up proper logging for Voltha"""
18
19import logging
20import logging.config
21from collections import OrderedDict
22
23import structlog
24from structlog.stdlib import BoundLogger, INFO
25
26try:
27 from thread import get_ident as _get_ident
28except ImportError:
29 from dummy_thread import get_ident as _get_ident
30
31
32class FluentRenderer(object):
33 def __call__(self, logger, name, event_dict):
34 # in order to keep structured log data in event_dict to be forwarded as
35 # is to the fluent logger, we need to pass it into the logger framework
36 # as the first positional argument.
37 args = (event_dict, )
38 kwargs = {}
39 return args, kwargs
40
41
42class PlainRenderedOrderedDict(OrderedDict):
43 """Our special version of OrderedDict that renders into string as a dict,
44 to make the log stream output cleaner.
45 """
46 def __repr__(self, _repr_running={}):
47 'od.__repr__() <==> repr(od)'
48 call_key = id(self), _get_ident()
49 if call_key in _repr_running:
50 return '...'
51 _repr_running[call_key] = 1
52 try:
53 if not self:
54 return '{}'
55 return '{%s}' % ", ".join("%s: %s" % (k, v)
56 for k, v in self.items())
57 finally:
58 del _repr_running[call_key]
59
60
61def setup_logging(log_config, instance_id, verbosity_adjust=0, fluentd=None):
62 """
63 Set up logging such that:
64 - The primary logging entry method is structlog
65 (see http://structlog.readthedocs.io/en/stable/index.html)
66 - By default, the logging backend is Python standard lib logger
67 - Alternatively, fluentd can be configured with to be the backend,
68 providing direct bridge to a fluent logging agent.
69 """
70
71 def add_exc_info_flag_for_exception(_, name, event_dict):
72 if name == 'exception':
73 event_dict['exc_info'] = True
74 return event_dict
75
76 def add_instance_id(_, __, event_dict):
77 event_dict['instance_id'] = instance_id
78 return event_dict
79
80 # if fluentd is specified, we need to override the config data with
81 # its host and port info
82 if fluentd is not None:
83 fluentd_host = fluentd.split(':')[0].strip()
84 fluentd_port = int(fluentd.split(':')[1].strip())
85
86 handlers = log_config.get('handlers', None)
87 if isinstance(handlers, dict):
88 for _, defs in handlers.iteritems():
89 if isinstance(defs, dict):
90 if defs.get('class', '').endswith('FluentHandler'):
91 defs['host'] = fluentd_host
92 defs['port'] = fluentd_port
93
94 # Configure standard logging
95 logging.config.dictConfig(log_config)
96 logging.root.level -= 10 * verbosity_adjust
97
98 processors = [
99 add_exc_info_flag_for_exception,
100 structlog.processors.StackInfoRenderer(),
101 structlog.processors.format_exc_info,
102 add_instance_id,
103 FluentRenderer(),
104 ]
105 structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(),
106 context_class=PlainRenderedOrderedDict,
107 wrapper_class=BoundLogger,
108 processors=processors)
109
110 # Mark first line of log
111 log = structlog.get_logger()
112 log.info("first-line")
113 return log