2

So, after reading documentation and couple of stackoverflow questions and trying it in Python 3.4 i dont get it. When does it rotate and on what conditions?

There are 2 threads, they never stop. They call this code (i know that logger is a singleton already, there's more unrelated code in Utility):

import logging
from singleton import Singleton
from logging.handlers import TimedRotatingFileHandler

class Utility(metaclass=Singleton):

    def _logger(self):
        logger = logging.getLogger("main")
        logger.setLevel(logging.INFO)
        if not logger.hasHandlers():
            handler = TimedRotatingFileHandler(
                filename = "info.log",
                when = 's',
                interval = 10,
                backupCount = 10,
                encoding = 'utf-8'
            )
            logger.addHandler(handler)
        return logger

    def log(self, *text):
        self._logger().info(' '.join(str(t) for t in text))
UnstableFractal
  • 1,403
  • 2
  • 15
  • 29
  • And suddenly it started to work with no changes whatsoever. Python 3.4.3 32bit. Randomly it doesn't work. Maybe thread-related problem, no logs are lost, though. – UnstableFractal Aug 10 '15 at 11:19

2 Answers2

3

As far as I can see rotation happens only if a new entry is logged (well, it makes sense, doesn't it?).

And this is what is happening: log() calls handle() which calls emit() (of BaseRotatingHandler in your case https://github.com/python/cpython/blob/829b49cbd2e4b1d573470da79ca844b730120f3d/Lib/logging/handlers.py#L63):

def emit(self, record):
    """
    Emit a record.
    Output the record to the file, catering for rollover as described
    in doRollover().
    """
    try:
        if self.shouldRollover(record):
            self.doRollover()
        logging.FileHandler.emit(self, record)
    except Exception:
        self.handleError(record)

So you can see that there are two methods involved in rotation: shouldRollover() and doRollover(). They are defined on your TimedRotatingFileHandler (https://github.com/python/cpython/blob/829b49cbd2e4b1d573470da79ca844b730120f3d/Lib/logging/handlers.py#L193) and are pretty straightforward.

twil
  • 6,032
  • 1
  • 30
  • 28
0

Revisiting old question, just use loguru

UnstableFractal
  • 1,403
  • 2
  • 15
  • 29