How to configure Logging in Python

Viewed 51701

I am new to Python and just starting one project. I am used to use log4j in Java and I would like to log all modules and classes in Python as I do in Java.

In Java I have one log configuration file in src folder named log4j.properties like below:

log4j.rootLogger=DEBUG, Console, fileout

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %5p [%t] (%F:%L) - %m%n

log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=servidor.log
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} (%F:%L) %p %t %c - %m%n

It logs to console and a file.

In my classes, I only have to import log4j and add a static attribute to recovery the log4j logger with config loaded then all classes will be logging in console and file. The configuration file is loaded automaticaly by the name. For example:

import org.apache.log4j.Logger;

public class Main {
    public static Logger logger = Logger.getLogger(Main.class);
    public static void main(String[] args) {
        logger.info("Hello");
    }
}

Now I am having problem to setup logging in Python, I have read the docs but I couldn't find a way to use it in many modules/classes. How could I setup Python logging in an easy way to log my modules and classes without code much in every module/class? Is it possible to reproduce same code that I have wrote in Python?

3 Answers

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.

Here are the basics.

1. Install package

pip install autopylogger

2. Usage

# import the package
from autopylogger import init_logging

# Initialise the logging module
mylogger = init_logging(log_name="myfirstlogger", log_directory="logsdir")

That's it, logging object has been initialized with file writing and rotation feature enabled.

You can write the logs by the below commands.

# Write logs - DEBUG | INFO | WARNING | ERROR
mylogger.debug('This is a INFO log')
mylogger.info('This is a DEBUG log')
mylogger.warning('This is a WARNING log')
mylogger.error('This is a ERROR log')

Why should you use autopylogger?

  • File Appender - Enabled by default.

  • Log rotation - Enabled by default. Can also configure rotation based on both time & size, which is missing in basic logging library.

  • Critical log mailing feature - Send mail for critical logs by initializing library with your SMTP server credentials. Comes handy in PRODUCTION environments.

For documentation, refer to Official Github page, PyPI Official page

Related