Add LOG_HANDLER to settings, so that other file handlers can be chosen

This commit is contained in:
Taito Horiuchi 2016-09-15 09:43:19 +03:00
parent 129421c7e3
commit 3823b032ff
2 changed files with 43 additions and 1 deletions

View File

@ -101,12 +101,25 @@ def configure_logging(settings=None, install_root_handler=True):
logging.root.addHandler(handler)
def _import_hander(name):
"""Return class from dotted name"""
import importlib
module_name, class_name = name.rsplit('.', 1)
module = importlib.import_module(module_name)
return getattr(module, class_name)
def _get_handler(settings):
""" Return a log handler object according to settings """
filename = settings.get('LOG_FILE')
if filename:
encoding = settings.get('LOG_ENCODING')
handler = logging.FileHandler(filename, encoding=encoding)
handler = settings.get('LOG_HANDLER')
if handler:
Handler = _import_hander(handler)
else:
Handler = logging.FileHandler
handler = Handler(filename, encoding=encoding)
elif settings.getbool('LOG_ENABLED'):
handler = logging.StreamHandler()
else:

View File

@ -0,0 +1,29 @@
from logging import FileHandler
from logging.handlers import TimedRotatingFileHandler
from scrapy.utils import log
import unittest
class TestCase(unittest.TestCase):
def test_import_handler(self):
"""Test function: _import_handler"""
name = 'logging.handlers.TimedRotatingFileHandler'
self.assertEqual(log._import_hander(name), TimedRotatingFileHandler)
def test_get_handler(self):
"""Test function: _get_handler with LOG_FILE"""
# First create log file path
import os
import tempfile
file_path = tempfile.mkstemp()[1]
settings = {'LOG_FILE': file_path, 'LOG_ENCODIUNG': 'utf-8', 'LOG_LEVEL': 'DEBUG'}
handler = log._get_handler(settings)
self.assertIsInstance(handler, FileHandler)
# Adding LOG_HANDLER to settings should update the handler
settings['LOG_HANDLER'] = 'logging.handlers.TimedRotatingFileHandler'
handler = log._get_handler(settings)
self.assertIsInstance(handler, TimedRotatingFileHandler)
# Remove log file
os.remove(file_path)