Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/airflow/logging_config.py: 64%
47 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
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
20import logging
21import warnings
22from logging.config import dictConfig
24from airflow.configuration import conf
25from airflow.exceptions import AirflowConfigException
26from airflow.utils.module_loading import import_string
28log = logging.getLogger(__name__)
31def configure_logging():
32 """Configure & Validate Airflow Logging."""
33 logging_class_path = ""
34 try:
35 logging_class_path = conf.get("logging", "logging_config_class")
36 except AirflowConfigException:
37 log.debug("Could not find key logging_config_class in config")
39 if logging_class_path:
40 try:
41 logging_config = import_string(logging_class_path)
43 # Make sure that the variable is in scope
44 if not isinstance(logging_config, dict):
45 raise ValueError("Logging Config should be of dict type")
47 log.info("Successfully imported user-defined logging config from %s", logging_class_path)
48 except Exception as err:
49 # Import default logging configurations.
50 raise ImportError(f"Unable to load custom logging from {logging_class_path} due to {err}")
51 else:
52 logging_class_path = "airflow.config_templates.airflow_local_settings.DEFAULT_LOGGING_CONFIG"
53 logging_config = import_string(logging_class_path)
54 log.debug("Unable to load custom logging, using default config instead")
56 try:
57 # Ensure that the password masking filter is applied to the 'task' handler
58 # no matter what the user did.
59 if "filters" in logging_config and "mask_secrets" in logging_config["filters"]:
60 # But if they replace the logging config _entirely_, don't try to set this, it won't work
61 task_handler_config = logging_config["handlers"]["task"]
63 task_handler_config.setdefault("filters", [])
65 if "mask_secrets" not in task_handler_config["filters"]:
66 task_handler_config["filters"].append("mask_secrets")
68 # Try to init logging
69 dictConfig(logging_config)
70 except (ValueError, KeyError) as e:
71 log.error("Unable to load the config, contains a configuration error.")
72 # When there is an error in the config, escalate the exception
73 # otherwise Airflow would silently fall back on the default config
74 raise e
76 validate_logging_config(logging_config)
78 return logging_class_path
81def validate_logging_config(logging_config):
82 """Validate the provided Logging Config."""
83 # Now lets validate the other logging-related settings
84 task_log_reader = conf.get("logging", "task_log_reader")
86 logger = logging.getLogger("airflow.task")
88 def _get_handler(name):
89 return next((h for h in logger.handlers if h.name == name), None)
91 if _get_handler(task_log_reader) is None:
92 # Check for pre 1.10 setting that might be in deployed airflow.cfg files
93 if task_log_reader == "file.task" and _get_handler("task"):
94 warnings.warn(
95 f"task_log_reader setting in [logging] has a deprecated value of {task_log_reader!r}, "
96 "but no handler with this name was found. Please update your config to use task. "
97 "Running config has been adjusted to match",
98 DeprecationWarning,
99 )
100 conf.set("logging", "task_log_reader", "task")
101 else:
102 raise AirflowConfigException(
103 f"Configured task_log_reader {task_log_reader!r} was not a handler of "
104 f"the 'airflow.task' logger."
105 )