diff options
author | Javier Sánchez Parra <jsanchez@soleta.eu> | 2022-09-27 17:35:22 +0200 |
---|---|---|
committer | Javier Sánchez Parra <jsanchez@soleta.eu> | 2022-11-02 16:37:41 +0100 |
commit | f1fff0b0b6587363523e408c19732ff7c1fef411 (patch) | |
tree | 9874fa9e632ffb23eeb1db4cf270607f3f1ca56f /src | |
parent | 3703fd60634fe41f3ae131bd2b4c4ca9f0ef8b3e (diff) |
log: import fcntl only on linux
Otherwise, ogClient do not work on Windows because fcntl module do not
exists on Windows.
Diffstat (limited to 'src')
-rw-r--r-- | src/log.py | 140 |
1 files changed, 73 insertions, 67 deletions
@@ -2,79 +2,85 @@ import logging import logging.config import os -from src.utils.net import getifaddr -DEFAULT_LOGGING_LINUX = { - 'version': 1, - 'disable_existing_loggers': False, - 'formatters': { - 'formatter.syslog': { - '()': 'logging.Formatter', - 'format': 'ogClient: [{levelname}] - {message}', - 'style': '{', +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': '{', + }, }, - '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', - }, - 'samba': { - 'class': 'logging.FileHandler', - 'formatter': 'formatter.syslogtime', - 'filename': f'/opt/opengnsys/log/{getifaddr(os.getenv("DEVICE"))}.log', - }, - }, - 'loggers': { - '': { - 'handlers': ['syslog', 'console'], - 'level': 'INFO', + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'formatter.console', + 'stream': 'ext://sys.stdout', + }, + 'syslog': { + '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': 'INFO', + }, + } } -} + return logconfig -DEFAULT_LOGGING_WIN = { - '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', + +def _default_logging_win(): + logconfig = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'formatter.console': { + '()': 'logging.Formatter', + 'format': 'ogClient: [{levelname}] - {message}', + 'style': '{', + } }, - }, - 'loggers': { - '': { - 'handlers': ['console'], - 'level': 'DEBUG', + '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): """ @@ -86,9 +92,9 @@ def configure_logging(mode, level): logging to the expected samba shared log file ({ip}.txt.log). """ if mode == 'windows': - logconfig = DEFAULT_LOGGING_WIN + logconfig = _default_logging_win() else: - logconfig = DEFAULT_LOGGING_LINUX + logconfig = _default_logging_linux() if mode == 'live': logconfig['loggers']['']['handlers'].append('samba') |