[Fixed]-Logging formatters in django

42👍

From Python logging module documentation:

  • asctime: %(asctime)s
    Human-readable time when the LogRecord was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time).

  • created: %(created)f
    Time when the LogRecord was created (as returned by time.time()).

  • filename: %(filename)s
    Filename portion of pathname.

  • funcName: %(funcName)s
    Name of function containing the logging call.

  • levelname: %(levelname)s
    Text logging level for the message (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’).

  • levelno: %(levelno)s
    Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).

  • lineno: %(lineno)d
    Source line number where the logging call was issued (if available).

  • module: %(module)s
    Module (name portion of filename).

  • msecs: %(msecs)d
    Millisecond portion of the time when the LogRecord was created.

  • message: %(message)s
    The logged message, computed as msg % args. This is set when Formatter.format() is invoked.

  • name: %(name)s
    Name of the logger used to log the call.

  • pathname: %(pathname)s
    Full pathname of the source file where the logging call was issued (if available).

  • process: %(process)d
    Process ID (if available).

  • processName: %(processName)s
    Process name (if available).

  • relativeCreated: %(relativeCreated)d
    Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.

  • thread: %(thread)d
    Thread ID (if available).

  • threadName: %(threadName)s
    Thread name (if available).

The following arguments are also available to Formatter.format(), although they are not intended to be included in the format string:

  • args:
    The tuple of arguments merged into msg to produce message.

  • exc_info:
    Exception tuple (à la sys.exc_info) or, if no exception has occurred, None.

  • msg:
    The format string passed in the original logging call. Merged with args to produce message, or an arbitrary object (see Using arbitrary objects as messages).

4👍

Step1. Edit your settings.py file

$ cd mysite
$ vim mysite/settings.py
'formatters': {
    'simple': {
        'format': '%(levelname)s %(asctime)s %(name)s.%(funcName)s:%(lineno)s- %(message)s'
    },
},

Step2. You should use logger in your coding like this:

import logging
logger = logging.getLogger(__name__)

def fn1() {
    logger.info('great!')
    logger.info(__name__)
}

Hope that helps you!

Leave a comment