summaryrefslogtreecommitdiffstats
path: root/src/log.py
blob: cabe4d08744a7136018f3937608c0ec89d4ad8cb (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import logging
import logging.config
import os


def _default_logging_linux():
    from src.utils.net import getifaddr
    logconfig = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'formatter.syslog': {
                '()': 'logging.Formatter',
                'format': 'ogClient: [{levelname}] - {message}',
                'style': '{',
            },
            'formatter.console': {
                '()': 'logging.Formatter',
                'format': '[{levelname}] - {message}',
                'style': '{',
            },
            'formatter.syslogtime': {
                '()': 'logging.Formatter',
                'datefmt': '%Y-%m-%d %H:%M:%S',
                'format': '({asctime}) ogClient: [{levelname}] - {message}',
                'style': '{',
            },
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'formatter': 'formatter.console',
                'stream': 'ext://sys.stdout',
            },
            'syslog': {
                'class': 'logging.handlers.SysLogHandler',
                'formatter': 'formatter.syslog',
                'address': '/dev/log',
            },
        },
        'loggers': {
            '': {
                'handlers': ['syslog', 'console'],
                'level': 'INFO',
            },
        }
    }
    return logconfig


def _default_logging_live():
    from src.utils.net import getifaddr
    logconfig = _default_logging_linux()
    samba = {
            'samba': {
                'class': 'logging.FileHandler',
                'formatter': 'formatter.syslogtime',
                'filename': f'/opt/opengnsys/log/{getifaddr(os.getenv("DEVICE"))}.log',
            }
        }
    logconfig['handlers'].update(samba)
    logconfig['loggers']['']['handlers'].append('samba')
    return logconfig


def _default_logging_win():
    logconfig = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'formatter.console': {
                '()': 'logging.Formatter',
                'format': 'ogClient: [{levelname}] - {message}',
                'style': '{',
            }
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'formatter.console',
                'stream': 'ext://sys.stdout',
            },
        },
        'loggers': {
            '': {
                'handlers': ['console'],
                'level': 'DEBUG',
            },
        }
    }
    return logconfig


def configure_logging(mode, level):
    """
    Receives a ogClient operating mode.

    Configures the default logger according to the operating mode.

    For example, in the case of running live mode it will activate
    logging to the expected samba shared log file ({ip}.txt.log).
    """
    if mode == 'windows':
        logconfig = _default_logging_win()
    elif mode == 'linux':
        logconfig = _default_logging_linux()
    elif mode == 'live':
        logconfig = _default_logging_live()
    else:
        raise ValueError(f'Error: Mode {mode} not supported')

    logconfig['loggers']['']['level'] = level

    logging.config.dictConfig(logconfig)