Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [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 | |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 19 | from __future__ import absolute_import |
| 20 | import builtins # bring in python 2 to 3 compat imports: https://python-future.org/imports.html |
Chip Boling | a9febbf | 2020-07-29 12:34:12 -0500 | [diff] [blame^] | 21 | import json |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 22 | import logging |
| 23 | import logging.config |
| 24 | from collections import OrderedDict |
| 25 | |
| 26 | import structlog |
| 27 | from structlog.stdlib import BoundLogger, INFO |
| 28 | |
| 29 | try: |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 30 | from _thread import get_ident as _get_ident |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 31 | except ImportError: |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 32 | from _dummy_thread import get_ident as _get_ident |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class StructuredLogRenderer(object): |
| 36 | def __call__(self, logger, name, event_dict): |
| 37 | # in order to keep structured log data in event_dict to be forwarded as |
| 38 | # is, we need to pass it into the logger framework as the first |
| 39 | # positional argument. |
| 40 | args = (event_dict,) |
| 41 | kwargs = {} |
| 42 | return args, kwargs |
| 43 | |
| 44 | |
Chip Boling | a9febbf | 2020-07-29 12:34:12 -0500 | [diff] [blame^] | 45 | class EncoderFix(json.JSONEncoder): |
| 46 | def default(self, obj): |
| 47 | return str(obj) |
| 48 | |
| 49 | |
| 50 | class JsonRenderedOrderedDict(OrderedDict): |
| 51 | """Our special version of OrderedDict that renders into string as a dict, |
| 52 | to make the log stream output cleaner. |
| 53 | """ |
| 54 | def __repr__(self, _repr_running=None): |
| 55 | # od.__repr__() <==> repr(od) |
| 56 | call_key = id(self), _get_ident() |
| 57 | _repr_running = _repr_running or dict() |
| 58 | |
| 59 | if call_key in _repr_running: |
| 60 | return '...' |
| 61 | |
| 62 | _repr_running[call_key] = 1 |
| 63 | try: |
| 64 | if not self: |
| 65 | return '' |
| 66 | |
| 67 | # Convert to JSON but strip off outside '{}' |
| 68 | msg = '"msg":"{}",'.format(self.pop('event')) |
| 69 | json_msg = json.dumps(self, indent=None, |
| 70 | cls=EncoderFix, |
| 71 | separators=(', ', ': '), |
| 72 | sort_keys=True)[1:-1] |
| 73 | return msg + json_msg |
| 74 | |
| 75 | finally: |
| 76 | del _repr_running[call_key] |
| 77 | |
| 78 | |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 79 | class PlainRenderedOrderedDict(OrderedDict): |
| 80 | """Our special version of OrderedDict that renders into string as a dict, |
| 81 | to make the log stream output cleaner. |
| 82 | """ |
| 83 | def __repr__(self, _repr_running={}): |
| 84 | 'od.__repr__() <==> repr(od)' |
| 85 | call_key = id(self), _get_ident() |
| 86 | if call_key in _repr_running: |
| 87 | return '...' |
| 88 | _repr_running[call_key] = 1 |
| 89 | try: |
| 90 | if not self: |
| 91 | return '{}' |
| 92 | return '{%s}' % ", ".join("%s: %s" % (k, v) |
| 93 | for k, v in self.items()) |
| 94 | finally: |
| 95 | del _repr_running[call_key] |
| 96 | |
| 97 | |
| 98 | def setup_logging(log_config, instance_id, verbosity_adjust=0): |
| 99 | """ |
| 100 | Set up logging such that: |
| 101 | - The primary logging entry method is structlog |
| 102 | (see http://structlog.readthedocs.io/en/stable/index.html) |
| 103 | - By default, the logging backend is Python standard lib logger |
| 104 | """ |
| 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 | # Configure standard logging |
| 116 | logging.config.dictConfig(log_config) |
Rohan Agrawal | c1ee296 | 2020-03-25 20:33:11 +0000 | [diff] [blame] | 117 | logging.root.level = verbosity_adjust |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 118 | |
| 119 | processors = [ |
| 120 | add_exc_info_flag_for_exception, |
| 121 | structlog.processors.StackInfoRenderer(), |
| 122 | structlog.processors.format_exc_info, |
| 123 | add_instance_id, |
Chip Boling | a9febbf | 2020-07-29 12:34:12 -0500 | [diff] [blame^] | 124 | structlog.processors.UnicodeDecoder(), |
| 125 | structlog.processors.JSONRenderer() |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 126 | ] |
| 127 | structlog.configure(logger_factory=structlog.stdlib.LoggerFactory(), |
Chip Boling | a9febbf | 2020-07-29 12:34:12 -0500 | [diff] [blame^] | 128 | context_class=JsonRenderedOrderedDict, |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 129 | wrapper_class=BoundLogger, |
| 130 | processors=processors) |
| 131 | |
| 132 | # Mark first line of log |
| 133 | log = structlog.get_logger() |
Matteo Scandolo | 63efb06 | 2019-11-26 12:14:48 -0700 | [diff] [blame] | 134 | log.info("first-line", log_level=logging.root.level, verbosity_adjust=verbosity_adjust) |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 135 | return log |
| 136 | |
| 137 | |
Rohan Agrawal | c1ee296 | 2020-03-25 20:33:11 +0000 | [diff] [blame] | 138 | def string_to_int(loglevel): |
| 139 | l = loglevel.upper() |
| 140 | if l == "DEBUG": return 10 |
| 141 | elif l == "INFO": return 20 |
| 142 | elif l == "WARN": return 30 |
| 143 | elif l == "ERROR": return 40 |
| 144 | elif l == "FATAL": return 50 |
| 145 | else: return 0 |
| 146 | |
| 147 | |
Chip Boling | a9febbf | 2020-07-29 12:34:12 -0500 | [diff] [blame^] | 148 | def update_logging(instance_id, _vcore_id, verbosity_adjust=0): |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 149 | """ |
| 150 | Add the vcore id to the structured logger |
| 151 | :param vcore_id: The assigned vcore id |
| 152 | :return: structure logger |
| 153 | """ |
| 154 | def add_exc_info_flag_for_exception(_, name, event_dict): |
| 155 | if name == 'exception': |
| 156 | event_dict['exc_info'] = True |
| 157 | return event_dict |
| 158 | |
| 159 | def add_instance_id(_, __, event_dict): |
| 160 | event_dict['instance_id'] = instance_id |
| 161 | return event_dict |
| 162 | |
Rohan Agrawal | c1ee296 | 2020-03-25 20:33:11 +0000 | [diff] [blame] | 163 | logging.root.level = verbosity_adjust |
| 164 | |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 165 | processors = [ |
| 166 | add_exc_info_flag_for_exception, |
| 167 | structlog.processors.StackInfoRenderer(), |
| 168 | structlog.processors.format_exc_info, |
| 169 | add_instance_id, |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 170 | StructuredLogRenderer(), |
| 171 | ] |
| 172 | structlog.configure(processors=processors) |
| 173 | |
| 174 | # Mark first line of log |
| 175 | log = structlog.get_logger() |
| 176 | log.info("updated-logger") |
| 177 | return log |