logger.py 2.75 KB
Newer Older
1
#
David Trattnig's avatar
David Trattnig committed
2
# Aura Engine (https://gitlab.servus.at/aura/engine)
3
#
David Trattnig's avatar
David Trattnig committed
4
# Copyright (C) 2017-2020 - The Aura Engine Team.
5
#
6
7
8
9
# 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.
10
#
11
12
13
14
# 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.
15
#
16
17
# 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/>.
18

David Trattnig's avatar
David Trattnig committed
19

20
21
import logging

David Trattnig's avatar
David Trattnig committed
22
from modules.base.config import AuraConfig
23

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
24

25
class AuraLogger():
26
27
28
29
30
31
32
33
34
35
    """
    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.
    """
36
    config = None
37
38
    logger = None

39
40
41
42
43
44
45
46
47
48


    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
        """
49
        self.config = config
50
51
        self.__create_logger(name)

52

53
54
55

    def __create_logger(self, name):
        """
56
57
58
59
        Creates the logger instance for the given name.

        Args:
            name (String):  The name of the logger
60
61
62
63
64
65
66
        """
        lvl = self.config.get("loglevel")

        # create logger
        self.logger = logging.getLogger(name)
        self.logger.setLevel(lvl)

67
68
        if not self.logger.hasHandlers():
            # create file handler for logger
69
            file_handler = logging.FileHandler(self.config.get("logdir") + "/"+name+".log")
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
            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")