Rework logging configuration

This commit is contained in:
Alexandre Aubin 2021-01-31 16:01:38 +01:00
parent c931f8a532
commit fcf9e58ce4

View file

@ -93,15 +93,13 @@ def init_logging(interface="cli",
if not os.path.isdir(logdir): if not os.path.isdir(logdir):
os.makedirs(logdir, 0o750) os.makedirs(logdir, 0o750)
# ####################################################################### # logging_configuration = {
# Logging configuration for CLI (or any other interface than api...) #
# ####################################################################### #
if interface != "api":
configure_logging({
'version': 1, 'version': 1,
'main_logger': "yunohost",
'disable_existing_loggers': True, 'disable_existing_loggers': True,
'formatters': { 'formatters': {
'console': {
'format': '%(relativeCreated)-5d %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s'
},
'tty-debug': { 'tty-debug': {
'format': '%(relativeCreated)-4d %(fmessage)s' 'format': '%(relativeCreated)-4d %(fmessage)s'
}, },
@ -115,11 +113,15 @@ def init_logging(interface="cli",
}, },
}, },
'handlers': { 'handlers': {
'tty': { 'cli': {
'level': 'DEBUG' if debug else 'INFO', 'level': 'DEBUG' if debug else 'INFO',
'class': 'moulinette.interfaces.cli.TTYHandler', 'class': 'moulinette.interfaces.cli.TTYHandler',
'formatter': 'tty-debug' if debug else '', 'formatter': 'tty-debug' if debug else '',
}, },
'api': {
'level': 'DEBUG' if debug else 'INFO',
'class': 'moulinette.interfaces.api.APIQueueHandler',
},
'file': { 'file': {
'class': 'logging.FileHandler', 'class': 'logging.FileHandler',
'formatter': 'precise', 'formatter': 'precise',
@ -130,92 +132,43 @@ def init_logging(interface="cli",
'loggers': { 'loggers': {
'yunohost': { 'yunohost': {
'level': 'DEBUG', 'level': 'DEBUG',
'handlers': ['file', 'tty'] if not quiet else ['file'], 'handlers': ['file', interface] if not quiet else ['file'],
'propagate': False, 'propagate': False,
}, },
'moulinette': { 'moulinette': {
'level': 'DEBUG', 'level': 'DEBUG',
'handlers': [], 'handlers': ['file', interface] if not quiet else ['file'],
'propagate': True,
},
'moulinette.interface': {
'level': 'DEBUG',
'handlers': ['file', 'tty'] if not quiet else ['file'],
'propagate': False,
},
'moulinette.core': {
'level': 'DEBUG' if debug else 'ERROR',
'handlers': ['file', 'tty'] if not quiet else ['file'],
'propagate': False, 'propagate': False,
}, },
}, },
'root': { 'root': {
'level': 'DEBUG', 'level': 'DEBUG',
'handlers': ['file', 'tty'] if debug else ['file'], 'handlers': ['file', interface] if debug else ['file'],
}, },
}) }
# ####################################################################### #
# Logging configuration for CLI (or any other interface than api...) #
if interface != "api":
logging_configuration["main_logger"] = "yunohost"
configure_logging(logging_configuration)
# Logging configuration for API # # Logging configuration for API #
# ####################################################################### #
else: else:
configure_logging({ # We use a WatchedFileHandler instead of regular FileHandler to possibly support log rotation etc
'version': 1, logging_configuration["handlers"]["file"]["class"] = 'logging.handlers.WatchedFileHandler'
'disable_existing_loggers': True,
'formatters': { # This is for when launching yunohost-api in debug mode, we want to display stuff in the console
'console': { if debug:
'format': '%(relativeCreated)-5d %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s' logging_configuration["handlers"]["console"] = {
},
'precise': {
'format': '%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s'
},
},
'filters': {
'action': {
'()': 'moulinette.utils.log.ActionFilter',
},
},
'handlers': {
'api': {
'level': 'DEBUG' if debug else 'INFO',
'class': 'moulinette.interfaces.api.APIQueueHandler',
},
'file': {
'class': 'logging.handlers.WatchedFileHandler',
'formatter': 'precise',
'filename': logfile,
'filters': ['action'],
},
'console': {
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
'formatter': 'console', 'formatter': 'console',
'stream': 'ext://sys.stdout', 'stream': 'ext://sys.stdout',
'filters': ['action'], 'filters': ['action'],
}, },
}, logging_configuration["loggers"]["yunohost"]["handlers"].append("console")
'loggers': { logging_configuration["loggers"]["moulinette"]["handlers"].append("console")
'yunohost': { logging_configuration["root"]["handlers"].append("console")
'level': 'DEBUG',
'handlers': ['file', 'api'] + (['console'] if debug else []), configure_logging(logging_configuration)
'propagate': False,
},
'moulinette': {
'level': 'DEBUG',
'handlers': [],
'propagate': True,
},
'moulinette.core': {
'level': 'DEBUG' if debug else 'ERROR',
'handlers': ['file', 'tty'] if not quiet else ['file'],
'propagate': False,
},
'moulinette.interface.api': {
'level': 'DEBUG',
'handlers': [],
'propagate': True,
},
},
'root': {
'level': 'DEBUG',
'handlers': ['file'] + (['console'] if debug else []),
},
})