ospd.logger.init_logging()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nop 5
dl 0
loc 32
rs 9.256
c 0
b 0
f 0
1
# Copyright (C) 2014-2021 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: AGPL-3.0-or-later
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Affero General Public License as
7
# published by the Free Software Foundation, either version 3 of the
8
# License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
import logging
19
import os
20
import configparser
21
import time
22
23
from logging.config import fileConfig
24
from pathlib import Path
25
from typing import Optional
26
27
28
DEFAULT_HANDLER_CONSOLE = {
29
    'class': 'logging.StreamHandler',
30
    'level': 'INFO',
31
    'formatter': 'file',
32
    'args': 'sys.stdout,',
33
}
34
35
DEFAULT_HANDLER_FILE = {
36
    'class': 'FileHandler',
37
    'level': 'INFO',
38
    'formatter': 'file',
39
}
40
41
DEFAULT_HANDLER_SYSLOG = {
42
    'class': 'handlers.SysLogHandler',
43
    'level': 'INFO',
44
    'formatter': 'syslog',
45
    'args': '("/dev/log", handlers.SysLogHandler.LOG_USER)',
46
}
47
48
DEFAULT_HANDLERS = {'keys': 'default_handler'}
49
DEFAULT_FORMATTERS = {'keys': 'file,syslog'}
50
DEFAULT_FORMATTER_FILE = {
51
    'format': 'OSPD['
52
    + str(os.getpid())
53
    + '] %(asctime)s: %(levelname)s: (%(name)s) %(message)s',
54
    'datefmt': '',
55
}
56
DEFAULT_FORMATTER_SYSLOG = {
57
    'format': 'OSPD['
58
    + str(os.getpid())
59
    + '] %(levelname)s: (%(name)s) %(message)s',
60
    'datefmt': '',
61
}
62
DEFAULT_LOGGERS = {'keys': 'root'}
63
DEFAULT_ROOT_LOGGER = {
64
    'level': 'NOTSET',
65
    'handlers': 'default_handler',
66
    'propagate': '0',
67
}
68
69
70
def init_logging(
71
    log_level: int,
72
    *,
73
    log_file: Optional[str] = None,
74
    log_config: Optional[str] = None,
75
    foreground: Optional[bool] = False,
76
):
77
    config = configparser.ConfigParser()
78
    config['handlers'] = DEFAULT_HANDLERS
79
    config['formatters'] = DEFAULT_FORMATTERS
80
    config['formatter_file'] = DEFAULT_FORMATTER_FILE
81
    config['formatter_syslog'] = DEFAULT_FORMATTER_SYSLOG
82
83
    if foreground:
84
        config['handler_default_handler'] = DEFAULT_HANDLER_CONSOLE
85
    elif log_file:
86
        config['handler_default_handler'] = DEFAULT_HANDLER_FILE
87
        config['handler_default_handler']['args'] = "('" + log_file + "', 'a')"
88
    else:
89
        config['handler_default_handler'] = DEFAULT_HANDLER_SYSLOG
90
91
    config['handler_default_handler']['level'] = log_level
92
    log_config_path = Path(log_config)
93
    if log_config_path.exists():
94
        config.read(log_config)
95
    else:
96
        config['loggers'] = DEFAULT_LOGGERS
97
        config['logger_root'] = DEFAULT_ROOT_LOGGER
98
99
    fileConfig(config, disable_existing_loggers=False)
100
    logging.Formatter.converter = time.gmtime
101
    logging.getLogger()
102