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 |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 21 | |
| 22 | import structlog |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 23 | |
| 24 | |
Zack Williams | 18357ed | 2018-11-14 10:41:08 -0700 | [diff] [blame] | 25 | def setup_logging(log_config, instance_id, |
| 26 | verbosity_adjust=0, cache_on_use=True): |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 27 | """ |
| 28 | Set up logging such that: |
| 29 | - The primary logging entry method is structlog |
| 30 | (see http://structlog.readthedocs.io/en/stable/index.html) |
Zack Williams | 18357ed | 2018-11-14 10:41:08 -0700 | [diff] [blame] | 31 | - Optionally cache the logger on first use |
| 32 | :return: structured logger |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 33 | """ |
| 34 | |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 35 | def add_instance_id(_, __, event_dict): |
| 36 | event_dict['instance_id'] = instance_id |
| 37 | return event_dict |
| 38 | |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 39 | # Configure standard logging |
| 40 | logging.config.dictConfig(log_config) |
Shad Ansari | 1f3bb81 | 2019-04-16 20:15:46 -0700 | [diff] [blame] | 41 | # logging.root.level -= 10 * verbosity_adjust |
| 42 | logging.root.level = 40 # ERROR |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 43 | |
Zack Williams | 18357ed | 2018-11-14 10:41:08 -0700 | [diff] [blame] | 44 | structlog.configure( |
| 45 | processors=[ |
| 46 | structlog.stdlib.filter_by_level, |
| 47 | structlog.stdlib.PositionalArgumentsFormatter(), |
| 48 | structlog.processors.StackInfoRenderer(), |
| 49 | structlog.processors.format_exc_info, |
| 50 | add_instance_id, |
| 51 | structlog.processors.UnicodeEncoder(), |
| 52 | structlog.stdlib.ProcessorFormatter.wrap_for_formatter, |
| 53 | ], |
| 54 | context_class=dict, |
| 55 | logger_factory=structlog.stdlib.LoggerFactory(), |
| 56 | wrapper_class=structlog.stdlib.BoundLogger, |
| 57 | cache_logger_on_first_use=cache_on_use, |
| 58 | ) |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 59 | |
| 60 | # Mark first line of log |
| 61 | log = structlog.get_logger() |
Zack Williams | 18357ed | 2018-11-14 10:41:08 -0700 | [diff] [blame] | 62 | log.info("first-log-line, logging level %d" % logging.root.level) |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 63 | return log |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 64 | |
Matteo Scandolo | 83dedc1 | 2018-09-16 15:11:44 +0000 | [diff] [blame] | 65 | |
Zack Williams | 18357ed | 2018-11-14 10:41:08 -0700 | [diff] [blame] | 66 | def update_logging(instance_id, vcore_id, cache_on_use=True): |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 67 | """ |
| 68 | Add the vcore id to the structured logger |
| 69 | :param vcore_id: The assigned vcore id |
Zack Williams | 18357ed | 2018-11-14 10:41:08 -0700 | [diff] [blame] | 70 | :return: structured logger |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 71 | """ |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 72 | |
| 73 | def add_instance_id(_, __, event_dict): |
| 74 | event_dict['instance_id'] = instance_id |
| 75 | return event_dict |
| 76 | |
| 77 | def add_vcore_id(_, __, event_dict): |
| 78 | event_dict['vcore_id'] = vcore_id |
| 79 | return event_dict |
| 80 | |
Zack Williams | 18357ed | 2018-11-14 10:41:08 -0700 | [diff] [blame] | 81 | structlog.configure( |
| 82 | processors=[ |
| 83 | structlog.stdlib.filter_by_level, |
| 84 | structlog.stdlib.PositionalArgumentsFormatter(), |
| 85 | structlog.processors.StackInfoRenderer(), |
| 86 | structlog.processors.format_exc_info, |
| 87 | add_instance_id, |
| 88 | add_vcore_id, |
| 89 | structlog.processors.UnicodeEncoder(), |
| 90 | structlog.stdlib.ProcessorFormatter.wrap_for_formatter, |
| 91 | ], |
| 92 | context_class=dict, |
| 93 | logger_factory=structlog.stdlib.LoggerFactory(), |
| 94 | wrapper_class=structlog.stdlib.BoundLogger, |
| 95 | cache_logger_on_first_use=cache_on_use, |
| 96 | ) |
khenaidoo | cbe3083 | 2017-08-25 10:43:27 -0400 | [diff] [blame] | 97 | |
| 98 | # Mark first line of log |
| 99 | log = structlog.get_logger() |
| 100 | log.info("updated-logger") |
| 101 | return log |