Update documentation, and a bit of the functionality

This commit is contained in:
2017-01-16 08:56:58 +01:00
parent 67e73a02c5
commit 845a314250
5 changed files with 272 additions and 4 deletions

View File

@@ -16,6 +16,34 @@ __copyright__ = '(c) 2015 GT2'
class FlaskExtraLogger(logging.getLoggerClass()):
"""
A logger class that is capable of adding extra keywords to log formatters
Usage:
.. code-block:: python
import logging
from flask_logging_extras import register_logger_class
# This must be done before the app is initialized!
register_logger_class(cls=FlaskExtraLogger)
app = Flask(__name__)
app.config['FLASK_LOGGING_EXTRAS_KEYWORDS'] = ['category']
app.logger.init_app()
formatter = logging.Formatter(
'[%(asctime)s] [%(levelname)s] [%(category)s] %(message)s')
handler = logging.FileHandler('app.log', mode='a')
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
app.logger.addHandler(handler)
app.logger.info('The message', category='my category')
# This will produce something like this in app.log:
# [TIMESTAMP2017-01-16 08:44:48.944] [INFO] [my category] The message
"""
def __init__(self, *args, **kwargs):
@@ -38,11 +66,25 @@ class FlaskExtraLogger(logging.getLoggerClass()):
super(FlaskExtraLogger, self)._log(*args, **kwargs)
def init_app(self, app):
self.app = app
"""
Intialize the logger class with a Flask application
self.app.config.setdefault('FLASK_LOGGING_EXTRAS_KEYWORDS', [])
The class reads its necessary configuration from the config of this
application.
for kw in self.app.config['FLASK_LOGGING_EXTRAS_KEYWORDS']:
If the application doesnt call this, or doesnt have the
`FLASK_LOGGING_EXTRAS_KEYWORDS` in its config, no extra
functionality will be added.
:param app: a Flask application
:type app: Flask
:raises ValueError: if the app tries to register a keyword that is
reserved for internal use
"""
app.config.setdefault('FLASK_LOGGING_EXTRAS_KEYWORDS', [])
for kw in app.config['FLASK_LOGGING_EXTRAS_KEYWORDS']:
if kw in ['exc_info', 'extra', 'stack_info']:
raise ValueError(
'"{keyword}" member of FLASK_LOGGING_EXTRAS_KEYWORDS is '
@@ -53,15 +95,30 @@ def register_logger_class(cls=FlaskExtraLogger):
"""
Register a new logger class
It is effectively a wrapper around logging.setLoggerClass(), with an
It is effectively a wrapper around `logging.setLoggerClass()`, with an
added check to make sure the class can be used as a logger.
To use the extra features of the logger class in a Flask app, you must
call it before the app is instantiated.
This function returns the logger class that was the default before
calling. This might be useful if you only want to use `cls` in the
Flask app object, but not anywhere else in your code. In this case,
simply call `register_logger_class()` again with the return value from
the first invocation.
:param cls: a logger class to register as the default one
:type cls: class(logging.Logger)
:returns: the old default logger class
:rtype: class
:raises TypeError: if the class is not a subclass of `logging.Logger`
"""
if not issubclass(cls, logging.Logger):
raise TypeError(
"The logger class must be a subclass of logging.Logger!")
old_class = logging.get_logger_class()
logging.setLoggerClass(cls)
return old_class