remove freqtrade/logger.py

This commit is contained in:
gcarq 2018-03-25 21:41:25 +02:00
parent fa7f74b4bc
commit f374a062e1
5 changed files with 3 additions and 192 deletions

View File

@ -1,82 +0,0 @@
# pragma pylint: disable=too-few-public-methods
"""
This module contains the class for logger and logging messages
"""
import logging
from typing import Optional
class Logger(object):
"""
Logging class
"""
def __init__(self, name, level: Optional[int] = None) -> 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.logger = None
self.level = level or logging.getLogger().getEffectiveLevel()
self._init_logger()
def _init_logger(self) -> None:
"""
Setup the bot logger configuration
:return: None
"""
logging.basicConfig(
level=self.level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
self.logger = self.get_logger()
self.set_level(self.level)
def get_logger(self) -> logging.RootLogger:
"""
Return the logger instance to use for sending message
:return: the logger instance
"""
return logging.getLogger(self.name)
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

View File

@ -296,12 +296,9 @@ def start(args: Namespace) -> None:
:param args: Cli args from Arguments() :param args: Cli args from Arguments()
:return: None :return: None
""" """
# Initialize logger
logger.info('Starting freqtrade in Backtesting mode')
# Initialize configuration # Initialize configuration
config = setup_configuration(args) config = setup_configuration(args)
logger.info('Starting freqtrade in Backtesting mode')
# Initialize backtesting object # Initialize backtesting object
backtesting = Backtesting(config) backtesting = Backtesting(config)

View File

@ -25,7 +25,6 @@ from pandas import DataFrame
import freqtrade.vendor.qtpylib.indicators as qtpylib import freqtrade.vendor.qtpylib.indicators as qtpylib
from freqtrade.arguments import Arguments from freqtrade.arguments import Arguments
from freqtrade.configuration import Configuration from freqtrade.configuration import Configuration
from freqtrade.logger import Logger
from freqtrade.optimize import load_data from freqtrade.optimize import load_data
from freqtrade.optimize.backtesting import Backtesting from freqtrade.optimize.backtesting import Backtesting
from user_data.hyperopt_conf import hyperopt_optimize_conf from user_data.hyperopt_conf import hyperopt_optimize_conf
@ -592,13 +591,11 @@ def start(args: Namespace) -> None:
logging.getLogger('hyperopt.mongoexp').setLevel(logging.WARNING) logging.getLogger('hyperopt.mongoexp').setLevel(logging.WARNING)
logging.getLogger('hyperopt.tpe').setLevel(logging.WARNING) logging.getLogger('hyperopt.tpe').setLevel(logging.WARNING)
# Initialize logger
logger = Logger(__name__).get_logger()
logger.info('Starting freqtrade in Hyperopt mode')
# Initialize configuration # Initialize configuration
# Monkey patch the configuration with hyperopt_conf.py # Monkey patch the configuration with hyperopt_conf.py
configuration = Configuration(args) configuration = Configuration(args)
logger.info('Starting freqtrade in Hyperopt mode')
optimize_config = hyperopt_optimize_conf() optimize_config = hyperopt_optimize_conf()
config = configuration._load_common_config(optimize_config) config = configuration._load_common_config(optimize_config)
config = configuration._load_backtesting_config(config) config = configuration._load_backtesting_config(config)

View File

@ -50,7 +50,6 @@ def test_start(mocker, default_conf, caplog) -> None:
Test start() function Test start() function
""" """
start_mock = MagicMock() start_mock = MagicMock()
mocker.patch('freqtrade.logger.Logger.set_format', MagicMock())
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock) mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
mocker.patch('freqtrade.configuration.open', mocker.mock_open( mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf) read_data=json.dumps(default_conf)
@ -169,7 +168,6 @@ def test_fmin_best_results(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock()) mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value=fmin_result) mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value=fmin_result)
mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf) mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf)
mocker.patch('freqtrade.logger.Logger.set_format', MagicMock())
Strategy({'strategy': 'default_strategy'}) Strategy({'strategy': 'default_strategy'})
hyperopt = Hyperopt(conf) hyperopt = Hyperopt(conf)
@ -214,7 +212,6 @@ def test_fmin_throw_value_error(mocker, default_conf, caplog) -> None:
conf.update({'timerange': None}) conf.update({'timerange': None})
conf.update({'spaces': 'all'}) conf.update({'spaces': 'all'})
mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf) mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf)
mocker.patch('freqtrade.logger.Logger.set_format', MagicMock())
Strategy({'strategy': 'default_strategy'}) Strategy({'strategy': 'default_strategy'})
hyperopt = Hyperopt(conf) hyperopt = Hyperopt(conf)
hyperopt.trials = create_trials(mocker) hyperopt.trials = create_trials(mocker)
@ -256,7 +253,6 @@ def test_resuming_previous_hyperopt_results_succeeds(mocker, default_conf) -> No
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock()) mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={}) mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf) mocker.patch('freqtrade.optimize.hyperopt.hyperopt_optimize_conf', return_value=conf)
mocker.patch('freqtrade.logger.Logger.set_format', MagicMock())
Strategy({'strategy': 'default_strategy'}) Strategy({'strategy': 'default_strategy'})
hyperopt = Hyperopt(conf) hyperopt = Hyperopt(conf)

View File

@ -1,97 +0,0 @@
"""
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('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() and Logger._init_logger()
:return: None
"""
logger = Logger(name='get_logger', level=logging.WARNING)
get_logger = logger.get_logger()
assert logger.logger is 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_set_name() -> None:
"""
Test Logger.set_name()
:return: None
"""
logger = Logger(name='set_name')
assert logger.name == 'set_name'
logger.set_name('set_name_new')
assert logger.name == 'set_name_new'
def test_set_level() -> None:
"""
Test Logger.set_name()
:return: None
"""
logger = Logger(name='Foo', level=logging.WARNING)
assert logger.level == logging.WARNING
assert logger.get_logger().level == logging.WARNING
logger.set_level(logging.INFO)
assert logger.level == logging.INFO
assert logger.get_logger().level == logging.INFO
def test_sending_msg(caplog) -> None:
"""
Test send a logging message
:return: None
"""
logger = Logger(name='sending_msg', level=logging.WARNING).get_logger()
logger.info('I am an INFO message')
assert('sending_msg', logging.INFO, 'I am an INFO message') not in caplog.record_tuples
logger.warning('I am an WARNING message')
assert ('sending_msg', logging.WARNING, 'I am an WARNING message') in caplog.record_tuples
def test_set_format(caplog) -> None:
"""
Test Logger.set_format()
:return: None
"""
log = Logger(name='set_format')
logger = log.get_logger()
logger.info('I am the first message')
assert ('set_format', logging.INFO, 'I am the first message') in caplog.record_tuples
log.set_format(log_format='%(message)s', propagate=True)
logger.info('I am the second message')
assert ('set_format', logging.INFO, 'I am the second message') in caplog.record_tuples