2018-02-04 00:19:16 +00:00
|
|
|
# pragma pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
"""
|
|
|
|
This module contains the class for logger and logging messages
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2018-03-25 19:05:10 +00:00
|
|
|
from typing import Optional
|
2018-02-04 00:19:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Logger(object):
|
|
|
|
"""
|
|
|
|
Logging class
|
|
|
|
"""
|
2018-03-25 19:05:10 +00:00
|
|
|
def __init__(self, name, level: Optional[int] = None) -> None:
|
2018-02-04 00:19:16 +00:00
|
|
|
"""
|
|
|
|
Init the logger class
|
|
|
|
:param name: Name of the Logger scope
|
|
|
|
:param level: Logger level that should be used
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
self.name = name
|
2018-02-09 07:35:38 +00:00
|
|
|
self.logger = None
|
|
|
|
|
2018-03-25 19:05:10 +00:00
|
|
|
self.level = level or logging.getLogger().getEffectiveLevel()
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2018-02-04 00:19:16 +00:00
|
|
|
self._init_logger()
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def _init_logger(self) -> None:
|
2018-02-04 00:19:16 +00:00
|
|
|
"""
|
|
|
|
Setup the bot logger configuration
|
2018-02-09 07:35:38 +00:00
|
|
|
:return: None
|
2018-02-04 00:19:16 +00:00
|
|
|
"""
|
|
|
|
logging.basicConfig(
|
|
|
|
level=self.level,
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
)
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
self.logger = self.get_logger()
|
|
|
|
self.set_level(self.level)
|
|
|
|
|
2018-02-04 00:19:16 +00:00
|
|
|
def get_logger(self) -> logging.RootLogger:
|
|
|
|
"""
|
|
|
|
Return the logger instance to use for sending message
|
|
|
|
:return: the logger instance
|
|
|
|
"""
|
|
|
|
return logging.getLogger(self.name)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
def set_name(self, name: str) -> logging.RootLogger:
|
|
|
|
"""
|
|
|
|
Set the name of the logger
|
|
|
|
:param name: Name of the logger
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.logger = self.get_logger()
|
|
|
|
return self.logger
|
|
|
|
|
|
|
|
def set_level(self, level) -> None:
|
|
|
|
"""
|
|
|
|
Set the level of the logger
|
|
|
|
:param level:
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
self.level = level
|
|
|
|
self.logger.setLevel(self.level)
|
|
|
|
|
|
|
|
def set_format(self, log_format: str, propagate: bool = False) -> None:
|
|
|
|
"""
|
|
|
|
Set a new logging format
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
|
|
|
|
len_handlers = len(self.logger.handlers)
|
|
|
|
if len_handlers:
|
|
|
|
self.logger.removeHandler(handler)
|
|
|
|
|
|
|
|
handler.setFormatter(logging.Formatter(log_format))
|
|
|
|
self.logger.addHandler(handler)
|
|
|
|
|
|
|
|
self.logger.propagate = propagate
|