Skip to content
Snippets Groups Projects
logger.py 3.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • David Trattnig's avatar
    David Trattnig committed
    #
    
    David Trattnig's avatar
    David Trattnig committed
    # Aura Engine API (https://gitlab.servus.at/aura/engine-api)
    
    David Trattnig's avatar
    David Trattnig committed
    #
    
    David Trattnig's avatar
    David Trattnig committed
    # Copyright (C) 2020 - The Aura Engine Team.
    
    David Trattnig's avatar
    David Trattnig committed
    #
    # 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
    
    
    
    class AuraLogger:
    
    David Trattnig's avatar
    David Trattnig committed
        """
        AuraLogger Class
    
        Logger for all Aura Engine components. The default
        logger is `AuraEngine`. Other loggers are defined
    
        by passing a custom name on instantiation.
    
    
    David Trattnig's avatar
    David Trattnig committed
        The logger respects the log-level as defined in the
        engine's configuration file.
        """
    
    David Trattnig's avatar
    David Trattnig committed
        config = None
        logger = None
    
    
        def __init__(self, config, name="engine-api"):
    
    David Trattnig's avatar
    David Trattnig committed
            """
    
            Constructor to create a new logger defined by
    
    David Trattnig's avatar
    David Trattnig committed
            the passed name.
    
            Args:
                name (String):  The name of the logger
            """
            self.config = config
    
            self.create_logger(name)
    
    David Trattnig's avatar
    David Trattnig committed
    
    
        def get_log_level(self):
            """
            Retrieve the configured log level (default=INFO).
            """
            lvl = self.config.get("loglevel")
            mapping = {
                "debug": logging.DEBUG,
                "info": logging.INFO,
                "warning": logging.WARNING,
                "error": logging.ERROR,
                "critical": logging.CRITICAL,
            }
    
            log_level = mapping.get(lvl)
            if not log_level:
                print("No log level configured. Using INFO.")
                log_level = logging.INFO
            print(f"Setting log level {log_level} ({lvl})")
            return log_level
    
        def create_logger(self, name):
    
    David Trattnig's avatar
    David Trattnig committed
            """
            Creates the logger instance for the given name.
    
            Args:
                name (String):  The name of the logger
            """
    
            lvl = self.get_log_level()
    
    David Trattnig's avatar
    David Trattnig committed
    
            # create logger
            self.logger = logging.getLogger(name)
            self.logger.setLevel(lvl)
    
            if not self.logger.hasHandlers():
                # create file handler for logger
    
                file_path = self.config.get("logdir") + "/" + name + ".log"
                file_handler = logging.FileHandler(file_path)
    
    David Trattnig's avatar
    David Trattnig committed
                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.debug("Added handlers to logger")
    
    David Trattnig's avatar
    David Trattnig committed
            else:
    
                self.logger.debug("Reused logger")