blob: 5905542a553a889a10e6aad4431be70de1bba504 [file] [log] [blame]
Zsolt Haraszti86be6f12016-09-27 09:56:49 -07001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Zsolt Haraszti86be6f12016-09-27 09:56:49 -07003#
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
19import logging
20import logging.config
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070021
22import structlog
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070023
24
Zack Williams18357ed2018-11-14 10:41:08 -070025def setup_logging(log_config, instance_id,
26 verbosity_adjust=0, cache_on_use=True):
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070027 """
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 Williams18357ed2018-11-14 10:41:08 -070031 - Optionally cache the logger on first use
32 :return: structured logger
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070033 """
34
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070035 def add_instance_id(_, __, event_dict):
36 event_dict['instance_id'] = instance_id
37 return event_dict
38
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070039 # Configure standard logging
40 logging.config.dictConfig(log_config)
Shad Ansari1f3bb812019-04-16 20:15:46 -070041 # logging.root.level -= 10 * verbosity_adjust
42 logging.root.level = 40 # ERROR
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070043
Zack Williams18357ed2018-11-14 10:41:08 -070044 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 Haraszti86be6f12016-09-27 09:56:49 -070059
60 # Mark first line of log
61 log = structlog.get_logger()
Zack Williams18357ed2018-11-14 10:41:08 -070062 log.info("first-log-line, logging level %d" % logging.root.level)
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070063 return log
khenaidoocbe30832017-08-25 10:43:27 -040064
Matteo Scandolo83dedc12018-09-16 15:11:44 +000065
Zack Williams18357ed2018-11-14 10:41:08 -070066def update_logging(instance_id, vcore_id, cache_on_use=True):
khenaidoocbe30832017-08-25 10:43:27 -040067 """
68 Add the vcore id to the structured logger
69 :param vcore_id: The assigned vcore id
Zack Williams18357ed2018-11-14 10:41:08 -070070 :return: structured logger
khenaidoocbe30832017-08-25 10:43:27 -040071 """
khenaidoocbe30832017-08-25 10:43:27 -040072
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 Williams18357ed2018-11-14 10:41:08 -070081 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 )
khenaidoocbe30832017-08-25 10:43:27 -040097
98 # Mark first line of log
99 log = structlog.get_logger()
100 log.info("updated-logger")
101 return log