Merge pull request #1119 from YunoHost/moulinette-logging

Simpler and more consistent logging initialization
This commit is contained in:
Alexandre Aubin 2021-02-28 17:34:18 +01:00 committed by GitHub
commit 997fd4d2f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 118 deletions

View file

@ -89,116 +89,79 @@ def init_logging(interface="cli", debug=False, quiet=False, logdir="/var/log/yun
if not os.path.isdir(logdir): if not os.path.isdir(logdir):
os.makedirs(logdir, 0o750) os.makedirs(logdir, 0o750)
# ####################################################################### # logging_configuration = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'console': {
'format': '%(relativeCreated)-5d %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s'
},
'tty-debug': {
'format': '%(relativeCreated)-4d %(fmessage)s'
},
'precise': {
'format': '%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s'
},
},
'filters': {
'action': {
'()': 'moulinette.utils.log.ActionFilter',
},
},
'handlers': {
'cli': {
'level': 'DEBUG' if debug else 'INFO',
'class': 'moulinette.interfaces.cli.TTYHandler',
'formatter': 'tty-debug' if debug else '',
},
'api': {
'level': 'DEBUG' if debug else 'INFO',
'class': 'moulinette.interfaces.api.APIQueueHandler',
},
'file': {
'class': 'logging.FileHandler',
'formatter': 'precise',
'filename': logfile,
'filters': ['action'],
},
},
'loggers': {
'yunohost': {
'level': 'DEBUG',
'handlers': ['file', interface] if not quiet else ['file'],
'propagate': False,
},
'moulinette': {
'level': 'DEBUG',
'handlers': ['file', interface] if not quiet else ['file'],
'propagate': False,
},
},
'root': {
'level': 'DEBUG',
'handlers': ['file', interface] if debug else ['file'],
},
}
# Logging configuration for CLI (or any other interface than api...) # # Logging configuration for CLI (or any other interface than api...) #
# ####################################################################### #
if interface != "api": if interface != "api":
configure_logging( configure_logging(logging_configuration)
{
"version": 1,
"main_logger": "yunohost",
"disable_existing_loggers": True,
"formatters": {
"tty-debug": {"format": "%(relativeCreated)-4d %(fmessage)s"},
"precise": {
"format": "%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s"
},
},
"filters": {
"action": {
"()": "moulinette.utils.log.ActionFilter",
},
},
"handlers": {
"tty": {
"level": "DEBUG" if debug else "INFO",
"class": "moulinette.interfaces.cli.TTYHandler",
"formatter": "tty-debug" if debug else "",
},
"file": {
"class": "logging.FileHandler",
"formatter": "precise",
"filename": logfile,
"filters": ["action"],
},
},
"loggers": {
"yunohost": {
"level": "DEBUG",
"handlers": ["file", "tty"] if not quiet else ["file"],
"propagate": False,
},
"moulinette": {
"level": "DEBUG",
"handlers": [],
"propagate": True,
},
"moulinette.interface": {
"level": "DEBUG",
"handlers": ["file", "tty"] if not quiet else ["file"],
"propagate": False,
},
},
"root": {
"level": "DEBUG",
"handlers": ["file", "tty"] if debug else ["file"],
},
}
)
# ####################################################################### #
# 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
{ logging_configuration["handlers"]["file"]["class"] = 'logging.handlers.WatchedFileHandler'
"version": 1,
"disable_existing_loggers": True, # This is for when launching yunohost-api in debug mode, we want to display stuff in the console
"formatters": { if debug:
"console": { logging_configuration["handlers"]["console"] = {
"format": "%(relativeCreated)-5d %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s" 'class': 'logging.StreamHandler',
}, 'formatter': 'console',
"precise": { 'stream': 'ext://sys.stdout',
"format": "%(asctime)-15s %(levelname)-8s %(name)s %(funcName)s - %(fmessage)s" 'filters': ['action'],
}, },
}, logging_configuration["loggers"]["yunohost"]["handlers"].append("console")
"filters": { logging_configuration["loggers"]["moulinette"]["handlers"].append("console")
"action": { logging_configuration["root"]["handlers"].append("console")
"()": "moulinette.utils.log.ActionFilter",
}, configure_logging(logging_configuration)
},
"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",
"formatter": "console",
"stream": "ext://sys.stdout",
"filters": ["action"],
},
},
"loggers": {
"yunohost": {
"level": "DEBUG",
"handlers": ["file", "api"] + (["console"] if debug else []),
"propagate": False,
},
"moulinette": {
"level": "DEBUG",
"handlers": [],
"propagate": True,
},
},
"root": {
"level": "DEBUG",
"handlers": ["file"] + (["console"] if debug else []),
},
}
)

View file

@ -551,9 +551,8 @@ class Diagnoser:
@staticmethod @staticmethod
def get_description(id_): def get_description(id_):
key = "diagnosis_description_" + id_ key = "diagnosis_description_" + id_
descr = m18n.n(key)
# If no description available, fallback to id # If no description available, fallback to id
return descr if descr != key else id_ return m18n.n(key) if m18n.key_exists(key) else id_
@staticmethod @staticmethod
def i18n(report, force_remove_html_tags=False): def i18n(report, force_remove_html_tags=False):

View file

@ -397,14 +397,11 @@ def _get_and_format_service_status(service, infos):
# If no description was there, try to get it from the .json locales # If no description was there, try to get it from the .json locales
if not description: if not description:
translation_key = "service_description_%s" % service
description = m18n.n(translation_key)
# If descrption is still equal to the translation key, translation_key = "service_description_%s" % service
# that mean that we don't have a translation for this string if m18n.key_exists(translation_key):
# that's the only way to test for that for now description = m18n.key_exists(translation_key)
# if we don't have it, uses the one provided by systemd else:
if description == translation_key:
description = str(raw_status.get("Description", "")) description = str(raw_status.get("Description", ""))
output = { output = {