Source code for ecpi.logger_ecpi

'''
Created on 29 juil. 2019

@author: Colley Jean-Marc
'''

import logging
import datetime
import configparser
from ecpi.common import add_path_eclairs


TEMPLATE = '%(asctime)s#%(levelname)s[%(lineno)d]%(name)s %(funcName)s(): %(message)s'
DICT_LEVELS = {
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL
}


[docs]def check_logger_level(str_level): """Check the validity of the logger level specified in simu_config and choose the appropriate one. """ try: return DICT_LEVELS[str_level] except KeyError: return DICT_LEVELS['warning']
[docs]def return_level(file): """Return logger level from file parameters ecpi, ie key logger_level in section general """ cfg = configparser.ConfigParser( interpolation=configparser.ExtendedInterpolation()) try: cfg.read(file) except : return 'info' try: dict_section = cfg._sections['general'] level = dict_section['logger_level'] return level except: return 'info'
[docs]class ECPILogger(object): """Logger for ECPI """ def __init__(self, log_level='info', log_dest_path=None, stream=True, root='ecpi'): """**Constructor** :param cfg: configuration container from the configuration file :type cfg: ConfigParser :param log_dest_path: path where the log file will be created :type log_dest_path: str """ self.logger = logging.getLogger(root) self.logger.setLevel(DICT_LEVELS[log_level]) self.log_level = log_level formatter = logging.Formatter(TEMPLATE) if log_dest_path is not None: fh = logging.FileHandler(log_dest_path, mode='w') fh.setLevel(DICT_LEVELS[self.log_level]) fh.setFormatter(formatter) self.logger.addHandler(fh) self.filehandler = fh if stream: ch = logging.StreamHandler() ch.setLevel(DICT_LEVELS[self.log_level]) ch.setFormatter(formatter) self.logger.addHandler(ch) self.streamhandler = ch
[docs] def message_start(self): self.logger.info(f'===========> Start at {get_now_string()}')
[docs] def message_end(self): self.logger.info(f'===========> End at {get_now_string()}')
[docs]def get_logger_path(pfile): """ .. warning: dead code """ p_ecpi = __file__.find('/ecpi/') if p_ecpi is None: return None g_str = pfile[p_ecpi+1:-3].replace('/', '.') return g_str
[docs]def get_now_string(): """ .. warning: dead code """ return datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')