Skip to content
Snippets Groups Projects
Commit 9fd8acd9 authored by Frisinghelli Daniel's avatar Frisinghelli Daniel
Browse files

Made logging to file optional.

parent bc471953
No related branches found
No related tags found
No related merge requests found
...@@ -18,8 +18,38 @@ License ...@@ -18,8 +18,38 @@ License
import pathlib import pathlib
# logging configuration dictionary
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'brief': {
'format': '%(name)s: %(message)s'
},
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
'datefmt': '%Y-%m-%dT%H:%M:%S'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'brief',
'level': 'INFO',
'stream': 'ext://sys.stderr',
},
},
'loggers': {
'': {
'handlers': ['console'],
'level': 'INFO',
},
}
}
# the logging configuration dictionary # the logging configuration dictionary
def log_conf(logfile): def log_conf(logfile=None):
"""Set basic logging configuration. """Set basic logging configuration.
See the logging `docs`_ for a detailed description of the configuration See the logging `docs`_ for a detailed description of the configuration
...@@ -31,7 +61,8 @@ def log_conf(logfile): ...@@ -31,7 +61,8 @@ def log_conf(logfile):
Parameters Parameters
---------- ----------
logfile : `str` or :py:class:`pathlib.Path` logfile : `str` or :py:class:`pathlib.Path`
The file to save the logs to. The file to save the logs to. The default is `None`, which means only
log to stdout and not to file.
Returns Returns
------- -------
...@@ -40,44 +71,19 @@ def log_conf(logfile): ...@@ -40,44 +71,19 @@ def log_conf(logfile):
""" """
# check if the parent directory of the log file exists # check if the parent directory of the log file exists
logfile = pathlib.Path(logfile) if logfile is not None:
if not logfile.parent.is_dir(): logfile = pathlib.Path(logfile)
logfile.parent.mkdir(parents=True, exist_ok=True) if not logfile.parent.is_dir():
logfile.parent.mkdir(parents=True, exist_ok=True)
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'brief': {
'format': '%(name)s: %(message)s'
},
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
'datefmt': '%Y-%m-%dT%H:%M:%S'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'brief',
'level': 'INFO',
'stream': 'ext://sys.stderr',
},
'file': { # add log file to logging configuration
LOGGING_CONFIG['handlers']['file'] = {
'class': 'logging.FileHandler', 'class': 'logging.FileHandler',
'formatter': 'standard', 'formatter': 'standard',
'level': 'INFO', 'level': 'INFO',
'filename': logfile, 'filename': logfile,
'mode': 'a' 'mode': 'a'
} }
}, LOGGING_CONFIG['loggers']['']['handlers'].append('file')
'loggers': {
'': {
'handlers': ['console', 'file'],
'level': 'INFO',
},
}
}
return LOGGING_CONFIG return LOGGING_CONFIG
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment