Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 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 | |
| 19 | import logging |
| 20 | import logging.config |
| 21 | from collections import OrderedDict |
| 22 | |
| 23 | import structlog |
| 24 | from structlog.stdlib import BoundLogger, INFO |
| 25 | |
| 26 | try: |
| 27 | from thread import get_ident as _get_ident |
| 28 | except ImportError: |
| 29 | from dummy_thread import get_ident as _get_ident |
| 30 | |
| 31 | |
| 32 | class 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 | |
| 42 | class 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 | |
| 61 | def 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 |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def update_logging(instance_id, vcore_id): |
| 117 | """ |
| 118 | Add the vcore id to the structured logger |
| 119 | :param vcore_id: The assigned vcore id |
| 120 | :return: structure logger |
| 121 | """ |
| 122 | def add_exc_info_flag_for_exception(_, name, event_dict): |
| 123 | if name == 'exception': |
| 124 | event_dict['exc_info'] = True |
| 125 | return event_dict |
| 126 | |
| 127 | def add_instance_id(_, __, event_dict): |
| 128 | event_dict['instance_id'] = instance_id |
| 129 | return event_dict |
| 130 | |
| 131 | def add_vcore_id(_, __, event_dict): |
| 132 | event_dict['vcore_id'] = vcore_id |
| 133 | return event_dict |
| 134 | |
| 135 | processors = [ |
| 136 | add_exc_info_flag_for_exception, |
| 137 | structlog.processors.StackInfoRenderer(), |
| 138 | structlog.processors.format_exc_info, |
| 139 | add_instance_id, |
| 140 | add_vcore_id, |
| 141 | FluentRenderer(), |
| 142 | ] |
| 143 | structlog.configure(processors=processors) |
| 144 | |
| 145 | # Mark first line of log |
| 146 | log = structlog.get_logger() |
| 147 | log.info("updated-logger") |
| 148 | return log |