blob: a6950b73b5078453aa31c4067625e3fe82c53558 [file] [log] [blame]
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001#
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
19import logging
20import logging.config
21
22import structlog
23
24
25def setup_logging(log_config, instance_id,
26 verbosity_adjust=0, cache_on_use=True):
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)
31 - Optionally cache the logger on first use
32 :return: structured logger
33 """
34
35 def add_instance_id(_, __, event_dict):
36 event_dict['instance_id'] = instance_id
37 return event_dict
38
39 # Configure standard logging
40 logging.config.dictConfig(log_config)
41 logging.root.level -= 10 * verbosity_adjust
42
43 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 )
58
59 # Mark first line of log
60 log = structlog.get_logger()
61 log.info("first-log-line, logging level %d" % logging.root.level)
62 return log
63
64
65def update_logging(instance_id, vcore_id, cache_on_use=True):
66 """
67 Add the vcore id to the structured logger
68 :param vcore_id: The assigned vcore id
69 :return: structured logger
70 """
71
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
80 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 )
96
97 # Mark first line of log
98 log = structlog.get_logger()
99 log.info("updated-logger")
100 return log