Coder Social home page Coder Social logo

freedomofpress / django-logging Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cipriantarta/django-logging

0.0 5.0 0.0 120 KB

A Django library that logs request, response and exception details in a JSON document. It uses the python rotation mechanism to rotate the file logs, but the rotation files will be gziped.

License: Other

Python 100.00%

django-logging's Introduction

Django Logging

A Django library that logs request, response and exception details in a JSON document. It uses the python rotation mechanism to rotate the file logs, but the rotation files will be gziped.

Installation

pip install git+https://github.com/cipriantarta/django-logging

or

pip install django-logging-json

Quick start

  1. Add "django_logging" to your INSTALLED_APPS settings like this:
INSTALLED_APPS = (
    ...
    'django_logging',
)
  1. Include the DjangoLoggingMiddleware middleware in your MIDDLEWARE_CLASSES like this:
MIDDLEWARE_CLASSES = (
    'django_logging.middleware.DjangoLoggingMiddleware',
    ...
)

Handlers

AppFileHandler

This handle will log request/response info to LOG_PATH/app.log. It will also log request/exception, for unhandled exceptions, in the same file. Log format:

Request and Response

{
    "INFO":
    {
        "timestamp":
        {
            "request": {
            ... request info ...
            },
            "response": {
            ... response info ...
            }
        }
    }
}

Request and Exception

{
    "ERROR":
    {
        "timestamp":
        {
            "request": {
            ... request info ...
            },
            "exception": {
                "message": "Exception message",
                "traceback": [
                    ...
                ]
            }
        }
    }
}

SQLFileHandler

This handler will log all queries to LOG_PATH/sql.log. In a production environment you should set LOG_LEVEL = Error or SQL_LOG = False to avoid performance issues. The queries will also be logged to the console if CONSOLE_LOG is set to True

DebugFileHandler

This handler will log debug messages to LOG_PATH/debug.log. This handler is only used when settings.DEBUG is set to True.

Log format:

[%(levelname)s - %(created)s], file:%(module)s.py, func:%(funcName)s, ln:%(lineno)s: %(message)s

Custom Use

To log debug messages:

from django_logging import log

log.debug('debug message')

To log handled exceptions:

from django_logging import log, ErrorLogObject

log.error(ErrorLogObject(request, exception))

Settings

Inspired by Django Rest Framework, Django Logging settings are grouped in a single dictionary.

To override Django Logging settings, add a dictionary in your project's settings file

DJANGO_LOGGING = {
    "CONSOLE_LOG": False
}

Default Settings

CONSOLE_LOG = True - Log to console.

SQL_LOG = True - Log SQL queries.

LOG_LEVEL = 'debug' - If settings.DEBUG is set to True, otherwise LOG_LEVEL is set to 'info'

DISABLE_EXISTING_LOGGERS = True - Set this to False if you want to combine with multiple loggers.

LOG_PATH = '{}/logs'.format(settings.BASE_DIR) - If the logs folder does not exist, it will be created.

IGNORED_PATHS = ['/admin', '/static', '/favicon.ico'] - List of URL endpoints to ignore.

RESPONSE_FIELDS = ('status', 'reason', 'charset', 'headers', 'content') - List of response fields to log.

CONTENT_JSON_ONLY = True - Log response content only if its a JSON document.

ROTATE_MB = 100 - Maximum size in MB that the log file can have before it gets rotated.

ROTATE_COUNT = 10 - Maximum number of rotated log files.

PROPOGATION = True - set this to False to disable propogation of the 'dl_logger' logger

Change Log

1.5.5 [2017-06-21]

  • fixes compatibility for django MIDDLEWARE django >= 1.10

1.5.4 [2017-02-08]

  • fixes compatibility with python < 3.5 when logging exceptions

1.5.3 [2016-06-26]

  • bug fixing

1.5 [2016-06-04]

  • do not return a response in process_exception. Give other middlewares a chance to process the exception.
  • console log indentation
  • elastic search support. below a sample chart using kibana with elasticsearch and django-logging

kibana_sample.png

1.4 [2016-02-19]

  • bug fixing
  • added support for query logging when using multiple database
  • added database alias for sql logs if multiple databases are used
  • added plain dict logging support

1.3 [2015-12-13]

  • added support for Python 2.7
  • added support for Django >= 1.4

1.2 [2015-11-22]

  • added sql logging support
  • log entries are now sorted by keys
  • console handler now indents the log entries by 4 spaces
  • log response content if it's JSON (bug)

1.1 [2015-11-17]

  • added exception "type" for exception log entries

django-logging's People

Contributors

chigby avatar conorsch avatar harrislapiroff avatar micimize avatar msheiny avatar saptaks avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

django-logging's Issues

Publish our fork as pip module

We are making substantial changes versus upstream. Let's start publishing these changes as a separate pypi package for ingestion by our various django code-bases.

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.