xref: /aosp_15_r20/external/pigweed/pw_arduino_build/py/pw_arduino_build/log.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Configure the system logger for the default pw command log format."""
15
16import logging
17
18_LOG = logging.getLogger(__name__)
19_STDERR_HANDLER = logging.StreamHandler()
20
21
22def install(level: int = logging.INFO) -> None:
23    """Configure the system logger for the arduino_builder log format."""
24
25    try:
26        import pw_cli.log  # pylint: disable=import-outside-toplevel
27
28        pw_cli.log.install(level=level)
29    except ImportError:
30        # Set log level on root logger to debug, otherwise any higher levels
31        # elsewhere are ignored.
32        root = logging.getLogger()
33        root.setLevel(logging.DEBUG)
34
35        _STDERR_HANDLER.setLevel(level)
36        _STDERR_HANDLER.setFormatter(
37            logging.Formatter(
38                "[%(asctime)s] " "%(levelname)s %(message)s", "%Y%m%d %H:%M:%S"
39            )
40        )
41        root.addHandler(_STDERR_HANDLER)
42
43
44def set_level(log_level: int):
45    """Sets the log level for logs to stderr."""
46    _STDERR_HANDLER.setLevel(log_level)
47