1#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18from __future__ import annotations
19
20import structlog.processors
21
22OLD_DEFAULT_LOG_FORMAT = "[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
23OLD_DEFAULT_COLOR_LOG_FORMAT = (
24 "[%(blue)s%(asctime)s%(reset)s] {%(blue)s%(filename)s:%(reset)s%(lineno)d} "
25 "%(log_color)s%(levelname)s%(reset)s - %(log_color)s%(message)s%(reset)s"
26)
27
28
29# This doesn't load the values from config, to avoid a cross dependency between shared logging and shared
30# config modules.
31def translate_config_values(
32 log_format: str, callsite_params: list[str]
33) -> tuple[str, tuple[structlog.processors.CallsiteParameter, ...]]:
34 if log_format == OLD_DEFAULT_LOG_FORMAT:
35 # It's the default, use the coloured version by default. This will automatically not put color codes
36 # if we're not a tty, or if colors are disabled
37 log_format = OLD_DEFAULT_COLOR_LOG_FORMAT
38
39 # This will raise an exception if the value isn't valid
40 params_out = tuple(
41 getattr(structlog.processors.CallsiteParameter, p, None) or structlog.processors.CallsiteParameter(p)
42 for p in filter(None, callsite_params)
43 )
44
45 return log_format, params_out