aboutsummaryrefslogtreecommitdiffstats
path: root/app/utils/logging/full_logger.py
blob: 411ecebb2aebba6a7615b188dcbb8f09800f9cdf (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
###############################################################################
# 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 = [h for h in self.log.handlers
                           if isinstance(h, MongoLoggingHandler)]
        if defined_handler:
            defined_handler[0].env = env
        else:
            self.add_handler(MongoLoggingHandler(env, self.level))