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