Configure automatically with current_app
This commit is contained in:
parent
fbebc6ca26
commit
b9c3128315
@ -32,7 +32,7 @@ no value is present in the message record.
|
||||
|
||||
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__ = '.'.join(__version_info__)
|
||||
@ -129,6 +129,9 @@ class FlaskExtraLogger(logging.getLoggerClass()):
|
||||
super(FlaskExtraLogger, self).__init__(*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:
|
||||
kwargs['extra'] = {}
|
||||
|
||||
@ -172,6 +175,8 @@ class FlaskExtraLogger(logging.getLoggerClass()):
|
||||
reserved for internal use
|
||||
"""
|
||||
|
||||
self.app = app
|
||||
|
||||
app.config.setdefault('FLASK_LOGGING_EXTRAS_KEYWORDS', {})
|
||||
app.config.setdefault('FLASK_LOGGING_EXTRAS_BLUEPRINT',
|
||||
(None, '<app>', '<not a request>'))
|
||||
|
2
setup.py
2
setup.py
@ -8,7 +8,7 @@ Flask-Logging-Extras provides extra logging functionality for Flask apps.
|
||||
from setuptools import setup
|
||||
|
||||
setup(name='Flask-Logging-Extras',
|
||||
version='0.1.0',
|
||||
version='0.1.1',
|
||||
url='https://github.com/gergelypolonkai/flask-logging-extras',
|
||||
license='MIT',
|
||||
author='Gergely Polonkai',
|
||||
|
@ -191,10 +191,10 @@ class LoggerBlueprintTestCase(TestCase):
|
||||
self.stream = ListStream()
|
||||
|
||||
formatter = logging.Formatter(fmt)
|
||||
handler = TestingStreamHandler(stream=self.stream)
|
||||
handler.setLevel(logging.DEBUG)
|
||||
handler.setFormatter(formatter)
|
||||
app.logger.addHandler(handler)
|
||||
self.handler = TestingStreamHandler(stream=self.stream)
|
||||
self.handler.setLevel(logging.DEBUG)
|
||||
self.handler.setFormatter(formatter)
|
||||
app.logger.addHandler(self.handler)
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
bp = Blueprint('test_blueprint', 'test_bpg13')
|
||||
@ -218,6 +218,15 @@ class LoggerBlueprintTestCase(TestCase):
|
||||
def tearDown(self):
|
||||
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):
|
||||
self.client.get('/app')
|
||||
self.assertEqual('<app> Message\n', self.stream.lines[-1])
|
||||
|
Loading…
Reference in New Issue
Block a user