File: logging_config.py

package info (click to toggle)
python-diskimage-builder 3.37.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,572 kB
  • sloc: sh: 7,380; python: 6,444; makefile: 37
file content (95 lines) | stat: -rw-r--r-- 3,122 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright 2016 Andreas Florath (andreas@florath.net)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# Python Logging Configuration for DIB
# Usage:
# In the main (application) file, do an
#   import logging_config
#   ...
#   logging_config.setup()
# It is then possible to use the normal python logging interface, like
#   logger = logging.getLogger(__name__)
#   logger.info("Info Message")

import json
import logging
import logging.config
import os


# A simple formatter to more or less copy oslo.log's ContextFormatter
class DibFormatter(logging.Formatter):

    def __init__(self, *args, **kwargs):
        self.fmt = kwargs['fmt']
        self.debug_suffix = kwargs.pop('debug_suffix')
        logging.Formatter.__init__(self, *args, **kwargs)

    def format(self, record):
        if record.levelno == logging.DEBUG:
            fmt = self.fmt + self.debug_suffix
        else:
            fmt = self.fmt

        self._style = logging.PercentStyle(fmt)
        self._fmt = self._style._fmt

        return logging.Formatter.format(self, record)


def setup():
    # Check for the DIB_DEBUG_TRACE environment variable
    # If it is set to something greater then 0, use debug
    # logging.
    LOG_LEVEL = "DEBUG" if 'DIB_DEBUG_TRACE' in os.environ \
                and int(os.environ['DIB_DEBUG_TRACE']) > 0 else "INFO"

    # Default logging configuration which can be overwritten
    # by a config file passed in by a user.
    PYTHON_LOGGING_CONFIG = {
        'version': 1,
        'disable_existing_loggers': False,

        # note that disk-image-create runs stdout through
        # outfilter.py, which adds the timestamp.  this doesn't have a
        # timestamp to avoid double logging
        'formatters': {
            'standard': {
                '()': 'diskimage_builder.logging_config.DibFormatter',
                'fmt': '%(levelname)s '
                '%(name)s [-] %(message)s',
                'debug_suffix': ' %(funcName)s %(pathname)s:%(lineno)d'
            }
        },
        'handlers': {
            'default': {
                'level': LOG_LEVEL,
                'class': 'logging.StreamHandler',
                'formatter': 'standard',
            }
        },
        'loggers': {
            '': {
                'handlers': ['default'],
                'level': LOG_LEVEL,
                'propagate': True
            }
        }
    }

    if 'DIB_PYTHON_LOGGING_CONFIG_FILE' in os.environ:
        with open(os.environ['DIB_PYTHON_LOGGING_CONFIG_FILE']) as fd:
            PYTHON_LOGGING_CONFIG = json.load(fd)

    logging.config.dictConfig(PYTHON_LOGGING_CONFIG)