Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Python's default logging formatter?

Tags:

python

logging

I'm trying to decipher the information contained in my logs (the logging setup is using the default formatter). The documentation states:

Do formatting for a record - if a formatter is set, use it. Otherwise, use the default formatter for the module.

However, I can't find any reference actually stating what this default format is.

like image 671
Trent Avatar asked May 26 '13 08:05

Trent


People also ask

What is the default logging level in Python?

The default level is WARNING , which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise. Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console.

What is Python logging?

Logging is a Python module in the standard library that provides the facility to work with the framework for releasing log messages from the Python programs. Logging is used to tracking events that occur when the software runs. This module is widely used by the developers when they work to logging.

What is logging config in Python?

The logging configuration functionality tries to offer convenience, and in part this is done by offering the ability to convert text in configuration files into Python objects used in logging configuration - for example, as described in User-defined objects.

Does Python logging use log4j?

The inbuilt logging module in python requires some handful of lines of code to configure log4j-like features viz - file appender, file rotation based on both time & size. For one-liner implementation of the features in your code, you can use the package autopylogger .


1 Answers

The default format is located here which is:

BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"   

The Format code will tell you how you can customize it. Here is one example on how you can customize it.

import sys import logging  logging.basicConfig(     level=logging.DEBUG,     format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",     datefmt="%d/%b/%Y %H:%M:%S",     stream=sys.stdout)  logging.info("HEY") 

Which results in:

[26/May/2013 06:41:40] INFO [root.<module>:1] HEY 
like image 178
rh0dium Avatar answered Sep 21 '22 05:09

rh0dium