logger.py 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
#  engine
#
#  Playout Daemon for autoradio project
#
#
#  Copyright (C) 2017-2018 Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>
#
#  This file is part of engine.
#
#  engine is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  engine 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with engine. If not, see <http://www.gnu.org/licenses/>.
#
24
25
26

import logging

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
27
from libraries.base.config import AuraConfig
28

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
29
30

class AuraLogger(AuraConfig):
31
32
33
    logger = None

    def __init__(self):
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
34
        super(AuraLogger, self).__init__()
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        self.__create_logger("AuraEngine")

    def __create_logger(self, name):
        """
        Creates the logger instance for AuraEngine
        :param name: LoggerName
        :return:
        """
        lvl = self.config.get("loglevel")

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

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        if not self.logger.hasHandlers():
            # create file handler for logger
            file_handler = logging.FileHandler(self.config.get("logdir") + "/engine.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")