Completed
Push — master ( b8b3a6...4be81e )
by
unknown
26s queued 14s
created

ospd.logger.init_logging()   A

Complexity

Conditions 4

Size

Total Lines 31
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 25
nop 5
dl 0
loc 31
rs 9.28
c 0
b 0
f 0
1
# Copyright (C) 2014-2020 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
22
from logging.config import fileConfig
23
from pathlib import Path
24
from typing import Optional
25
26
27
DEFAULT_HANDLER_CONSOLE = {
28
    'class': 'logging.StreamHandler',
29
    'level': 'INFO',
30
    'formatter': 'file',
31
    'args': 'sys.stdout,',
32
}
33
34
DEFAULT_HANDLER_FILE = {
35
    'class': 'FileHandler',
36
    'level': 'INFO',
37
    'formatter': 'file',
38
}
39
40
DEFAULT_HANDLER_SYSLOG = {
41
    'class': 'handlers.SysLogHandler',
42
    'level': 'INFO',
43
    'formatter': 'syslog',
44
    'args': '("/dev/log", handlers.SysLogHandler.LOG_USER)',
45
}
46
47
DEFAULT_HANDLERS = {'keys': 'default_handler'}
48
DEFAULT_FORMATTERS = {'keys': 'file,syslog'}
49
DEFAULT_FORMATTER_FILE = {
50
    'format': 'OSPD['
51
    + str(os.getpid())
52
    + '] %(asctime)s: %(levelname)s: (%(name)s) %(message)s',
53
    'datefmt': '',
54
}
55
DEFAULT_FORMATTER_SYSLOG = {
56
    'format': 'OSPD['
57
    + str(os.getpid())
58
    + '] %(levelname)s: (%(name)s) %(message)s',
59
    'datefmt': '',
60
}
61
DEFAULT_LOGGERS = {'keys': 'root'}
62
DEFAULT_ROOT_LOGGER = {
63
    'level': 'NOTSET',
64
    'handlers': 'default_handler',
65
    'propagate': '0',
66
}
67
68
69
def init_logging(
70
    log_level: int,
71
    *,
72
    log_file: Optional[str] = None,
73
    log_config: Optional[str] = None,
74
    foreground: Optional[bool] = False
75
):
76
    config = configparser.ConfigParser()
77
    config['handlers'] = DEFAULT_HANDLERS
78
    config['formatters'] = DEFAULT_FORMATTERS
79
    config['formatter_file'] = DEFAULT_FORMATTER_FILE
80
    config['formatter_syslog'] = DEFAULT_FORMATTER_SYSLOG
81
82
    if foreground:
83
        config['handler_default_handler'] = DEFAULT_HANDLER_CONSOLE
84
    elif log_file:
85
        config['handler_default_handler'] = DEFAULT_HANDLER_FILE
86
        config['handler_default_handler']['args'] = "('" + log_file + "', 'a')"
87
    else:
88
        config['handler_default_handler'] = DEFAULT_HANDLER_SYSLOG
89
90
    config['handler_default_handler']['level'] = log_level
91
    log_config_path = Path(log_config)
92
    if log_config_path.exists():
93
        config.read(log_config)
94
    else:
95
        config['loggers'] = DEFAULT_LOGGERS
96
        config['logger_root'] = DEFAULT_ROOT_LOGGER
97
98
    fileConfig(config, disable_existing_loggers=False)
99
    logging.getLogger()
100