Git Product home page Git Product logo

flask_logify's Introduction

Flask-Logify

version

Advanced Logging configuration for flask application based on yaml or json file. See logging.config

NOTE: If you want to use flask_logify.handlers.FlaskSysLogHandler you must init this extension with an app context.

The dump of request or response are made by builders, there are two concrete implementations:

  1. LogTextBuilder: message as plain text (configurable).
  2. LogJSONBuilder: message as json format.

You can create your own builder by extending class LogBuilder. In order to get the correct remote address you can override LogBuilder.get_remote_address method or you can inject a function in LogBuilder constructor.

See example usage in example/test.py.

FlaskLogging as a decorator attribute with which you can disable log messages for a specific route, passing a logging filter to it. For example you want disable log for health check endpoint. See example/text.py.

Quickstart

Install flask_logify using pip:

$ pip install Flask-Logify

Example usage

An example of log file configuration is under example folder.

Only yaml or json format are supported.

from flask import Flask
from flask_logify import FlaskLogging


app = Flask(__name__)
app.config['LOG_FILE_CONF'] = 'log.yaml'
app.config['LOG_LOGGER_NAME'] = 'flask-development'

logging = FlaskLogging()
with app.app_context():
    logging.init_app(app)

app.run()

Go to http://127.0.0.1:5000/ and see log messages like configured

Configuration

Base configuration keys:

  1. LOGGING: (default: None) dict logging configuration
  2. LOG_FILE_CONF: (default: None) absolute path of configuration file (has priority on LOGGING)
  3. LOG_APP_NAME: (default: flask) the ident of the record used by syslog
  4. LOG_LOGGER_NAME: (default: flask-development) usually is {LOG_APP_NAME}-{FLASK_ENV}
  5. REQUEST_ID_HEADER: (default: X-Request-ID) request id header key
  6. LOG_BUILDER: (default: text) log builder implementation (text, json)

Text and JSON builder configuration keys:

  1. LOG_REQ_HEADERS: (default: []) request headers to dump always
  2. LOG_RESP_HEADERS: (default: []) response headers to dump always
  3. LOG_REQ_SKIP_DUMP: (default: not DEBUG) if true dump of request body and headers are skipped
  4. LOG_RESP_SKIP_DUMP: (default: DEBUG) if true dump of response body and headers are skipped

Text builder only:

  1. LOG_RESP_FORMAT: (default: "OUTGOING RESPONSE for {address} at {path}: {level} STATUS {status}{headers}{body}") log message format for response
  2. LOG_REQ_FORMAT: (default: "INCOMING REQUEST: {address} {method} {scheme} {path}{headers}{body}") log message format for request

License MIT

flask_logify's People

Contributors

cs91chris avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

flask_logify's Issues

v2.4.3 loosing app context with Flask

Output with v2.4.2

The same configuration file when loaded with v2.4.2 works as intended
image

config.yaml

version: 1
disable_existing_loggers: true

formatters:
  consoleSimple1:
    (): flask_logify.formatters.RequestFormatter
    request_id_header: X-Request-ID
    format: "[%(asctime)s] [%(levelname)s] [%(request_id)s]: %(message)s"
handlers:
  console:
      class: logging.StreamHandler
      stream: ext://sys.stdout

  consoleDevelopment:
    class: logging.StreamHandler
    stream: ext://sys.stdout
    formatter: consoleSimple
    level: DEBUG
loggers:
  flask-development:
    handlers: [ consoleDevelopment,file,file_json]
    level: DEBUG
    propogate: yes

Traceback errors with v2.4.3

[2021-07-18 16:38:14,987] ERROR in logging: bad configuration file: ./logs/config.yaml
[2021-07-18 16:38:14,988] ERROR in logging: the configuration below

[2021-07-18 16:38:14,988] ERROR in logging: Unable to configure formatter 'consoleSimple1'
Traceback (most recent call last):
  File "c:\users\username\appdata\local\programs\python\python39\lib\logging\config.py", line 543, in configure
    formatters[name] = self.configure_formatter(
  File "c:\users\username\appdata\local\programs\python\python39\lib\logging\config.py", line 655, in configure_formatter
    result = self.configure_custom(config)
  File "c:\users\username\appdata\local\programs\python\python39\lib\logging\config.py", line 474, in configure_custom
    result = c(**kwargs)
  File "S:\Projects\Programming\2021\project_test\env\lib\site-packages\flask_logify\formatters.py", line 12, in __init__   
    self.app_name = flask.current_app.config['LOG_APP_NAME']
  File "S:\Projects\Programming\2021\project_test\env\lib\site-packages\werkzeug\local.py", line 422, in __get__
    obj = instance._get_current_object()
  File "S:\Projects\Programming\2021\project_test\env\lib\site-packages\werkzeug\local.py", line 544, in _get_current_object
    return self.__local()  # type: ignore
  File "S:\Projects\Programming\2021\project_test\env\lib\site-packages\flask\globals.py", line 47, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "S:\Projects\Programming\2021\project_test\env\lib\site-packages\flask_logify\logging.py", line 66, in init_app      
    logging.config.dictConfig(self._conf)
  File "c:\users\username\appdata\local\programs\python\python39\lib\logging\config.py", line 809, in dictConfig
    dictConfigClass(config).configure()
  File "c:\users\username\appdata\local\programs\python\python39\lib\logging\config.py", line 546, in configure
    raise ValueError('Unable to configure '
ValueError: Unable to configure formatter 'consoleSimple1'

Program field in log formatter

How do we use LOG_APP_NAME for formatting the log files.

I have this inside my config.yaml
formatters: consoleSimple: (): flask_logify.formatters.RequestFormatter format: "%(program)% [%(asctime)s] [%(levelname)s] : %(message)s"

Error
--- Logging error --- Traceback (most recent call last): File "\appdata\local\programs\python\python39\lib\logging\__init__.py", line 434, in format return self._format(record) File "\appdata\local\programs\python\python39\lib\logging\__init__.py", line 430, in _format return self._fmt % record.__dict__ KeyError: 'program'
Same with %(PROGRAM)%

Coloring

Is it currently possible to use coloring with the Flask-Logify ?

I have tried these two options but have shortcoming in both:

1. colored-logs

A famous external python package commonly used for coloring logs.
Issue : After installing it on top of Flask-Logify, we loose all the formatting from Flask-Logify

2. ANSI escape characters

format: \x1b[91m"[%(asctime)s] [%(levelname)s] : %(message)s"\x1b[0m

This does produce a colored log, but the issue is, there is nothing like RotatingStreamHandler
So, with the following YAML configuration, logs are duplicated in the console

formatters:
  humanDebug:
    (): flask_logify.formatters.RequestFormatter
    format: \x1b[91m"[%(asctime)s] [%(levelname)s] : %(message)s"\x1b[0m
  humanInfo:
    (): flask_logify.formatters.RequestFormatter
    format: \x1b[93m"[%(asctime)s] [%(levelname)s] : %(message)s"\x1b[0m

handlers:
  console:
    class: logging.StreamHandler
    stream: ext://sys.stdout

  consoleDebug:
    class: logging.StreamHandler
    stream: ext://sys.stdout
    formatter: humanDebug
    level: DEBUG

  consoleInfo:
    class: logging.StreamHandler
    stream: ext://sys.stdout
    formatter: humanInfo
    level: INFO

loggers:
  flask-development:
    handlers: [ consoleDebug,consoleInfo,consoleWarn]
    level: DEBUG
    propogate: yes

Missing Example file

Example file link is broken.
Amazing project btw. Saves a ton of time configuring logging handlers. :))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.