From 0247b3a4b5c3389347bfa05aaa293ac24ee54842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Trellu?= Date: Fri, 29 Oct 2021 01:29:22 -0400 Subject: [PATCH] [log_format] Add an option to change the log format (#3016) * [log_format] Add an option to change the log format Hacing an option to change the log format could be useful when logs are shipped into an aggregator such as Elasticsearch. The current format is very hard to parse. * [log_format] Set default Formatter Because mycroft.configuration.Configuration() class import LOG class, a default Formatter have to be defined before import the mycroft.configuration.Configuration() class. --- mycroft/configuration/mycroft.conf | 8 ++++++++ mycroft/util/log.py | 4 ++++ test/unittests/util/commented.json | 1 + test/unittests/util/plain.json | 1 + 4 files changed, 14 insertions(+) diff --git a/mycroft/configuration/mycroft.conf b/mycroft/configuration/mycroft.conf index b6922259aa..67dc2aee7b 100644 --- a/mycroft/configuration/mycroft.conf +++ b/mycroft/configuration/mycroft.conf @@ -263,6 +263,14 @@ // If not defined, the default log level is INFO. //"log_level": "INFO", + // Format of logs to store. + // NOTE: This configuration setting is special and can only be changed in the + // SYSTEM or USER configuration file, it will not be read if defined in the + // DEFAULT (here) or in the REMOTE mycroft config. + // If not defined, the default log format is: + // {asctime} | {levelname:8} | {process:5} | {name} | {message} + //"log_format": "{asctime} | {levelname:8} | {process:5} | {name} | {message}", + // Messagebus types that will NOT be output to logs "ignore_logs": ["enclosure.mouth.viseme", "enclosure.mouth.display"], diff --git a/mycroft/util/log.py b/mycroft/util/log.py index b000f0ff01..5894784e89 100644 --- a/mycroft/util/log.py +++ b/mycroft/util/log.py @@ -87,6 +87,10 @@ class LOG: cls.handler.setFormatter(formatter) config = mycroft.configuration.Configuration.get(remote=False) + if config.get('log_format'): + formatter = logging.Formatter(config.get('log_format'), style='{') + cls.handler.setFormatter(formatter) + cls.level = logging.getLevelName(config.get('log_level', 'INFO')) # Enable logging in external modules diff --git a/test/unittests/util/commented.json b/test/unittests/util/commented.json index 00a581f6df..b7fed8a813 100644 --- a/test/unittests/util/commented.json +++ b/test/unittests/util/commented.json @@ -85,6 +85,7 @@ "test": false }, "log_level": "DEBUG", + "log_format": "{asctime} {levelname} {process} {name} {message}", "ignore_logs": ["enclosure.mouth.viseme"], "session": { "ttl": 180 diff --git a/test/unittests/util/plain.json b/test/unittests/util/plain.json index eec84631d8..746fc7bb40 100644 --- a/test/unittests/util/plain.json +++ b/test/unittests/util/plain.json @@ -67,6 +67,7 @@ "test": false }, "log_level": "DEBUG", + "log_format": "{asctime} {levelname} {process} {name} {message}", "ignore_logs": ["enclosure.mouth.viseme"], "session": { "ttl": 180