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 | |
khenaidoo | 50b286d | 2018-03-02 17:44:30 -0500 | [diff] [blame] | 32 | class StructuredLogRenderer(object): |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 33 | def __call__(self, logger, name, event_dict): |
| 34 | # in order to keep structured log data in event_dict to be forwarded as |
khenaidoo | 50b286d | 2018-03-02 17:44:30 -0500 | [diff] [blame] | 35 | # is, we need to pass it into the logger framework as the first |
| 36 | # positional argument. |
| 37 | args = (event_dict,) |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 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 | |
khenaidoo | 50b286d | 2018-03-02 17:44:30 -0500 | [diff] [blame] | 61 | def setup_logging(log_config, instance_id, verbosity_adjust=0): |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 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 |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 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 | |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 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, |
khenaidoo | 50b286d | 2018-03-02 17:44:30 -0500 | [diff] [blame] | 87 | StructuredLogRenderer(), |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 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 |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 98 | |
Matteo Scandolo | 83dedc1 | 2018-09-16 15:11:44 +0000 | [diff] [blame] | 99 | |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 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 | event_dict['instance_id'] = instance_id |
| 113 | return event_dict |
| 114 | |
| 115 | def add_vcore_id(_, __, event_dict): |
| 116 | event_dict['vcore_id'] = vcore_id |
| 117 | return event_dict |
| 118 | |
| 119 | processors = [ |
| 120 | add_exc_info_flag_for_exception, |
| 121 | structlog.processors.StackInfoRenderer(), |
| 122 | structlog.processors.format_exc_info, |
| 123 | add_instance_id, |
| 124 | add_vcore_id, |
khenaidoo | 50b286d | 2018-03-02 17:44:30 -0500 | [diff] [blame] | 125 | StructuredLogRenderer(), |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 126 | ] |
| 127 | structlog.configure(processors=processors) |
| 128 | |
| 129 | # Mark first line of log |
| 130 | log = structlog.get_logger() |
| 131 | log.info("updated-logger") |
| 132 | return log |