1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- # -*- coding: utf-8 -*-
- """
- @author: jürgen.pannosch, tanja.zolotareva
- """
- import sys
- import os
- import logging
- from datetime import datetime
- sys.path.append(os.getcwd())
- class Log():
- '''
- '''
- pass
- def __init__(self, name: str = None,
- log_file: str = None,
- log_level: str = "ERROR",
- stdout_log_level: str = "INFO",
- print_to_stdout: bool = True):
- """Sets the log level and the path where the log file is stored
- :param log_file: Path to the log file.
- :param log_level: Log level."""
- if name is None:
- name = ''
- self._logger = logging.getLogger(name)
-
- self._logger.setLevel("DEBUG")
- if (self._logger.hasHandlers()):
- self._logger.handlers.clear()
- if log_file is None:
- log_file = os.path.join(".", "logs", str(datetime.today().date()) + ".log")
- assert(isinstance(log_file, str)),\
- "Parameter 'log_path' must be of string type"
- os.makedirs(os.path.dirname(log_file), exist_ok=True)
- formatter = logging.Formatter(
- '\n %(name)s %(asctime)s %(levelname)s %(message)s')
- file_handler = logging.FileHandler(log_file)
- file_handler.setFormatter(formatter)
- file_handler.setLevel(log_level)
- self._logger.addHandler(file_handler)
- if print_to_stdout:
- stream_handler = logging.StreamHandler(sys.stdout)
- stream_handler.setFormatter(formatter)
- stream_handler.setLevel(stdout_log_level)
- self._logger.addHandler(stream_handler)
- # self._logger.setLevel(log_level)
- def info(self, message: str):
- self._logger.info(message)
- def warning(self, message: str):
- self._logger.warning(message)
- def error(self, message: str):
- self._logger.error(message)
- def log_and_raise_error(self, message):
- '''
- '''
- self._logger.error(message, exc_info=True)
- raise Exception(message)
- def log_and_raise_error_stack_info(self, message):
- '''
- '''
- self._logger.error(message, exc_info=True, stack_info=True)
- raise Exception(message)
- def log_and_raise_warning(self, message):
- '''
- '''
- self._logger.warning(message)
- raise Warning(message)
|