This page looks best with JavaScript enabled

Python Logger

 ·  ☕ 2 min read  ·  🐨 Puliyo

Here is the takeaway to setup logger in your python script.

It will rotate daily and maintains log file for 7 days.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import logging, logging.handlers

LOGFILE = /path/tp/my/logfile.log

def initlogger(level = logging.DEBUG):
  logger = logging.getLogger('my-logger')
  logger.setLevel(level)
  if not logger.handlers:
    handler = logging.handlers.TimedRotatingFileHandler(
      LOGFILE, when = 'D', backupCount = 7
    )
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
  return logger

initlogger()

Call the function once at the beginning and call logging.getLogger(‘my-logger’) wherever you want to log.

getLogger is singleton hence will preserve the configuration if you provide same name ‘my-logger’ to getLogger.

1
2
3
4
5
6
7
8
initlogger()

...

logger = logging.getLogger('my-logger')
logger.debug("DEBUG MESSAGE")
logger.info("INFO: " + log)
logger.warning('%s[%d]: %s', str(e), retcode, msg)

Breakdown explanation

1
if not logger.handlers

Typically initlogger should be called once.

When for some reason it is called again, this check will prevent same handler to be attached.

When you find log message appearing twice, it is most likely because of multiple handler attached.

1
2
3
handler = logging.handlers.TimedRotatingFileHandler(
  LOGFILE, when = 'D', backupCount = 7
)

TimedRotatingFileHandler is a predefined handler which does log rotation for you.

What you need to do is specify the type of interval in when and at most how much files to keep backupCount.

You can change the interval of when by specifying interval argument (default interval=1).

According to the reference, log is rotated at time handler got attached.

1
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

Formatter allows you to structure the content of the log message.

%(message)s is required if you want to insert your message in the log.

Output example below:

1
2
$ logger.debug('debug message')
> 2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message

Reference

Logging HOWTO

import logging

TimedRotatingFileHandler

Share on
Support the author with