summaryrefslogtreecommitdiffstats
path: root/app/utils/logging/full_logger.py
blob: a88f00e1db96f2eeaab2ebce2ea3e1a846de8f3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################
import logging
import logging.handlers

from utils.logging.logger import Logger
from utils.logging.mongo_logging_handler import MongoLoggingHandler


class FullLogger(Logger):

    def __init__(self, env: str = None, log_file: str = None,
                 level: str = Logger.default_level):
        super().__init__(logger_name="{}-Full".format(self.PROJECT_NAME),
                         level=level)

        # Console handler
        self.add_handler(logging.StreamHandler())

        # Message handler
        self.add_handler(MongoLoggingHandler(env, self.level))

        # File handler
        if log_file:
            self.add_handler(logging.handlers.WatchedFileHandler(log_file))

    # Make sure we update MessageHandler with new env
    def set_env(self, env):
        super().set_env(env)

        defined_handler = next(
            filter(
                lambda handler: handler.__class__ == MongoLoggingHandler.__class__,
                self.log.handlers
            ), None)

        if defined_handler:
            defined_handler.env = env
        else:
            self.add_handler(MongoLoggingHandler(env, self.level))