0

I want to log a line describing the logging information for each log file created by the logger.

Currently I'm using a separate logger process which will be running all the time. It receives the information from a queue and writes it to the log. A lot of modules will be passing information to this logging queue.

My current sample code is:

import logging
import time

from logging.handlers import TimedRotatingFileHandler as rotate

def info_log(log_queue):
    logger = logging.getLogger("My Log")
    logger.setLevel(logging.INFO)

    handler = rotate("log/info.log", when="D", interval=30, backupCount=13)
    logger.addHandler(handler)

    desc_string = "yyyy/mm/dd-HH:MM:SS \t name \t country \n"
    logger.info(desc_string)

    while True:
        result=log_queue.get().split("#")
        logger.info(result[0] + "\t" result[1] + "\t" + result[2] + "\n")

Each time the log is rotated, I want desc_string to be written 1st to the log file.
How can I do that?

Or in other words, How to know in the program when a log is rotated?

RatDon
  • 3,403
  • 8
  • 43
  • 85

1 Answers1

2

Maybe you can simply override the doRollover method from TimedRotatingFileHandler ?

class CustomFileHandler(TimedRotatingFileHandler):
  def doRollover(self):
     super().doRollover()
     self.stream.write(desc_string)

# to use it 
handler = CustomFileHandler("log/info.log", when="D", interval=30, backupCount=13)
logger.addHandler(handler)
gbin
  • 2,960
  • 1
  • 14
  • 11
  • Looks like it'll do the job. I tried it and it shows an error, `super() takes at least 1 argument. None given.` – RatDon Jun 17 '16 at 08:22
  • 1
    This is a Python 3 syntax: if you are on Python 2 the syntax is `super(CustomFileHandler, self).doRollover()` – gbin Jun 17 '16 at 14:08