source: ogClient-Git/src/log.py @ 81ee4b0

Last change on this file since 81ee4b0 was 3dfe549, checked in by Jose M. Guisado <jguisado@…>, 3 years ago

#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.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1import logging
2import logging.config
3
4DEFAULT_LOGGING_LINUX = {
5    'version': 1,
6    'disable_existing_loggers': False,
7    'formatters': {
8        'formatter.syslog': {
9            '()': 'logging.Formatter',
10            'format': 'ogClient: [{levelname}] - {message}',
11            'style': '{',
12        },
13        'formatter.console': {
14            '()': 'logging.Formatter',
15            'format': '[{levelname}] - {message}',
16            'style': '{',
17        },
18    },
19    'handlers': {
20        'console': {
21            'level': 'INFO',
22            'class': 'logging.StreamHandler',
23            'formatter': 'formatter.console',
24            'stream': 'ext://sys.stdout',
25        },
26        'syslog': {
27            'level': 'DEBUG',
28            'class': 'logging.handlers.SysLogHandler',
29            'formatter': 'formatter.syslog',
30            'address': '/dev/log',
31        },
32    },
33    'loggers': {
34        '': {
35            'handlers': ['syslog', 'console'],
36            'level': 'DEBUG',
37        },
38    }
39}
40
41DEFAULT_LOGGING_WIN = {
42    'version': 1,
43    'disable_existing_loggers': False,
44    'formatters': {
45        'formatter.console': {
46            '()': 'logging.Formatter',
47            'format': 'ogClient: [{levelname}] - {message}',
48            'style': '{',
49        }
50    },
51    'handlers': {
52        'console': {
53            'level': 'DEBUG',
54            'class': 'logging.StreamHandler',
55            'formatter': 'formatter.console',
56            'stream': 'ext://sys.stdout',
57        },
58    },
59    'loggers': {
60        '': {
61            'handlers': ['console'],
62            'level': 'DEBUG',
63        },
64    }
65}
66
67def configure_logging(mode):
68    if mode == 'windows':
69        DEFAULT_LOGGING = DEFAULT_LOGGING_WIN
70    else:
71        DEFAULT_LOGGING = DEFAULT_LOGGING_LINUX
72    logging.config.dictConfig(DEFAULT_LOGGING)
Note: See TracBrowser for help on using the repository browser.