Add a class Logger to manage the logging messages
This class will evolve later to support color logging. For now it is used to not repeat the logging configuration everywhere.
This commit is contained in:
parent
cf753d5c40
commit
3b9e828fa4
40
freqtrade/logger.py
Normal file
40
freqtrade/logger.py
Normal file
@ -0,0 +1,40 @@
|
||||
# pragma pylint: disable=too-few-public-methods
|
||||
|
||||
"""
|
||||
This module contains the class for logger and logging messages
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
|
||||
class Logger(object):
|
||||
"""
|
||||
Logging class
|
||||
"""
|
||||
def __init__(self, name='', level=logging.INFO) -> None:
|
||||
"""
|
||||
Init the logger class
|
||||
:param name: Name of the Logger scope
|
||||
:param level: Logger level that should be used
|
||||
:return: None
|
||||
"""
|
||||
self.name = name
|
||||
self.level = level
|
||||
self._init_logger()
|
||||
|
||||
def _init_logger(self) -> logging:
|
||||
"""
|
||||
Setup the bot logger configuration
|
||||
:return: logging object
|
||||
"""
|
||||
logging.basicConfig(
|
||||
level=self.level,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
)
|
||||
|
||||
def get_logger(self) -> logging.RootLogger:
|
||||
"""
|
||||
Return the logger instance to use for sending message
|
||||
:return: the logger instance
|
||||
"""
|
||||
return logging.getLogger(self.name)
|
53
freqtrade/tests/test_logger.py
Normal file
53
freqtrade/tests/test_logger.py
Normal file
@ -0,0 +1,53 @@
|
||||
"""
|
||||
Unit test file for logger.py
|
||||
"""
|
||||
|
||||
import logging
|
||||
from freqtrade.logger import Logger
|
||||
|
||||
|
||||
def test_logger_object() -> None:
|
||||
"""
|
||||
Test the Constants object has the mandatory Constants
|
||||
:return: None
|
||||
"""
|
||||
logger = Logger()
|
||||
assert logger.name == ''
|
||||
assert logger.level == 20
|
||||
assert logger.level is logging.INFO
|
||||
assert hasattr(logger, 'get_logger')
|
||||
|
||||
logger = Logger(name='Foo', level=logging.WARNING)
|
||||
assert logger.name == 'Foo'
|
||||
assert logger.name != ''
|
||||
assert logger.level == 30
|
||||
assert logger.level is logging.WARNING
|
||||
|
||||
|
||||
def test_get_logger() -> None:
|
||||
"""
|
||||
Test logger.get_logger()
|
||||
:return: None
|
||||
"""
|
||||
logger = Logger(name='Foo', level=logging.WARNING)
|
||||
get_logger = logger.get_logger()
|
||||
assert get_logger is not None
|
||||
assert hasattr(get_logger, 'debug')
|
||||
assert hasattr(get_logger, 'info')
|
||||
assert hasattr(get_logger, 'warning')
|
||||
assert hasattr(get_logger, 'critical')
|
||||
assert hasattr(get_logger, 'exception')
|
||||
|
||||
|
||||
def test_sending_msg(caplog) -> None:
|
||||
"""
|
||||
Test send a logging message
|
||||
:return: None
|
||||
"""
|
||||
logger = Logger(name='FooBar', level=logging.WARNING).get_logger()
|
||||
|
||||
logger.info('I am an INFO message')
|
||||
assert('FooBar', logging.INFO, 'I am an INFO message') not in caplog.record_tuples
|
||||
|
||||
logger.warning('I am an WARNING message')
|
||||
assert ('FooBar', logging.WARNING, 'I am an WARNING message') in caplog.record_tuples
|
Loading…
Reference in New Issue
Block a user