summaryrefslogtreecommitdiffstats
path: root/src/log.py
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2021-11-18 10:29:46 +0100
committerJose M. Guisado <jguisado@soleta.eu>2021-11-18 10:29:46 +0100
commit3dfe54968b6e50d5b85391bd9302c2a4f0a21aa3 (patch)
tree423e8958b9ac5e34fa57a605b75c3934e77d3433 /src/log.py
parentfd1f01d76b803f2b72165d4e79b516dc50bde52f (diff)
#1065 Use logging module instead of syslog
We can't use syslog if we want to execute ogClient in the Windows platform. Use the native logging library so we can attach different handlers depending on the mode ogClient is executing. Logging configuration is done via a python dict. There is a different dict for linux and windows. These dicts define the configuration of the root logger, handlers and formatters used. As of now, it is only expected to use the root logger for everything logging related. The root logger is obtained via: LOGGER = logging.getLogger() More info about handlers, formatters and loggers: https://docs.python.org/3/howto/logging.html Logging configuration is done at startup, just after parsing the json (knowing ogclient mode). If json parsing goes bad, ogclient will only print a message to stdout.
Diffstat (limited to 'src/log.py')
-rw-r--r--src/log.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/log.py b/src/log.py
new file mode 100644
index 0000000..9d379eb
--- /dev/null
+++ b/src/log.py
@@ -0,0 +1,72 @@
+import logging
+import logging.config
+
+DEFAULT_LOGGING_LINUX = {
+ 'version': 1,
+ 'disable_existing_loggers': False,
+ 'formatters': {
+ 'formatter.syslog': {
+ '()': 'logging.Formatter',
+ 'format': 'ogClient: [{levelname}] - {message}',
+ 'style': '{',
+ },
+ 'formatter.console': {
+ '()': 'logging.Formatter',
+ 'format': '[{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',
+ },
+ },
+ 'loggers': {
+ '': {
+ 'handlers': ['syslog', 'console'],
+ 'level': 'DEBUG',
+ },
+ }
+}
+
+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',
+ },
+ },
+ 'loggers': {
+ '': {
+ 'handlers': ['console'],
+ 'level': 'DEBUG',
+ },
+ }
+}
+
+def configure_logging(mode):
+ if mode == 'windows':
+ DEFAULT_LOGGING = DEFAULT_LOGGING_WIN
+ else:
+ DEFAULT_LOGGING = DEFAULT_LOGGING_LINUX
+ logging.config.dictConfig(DEFAULT_LOGGING)