From 87057cd2e6adc90d90cc952e9bcc8efc352326fb Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Mon, 22 Jun 2020 19:12:03 +0200 Subject: [PATCH] Default logger. --- src/base/logger.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/base/logger.py diff --git a/src/base/logger.py b/src/base/logger.py new file mode 100644 index 0000000..d1d2673 --- /dev/null +++ b/src/base/logger.py @@ -0,0 +1,92 @@ +# +# Aura Engine (https://gitlab.servus.at/aura/engine) +# +# Copyright (C) 2017-2020 - The Aura Engine Team. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import logging + +from src.base.config import AuraConfig + + +class AuraLogger(): + """ + AuraLogger Class + + Logger for all Aura Engine components. The default + logger is `AuraEngine`. Other loggers are defined + by passing a custom name on instantiation. + + The logger respects the log-level as defined in the + engine's configuration file. + """ + config = None + logger = None + + + + def __init__(self, config, name="AuraEngine"): + """ + Constructor to create a new logger defined by + the passed name. + + Args: + name (String): The name of the logger + """ + self.config = config + self.__create_logger(name) + + + + def __create_logger(self, name): + """ + Creates the logger instance for the given name. + + Args: + name (String): The name of the logger + """ + lvl = self.config.get("loglevel") + + # create logger + self.logger = logging.getLogger(name) + self.logger.setLevel(lvl) + + if not self.logger.hasHandlers(): + # create file handler for logger + file_handler = logging.FileHandler(self.config.get("logdir") + "/"+name+".log") + file_handler.setLevel(lvl) + + # create stream handler for logger + stream_handler = logging.StreamHandler() + stream_handler.setLevel(lvl) + + # set format of log + datepart = "%(asctime)s:%(name)s:%(levelname)s" + message = " - %(message)s - " + filepart = "[%(filename)s:%(lineno)s-%(funcName)s()]" + formatter = logging.Formatter(datepart + message + filepart) + + # set log of handlers + file_handler.setFormatter(formatter) + stream_handler.setFormatter(formatter) + + # add handlers to the logger + self.logger.addHandler(file_handler) + self.logger.addHandler(stream_handler) + + self.logger.critical("ADDED HANDLERS") + else: + self.logger.critical("REUSED LOGGER") \ No newline at end of file -- GitLab