blob: a6950b73b5078453aa31c4067625e3fe82c53558 [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)
41 logging.root.level -= 10 * verbosity_adjust
42
Zack Williams18357ed2018-11-14 10:41:08 -070043 structlog.configure(
44 processors=[
45 structlog.stdlib.filter_by_level,
46 structlog.stdlib.PositionalArgumentsFormatter(),
47 structlog.processors.StackInfoRenderer(),
48 structlog.processors.format_exc_info,
49 add_instance_id,
50 structlog.processors.UnicodeEncoder(),
51 structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
52 ],
53 context_class=dict,
54 logger_factory=structlog.stdlib.LoggerFactory(),
55 wrapper_class=structlog.stdlib.BoundLogger,
56 cache_logger_on_first_use=cache_on_use,
57 )
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070058
59 # Mark first line of log
60 log = structlog.get_logger()
Zack Williams18357ed2018-11-14 10:41:08 -070061 log.info("first-log-line, logging level %d" % logging.root.level)
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070062 return log
khenaidoocbe30832017-08-25 10:43:27 -040063
Matteo Scandolo83dedc12018-09-16 15:11:44 +000064
Zack Williams18357ed2018-11-14 10:41:08 -070065def update_logging(instance_id, vcore_id, cache_on_use=True):
khenaidoocbe30832017-08-25 10:43:27 -040066 """
67 Add the vcore id to the structured logger
68 :param vcore_id: The assigned vcore id
Zack Williams18357ed2018-11-14 10:41:08 -070069 :return: structured logger
khenaidoocbe30832017-08-25 10:43:27 -040070 """
khenaidoocbe30832017-08-25 10:43:27 -040071
72 def add_instance_id(_, __, event_dict):
73 event_dict['instance_id'] = instance_id
74 return event_dict
75
76 def add_vcore_id(_, __, event_dict):
77 event_dict['vcore_id'] = vcore_id
78 return event_dict
79
Zack Williams18357ed2018-11-14 10:41:08 -070080 structlog.configure(
81 processors=[
82 structlog.stdlib.filter_by_level,
83 structlog.stdlib.PositionalArgumentsFormatter(),
84 structlog.processors.StackInfoRenderer(),
85 structlog.processors.format_exc_info,
86 add_instance_id,
87 add_vcore_id,
88 structlog.processors.UnicodeEncoder(),
89 structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
90 ],
91 context_class=dict,
92 logger_factory=structlog.stdlib.LoggerFactory(),
93 wrapper_class=structlog.stdlib.BoundLogger,
94 cache_logger_on_first_use=cache_on_use,
95 )
khenaidoocbe30832017-08-25 10:43:27 -040096
97 # Mark first line of log
98 log = structlog.get_logger()
99 log.info("updated-logger")
100 return log