source: ogClient-Git/src/log.py @ eac9426

Last change on this file since eac9426 was 7f6a7b6, checked in by Jose M. Guisado <jguisado@…>, 2 years ago

log: add file handler for ogLive "real time log"

Clients running in ogLive can show log messages via a lighttp server.
Particularly, a html page named "real time log" consists of <text-area>
tags with the contents of two particular text files
/tmp/session.log and /tmp/command.log

Adds a Python logging handler in order to write ogClient log messages
into /tmp/session.log. This way ogClient logs are show in the "real time
log" html page too.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1import logging
2import logging.config
3import os
4
5
6def _default_logging_linux():
7    from src.utils.net import getifaddr
8    logconfig = {
9        'version': 1,
10        'disable_existing_loggers': False,
11        'formatters': {
12            'formatter.syslog': {
13                '()': 'logging.Formatter',
14                'format': 'ogClient: [{levelname}] - {message}',
15                'style': '{',
16            },
17            'formatter.console': {
18                '()': 'logging.Formatter',
19                'format': '[{levelname}] - {message}',
20                'style': '{',
21            },
22            'formatter.syslogtime': {
23                '()': 'logging.Formatter',
24                'datefmt': '%Y-%m-%d %H:%M:%S',
25                'format': '({asctime}) ogClient: [{levelname}] - {message}',
26                'style': '{',
27            },
28        },
29        'handlers': {
30            'console': {
31                'class': 'logging.StreamHandler',
32                'formatter': 'formatter.console',
33                'stream': 'ext://sys.stdout',
34            },
35            'syslog': {
36                'class': 'logging.handlers.SysLogHandler',
37                'formatter': 'formatter.syslog',
38                'address': '/dev/log',
39            },
40        },
41        'loggers': {
42            '': {
43                'handlers': ['syslog', 'console'],
44                'level': 'INFO',
45            },
46        }
47    }
48    return logconfig
49
50
51def _default_logging_live():
52    from src.utils.net import getifaddr
53    logconfig = _default_logging_linux()
54    samba = {
55            'samba': {
56                'class': 'logging.FileHandler',
57                'formatter': 'formatter.syslogtime',
58                'filename': f'/opt/opengnsys/log/{getifaddr(os.getenv("DEVICE"))}.log',
59            }
60        }
61    rtlog = {
62            'rtlog': {
63                'class': 'logging.FileHandler',
64                'formatter': 'formatter.syslogtime',
65                'filename': f'/tmp/session.log',
66            }
67        }
68    logconfig['handlers'].update(samba)
69    logconfig['handlers'].update(rtlog)
70    logconfig['loggers']['']['handlers'].append('samba')
71    logconfig['loggers']['']['handlers'].append('rtlog')
72    return logconfig
73
74
75def _default_logging_win():
76    logconfig = {
77        'version': 1,
78        'disable_existing_loggers': False,
79        'formatters': {
80            'formatter.console': {
81                '()': 'logging.Formatter',
82                'format': 'ogClient: [{levelname}] - {message}',
83                'style': '{',
84            }
85        },
86        'handlers': {
87            'console': {
88                'level': 'DEBUG',
89                'class': 'logging.StreamHandler',
90                'formatter': 'formatter.console',
91                'stream': 'ext://sys.stdout',
92            },
93        },
94        'loggers': {
95            '': {
96                'handlers': ['console'],
97                'level': 'DEBUG',
98            },
99        }
100    }
101    return logconfig
102
103
104def configure_logging(mode, level):
105    """
106    Receives a ogClient operating mode.
107
108    Configures the default logger according to the operating mode.
109
110    For example, in the case of running live mode it will activate
111    logging to the expected samba shared log file ({ip}.txt.log).
112    """
113    if mode == 'windows':
114        logconfig = _default_logging_win()
115    elif mode == 'linux':
116        logconfig = _default_logging_linux()
117    elif mode == 'live':
118        logconfig = _default_logging_live()
119    else:
120        raise ValueError(f'Error: Mode {mode} not supported')
121
122    logconfig['loggers']['']['level'] = level
123
124    logging.config.dictConfig(logconfig)
Note: See TracBrowser for help on using the repository browser.