logger.py 1.4 KB
Newer Older
Monica Rainer's avatar
Monica Rainer committed
1
2
3
4
5
6
7
"""
Written by Avet Harutyunyan for the old GIANO pipeline
Implemented in the GIANOB DRS by Monica Rainer
"""

import logging
from drslib.config import CONFIG as CFG
8
from sys import stdout
Monica Rainer's avatar
Monica Rainer committed
9
10
11
12
13
14
15
16
17
18

class DrsLogger():
    def __init__(self):
        """The DrsLogger is constructed"""
        self.logger = logging.getLogger(CFG['APPNAME'] + '-Logger')
        #if not self.logger.handlers:
        self.logfile = CFG['LOG_FILE']
        self.filelogfmt = '%(asctime)s.%(msecs)d * %(message)s'
        self.conslogfmt = '%(message)s'
        self.logger.setLevel(logging.INFO)
19
20
21
22
        if CFG['LOGGER']['FILE']:
            self.addfilelogger()
        if CFG['LOGGER']['TERM']:
            self.addconsolelogger()
Monica Rainer's avatar
Monica Rainer committed
23
24
25
26
27
28
29
30
31
32
        
    def addfilelogger(self):
        """The logger to the LOG_FILE is defined"""
        file_handler = logging.FileHandler(self.logfile)
        formatter = logging.Formatter(fmt=self.filelogfmt, datefmt='%Y-%m-%d %H:%M:%S')
        file_handler.setFormatter(formatter)
        self.logger.addHandler(file_handler)
    
    def addconsolelogger(self):
        """The logger to the terminal is defined"""
33
        console_handler = logging.StreamHandler(stream=stdout)
Monica Rainer's avatar
Monica Rainer committed
34
35
36
37
38
39
40
        formatter = logging.Formatter(fmt=self.conslogfmt)
        console_handler.setFormatter(formatter)
        self.logger.addHandler(console_handler)
    
    def log(self, message):
        """The log is written"""
        self.logger.info(message)