From f1fff0b0b6587363523e408c19732ff7c1fef411 Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Tue, 27 Sep 2022 17:35:22 +0200 Subject: log: import fcntl only on linux Otherwise, ogClient do not work on Windows because fcntl module do not exists on Windows. --- src/log.py | 140 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 73 insertions(+), 67 deletions(-) (limited to 'src/log.py') diff --git a/src/log.py b/src/log.py index bf00c71..ef1fe71 100644 --- a/src/log.py +++ b/src/log.py @@ -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') -- cgit v1.2.3-18-g5258