Merge pull request #2 from gergelypolonkai/autoconfigure

Configure automatically with current_app
This commit is contained in:
Gergely Polonkai 2018-01-30 14:40:52 +01:00 committed by GitHub
commit 41bcdc4b0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 7 deletions

View File

@ -32,7 +32,7 @@ no value is present in the message record.
import logging import logging
from flask import has_request_context, request from flask import has_request_context, request, current_app, has_app_context
__version_info__ = ('0', '0', '1') __version_info__ = ('0', '0', '1')
__version__ = '.'.join(__version_info__) __version__ = '.'.join(__version_info__)
@ -129,6 +129,9 @@ class FlaskExtraLogger(logging.getLoggerClass()):
super(FlaskExtraLogger, self).__init__(*args, **kwargs) super(FlaskExtraLogger, self).__init__(*args, **kwargs)
def _log(self, *args, **kwargs): def _log(self, *args, **kwargs):
if has_app_context() and self.app is None:
self.init_app(current_app)
if 'extra' not in kwargs: if 'extra' not in kwargs:
kwargs['extra'] = {} kwargs['extra'] = {}
@ -172,6 +175,8 @@ class FlaskExtraLogger(logging.getLoggerClass()):
reserved for internal use reserved for internal use
""" """
self.app = app
app.config.setdefault('FLASK_LOGGING_EXTRAS_KEYWORDS', {}) app.config.setdefault('FLASK_LOGGING_EXTRAS_KEYWORDS', {})
app.config.setdefault('FLASK_LOGGING_EXTRAS_BLUEPRINT', app.config.setdefault('FLASK_LOGGING_EXTRAS_BLUEPRINT',
(None, '<app>', '<not a request>')) (None, '<app>', '<not a request>'))

View File

@ -8,7 +8,7 @@ Flask-Logging-Extras provides extra logging functionality for Flask apps.
from setuptools import setup from setuptools import setup
setup(name='Flask-Logging-Extras', setup(name='Flask-Logging-Extras',
version='0.1.0', version='0.1.1',
url='https://github.com/gergelypolonkai/flask-logging-extras', url='https://github.com/gergelypolonkai/flask-logging-extras',
license='MIT', license='MIT',
author='Gergely Polonkai', author='Gergely Polonkai',

View File

@ -191,10 +191,10 @@ class LoggerBlueprintTestCase(TestCase):
self.stream = ListStream() self.stream = ListStream()
formatter = logging.Formatter(fmt) formatter = logging.Formatter(fmt)
handler = TestingStreamHandler(stream=self.stream) self.handler = TestingStreamHandler(stream=self.stream)
handler.setLevel(logging.DEBUG) self.handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter) self.handler.setFormatter(formatter)
app.logger.addHandler(handler) app.logger.addHandler(self.handler)
app.logger.setLevel(logging.DEBUG) app.logger.setLevel(logging.DEBUG)
bp = Blueprint('test_blueprint', 'test_bpg13') bp = Blueprint('test_blueprint', 'test_bpg13')
@ -218,6 +218,15 @@ class LoggerBlueprintTestCase(TestCase):
def tearDown(self): def tearDown(self):
logging.setLoggerClass(self.original_logger_class) logging.setLoggerClass(self.original_logger_class)
def test_autoconfig(self):
logger = logging.getLogger('test')
logger.addHandler(self.handler)
with self.app.app_context():
logger.warning('Hello')
self.assertEqual('<norequest> Hello\n', self.stream.lines[-1])
def test_request_log(self): def test_request_log(self):
self.client.get('/app') self.client.get('/app')
self.assertEqual('<app> Message\n', self.stream.lines[-1]) self.assertEqual('<app> Message\n', self.stream.lines[-1])

View File

@ -1,5 +1,5 @@
[tox] [tox]
envlist = py27, py33, py34, py35, py36 envlist = py27, py34, py35, py36
[testenv] [testenv]
commands = commands =