Welcome to Flask-Logging-Extended’s documentation!

Extra functionality for Flask logging

Flask-Logging-Extras is a Flask extension that plugs into the logging mechanism of Flask applications.

Flask-Logging-Extras requires you to set FLASK_LOGGING_EXTRAS_KEYWORDS to a dictionary value, where the dictionary key is a the key you can use in the log message format, and the value is a default value that is substituted if no value is present in the message record.

class flask_logging_extras.FlaskExtraLoggerFormatter(*args, **kwargs)[source]

A log formatter class that is capable of adding extra keywords to log formatters and logging the blueprint name

Usage:

import logging
from logging.config import dictConfig

dictConfig({
    'formatters': {
        'extras': {
            'format': '[%(asctime)s] [%(levelname)s] [%(category)s] [%(bp)s] %(message)s',
        },
    },
    'handlers': {
        'extras_handler': {
            'class': 'logging.FileHandler',
            'args': ('app.log', 'a'),
            'formatter': 'extras',
            'level': 'INFO',
        },
    },
    'loggers': {
        'my_app': {
            'handlers': ['extras_handler'],
        }
    },
})

app = Flask(__name__)
app.config['FLASK_LOGGING_EXTRAS'] = {
    'BLUEPRINT': {
        'FORMAT_NAME': 'bp',
        'APP_BLUEPRINT': '<app>',
        'NO_REQUEST_BLUEPRINT': '<not a request>',
    },
    'RESOLVERS': {
        'categoy': '<unset>',
        'client': 'log_helper.get_client',
    },
}

bp = Blueprint('my_blueprint', __name__)
app.register_blueprint(bp)

logger = logging.getLogger('my_app')

# This will produce something like this in app.log:
# [2018-05-02 12:44:48.944] [INFO] [my category] [<not request>] The message
logger.info('The message', extra=dict(category='my category'))

# This will produce something like this in app.log:
# [2018-05-02 12:44:48.944] [INFO] [None] [<not request>] The message
logger.info('The message')

@app.route('/1')
def route_1():
    # This will produce a log message like this:
    # [2018-05-02 12:44:48.944] [INFO] [<unset>] [<app>] Message
    logger.info('Message')

    return ''

@bp.route('/2')
def route_2():
    # This will produce a log message like this:
    # [2018-05-02 12:44:48.944] [INFO] [None] [my_blueprint] Message
    logger.info('Message')

    return ''

# This will produce a log message like this:
# [2018-05-02 12:44:48.944] [INFO] [<unset>] [<NOT REQUEST>] Message
logger.info('Message')
format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

init_app(app)[source]

Initialise the formatter with app-specific values from app’s configuration

Indices and tables