khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 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 | |
| 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 StructuredLogRenderer(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, we need to pass it into the logger framework as the first |
| 36 | # 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): |
| 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 | """ |
| 68 | |
| 69 | def add_exc_info_flag_for_exception(_, name, event_dict): |
| 70 | if name == 'exception': |
| 71 | event_dict['exc_info'] = True |
| 72 | return event_dict |
| 73 | |
| 74 | def add_instance_id(_, __, event_dict): |
| 75 | event_dict['instance_id'] = instance_id |
| 76 | return event_dict |
| 77 | |
| 78 | # Configure standard logging |
| 79 | logging.config.dictConfig(log_config) |
| 80 | logging.root.level -= 10 * verbosity_adjust |
| 81 | |
| 82 | processors = [ |
| 83 | add_exc_info_flag_for_exception, |
| 84 | structlog.processors.StackInfoRenderer(), |
| 85 | structlog.processors.format_exc_info, |
| 86 | add_instance_id, |
| 87 | StructuredLogRenderer(), |
| 88 | ] |
| 89 | structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(), |
| 90 | context_class=PlainRenderedOrderedDict, |
| 91 | wrapper_class=BoundLogger, |
| 92 | processors=processors) |
| 93 | |
| 94 | # Mark first line of log |
| 95 | log = structlog.get_logger() |
| 96 | log.info("first-line") |
| 97 | return log |
| 98 | |
| 99 | |
| 100 | def update_logging(instance_id, vcore_id): |
| 101 | """ |
| 102 | Add the vcore id to the structured logger |
| 103 | :param vcore_id: The assigned vcore id |
| 104 | :return: structure logger |
| 105 | """ |
| 106 | def add_exc_info_flag_for_exception(_, name, event_dict): |
| 107 | if name == 'exception': |
| 108 | event_dict['exc_info'] = True |
| 109 | return event_dict |
| 110 | |
| 111 | def add_instance_id(_, __, event_dict): |
| 112 | if instance_id is not None: |
| 113 | event_dict['instance_id'] = instance_id |
| 114 | return event_dict |
| 115 | |
| 116 | def add_vcore_id(_, __, event_dict): |
| 117 | if vcore_id is not None: |
| 118 | event_dict['vcore_id'] = vcore_id |
| 119 | return event_dict |
| 120 | |
| 121 | processors = [ |
| 122 | add_exc_info_flag_for_exception, |
| 123 | structlog.processors.StackInfoRenderer(), |
| 124 | structlog.processors.format_exc_info, |
| 125 | add_instance_id, |
| 126 | add_vcore_id, |
| 127 | StructuredLogRenderer(), |
| 128 | ] |
| 129 | structlog.configure(processors=processors) |
| 130 | |
| 131 | # Mark first line of log |
| 132 | log = structlog.get_logger() |
| 133 | log.info("updated-logger") |
| 134 | return log |