diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2022-06-06 14:45:41 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2022-06-08 10:27:06 +0200 |
commit | 30fdcceea3efbd264b00d78fb0f86fd5a2ff8831 (patch) | |
tree | ce0f5c1cf19fd19049ee5d51bdb8137c1bc974b2 /src/log.py | |
parent | 1ab981a539f3553021b3cf1642619338f2782af1 (diff) |
src: improve logging
Adds new logging handler redirecting messages to the log file
located in the Samba shared directory (applies to live mode
clients, i.e: ogLive)
Parses log level configuration from ogclient.json. See:
{
"opengnsys": {
...
"log": "INFO",
...
}
...
}
Adds --debug option to set root logger level to DEBUG when starting
ogClient. Overrides log level from config file.
In addition:
- Replaces any occurence of print with a corresponding logging function.
- Unsets log level for handlers, use root logger level instead.
- Default level for root logger is INFO.
- Replaces level from response log messages to debug (ogRest)
Diffstat (limited to 'src/log.py')
-rw-r--r-- | src/log.py | 40 |
1 files changed, 33 insertions, 7 deletions
@@ -1,5 +1,8 @@ import logging import logging.config +import os + +from src.utils.net import getifaddr DEFAULT_LOGGING_LINUX = { 'version': 1, @@ -15,25 +18,34 @@ DEFAULT_LOGGING_LINUX = { 'format': '[{levelname}] - {message}', 'style': '{', }, + 'formatter.syslogtime': { + '()': 'logging.Formatter', + 'datefmt': '%Y-%m-%d %H:%M:%S', + 'format': '({asctime}) ogClient: [{levelname}] - {message}', + 'style': '{', + }, }, 'handlers': { 'console': { - 'level': 'INFO', 'class': 'logging.StreamHandler', 'formatter': 'formatter.console', 'stream': 'ext://sys.stdout', }, 'syslog': { - 'level': 'DEBUG', 'class': 'logging.handlers.SysLogHandler', 'formatter': 'formatter.syslog', 'address': '/dev/log', }, + 'samba': { + 'class': 'logging.FileHandler', + 'formatter': 'formatter.syslogtime', + 'filename': f'/opt/opengnsys/log/{getifaddr(os.getenv("DEVICE"))}.log', + }, }, 'loggers': { '': { 'handlers': ['syslog', 'console'], - 'level': 'DEBUG', + 'level': 'INFO', }, } } @@ -64,9 +76,23 @@ DEFAULT_LOGGING_WIN = { } } -def configure_logging(mode): +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': - DEFAULT_LOGGING = DEFAULT_LOGGING_WIN + logconfig = DEFAULT_LOGGING_WIN else: - DEFAULT_LOGGING = DEFAULT_LOGGING_LINUX - logging.config.dictConfig(DEFAULT_LOGGING) + logconfig = DEFAULT_LOGGING_LINUX + + if mode == 'live': + logconfig['loggers']['']['handlers'].append('samba') + + logconfig['loggers']['']['level'] = level + + logging.config.dictConfig(logconfig) |