Passed
Pull Request — master (#293)
by Juan José
01:26
created

ospd.logger.init_logging()   A

Complexity

Conditions 4

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
nop 5
dl 0
loc 30
rs 9.304
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
    'args': "('/home/jnicola/install/var/log/gvm/openvas.log', 'a')",
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
    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