python – Create a log file

python – Create a log file

You can use the logging module to accomplish this.

At the very easiest level, it will be set up like this:

logging.basicConfig(filename=logfilename.log, level=logging.INFO)

There are a number of different levels that you can use to write to the file, such as: text goes here)
logging.error(your text goes here)
logging.debug(your text goes here)

You can use these lines anywhere that you want to log to the file. If you want to replace the console printing with logging all together, just replace the print lines with

For more info on the topic, such as more configurable options (such as timestamps), check the docs (python 3):

Logging in python is very efficient and easy to use. You just have to define a python module for logging using python internal logging module. You can define as many logger as you want. You can also configure it to print the output to a console as well write to a file. Apart from this you can define a rotating file handler which will do a log rotation as well which helps in log rotation automation.
Below is the snippet to directly define and call the logger in any python module.

import sys
import logging
from logging.config import dictConfig

logging_config = dict(
        verbose: {
            format: ([%(asctime)s] %(levelname)s 
                       [%(name)s:%(lineno)s] %(message)s),
            datefmt: %d/%b/%Y %H:%M:%S,
        simple: {
            format: %(levelname)s %(message)s,
        api-logger: {class: logging.handlers.RotatingFileHandler,
                           formatter: verbose,
                           level: logging.DEBUG,
                           filename: logs/api.log,
                           maxBytes: 52428800,
                           backupCount: 7},
        batch-process-logger: {class: logging.handlers.RotatingFileHandler,
                             formatter: verbose,
                             level: logging.DEBUG,
                             filename: logs/batch.log,
                             maxBytes: 52428800,
                             backupCount: 7},
        console: {
            class: logging.StreamHandler,
            level: DEBUG,
            formatter: simple,
            stream: sys.stdout,
        api_logger: {
            handlers: [api-logger, console],
            level: logging.DEBUG
        batch_process_logger: {
            handlers: [batch-process-logger, console],
            level: logging.DEBUG


api_logger = logging.getLogger(api_logger)
batch_process_logger = logging.getLogger(batch_process_logger)

once you have defined this file (say, you can import it anywhere and use.

from logger_settings import api_logger world)

Hope this help. Thanks

python – Create a log file

To create the log file we can use logging package in python. Code to create log file –

import logging
LOG_FILENAME = logfile.log
for handler in logging.root.handlers[:]:
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG) Job Started...)
logging.debug(abc method started...)

And if you want to create the log file timestamp than we can accomplish that using datetime package. code to create log file with timestamp –

from datetime import datetime

Leave a Reply

Your email address will not be published. Required fields are marked *