title: python TimedRotatingFileHandler old empty files
body: <p>I have a problem with the python <strong>logging</strong> library. I have a <strong>flask</strong> application that uses the following class (<strong>_WrapperLog</strong>) to write logs to a file, the library correctly writes logs to today's file and cleans up files older than 7 days correctly. the problem is that when the log rotation occurs, the old files remain saved but their content disappears (as in the example below), only the first 2/4 lines of the file remain, and then nothing, what could be due ?</p>
  <pre><code>import logging
  from logging.handlers import TimedRotatingFileHandler
  import os
  from datetime import datetime
  from os import name as os_name
  from sys import stdout as sys_stdout
  
  class _WrapperLog(object):
  
      class __Log:
          logger = None
          loggerNameTimeStamp=None
          def __init__(self):
              self.logger = self.get_logger()
          def __str__(self):
              return str(self)
          
          def get_logger(self):
              if datetime.now().strftime(&quot;%Y-%m-%d&quot;) == self.loggerNameTimeStamp:
                  return self.logger
              if self.logger:
                  while len(self.logger.handlers) &gt; 0:
                      self.logger.handlers.pop()
              self.logger = logging.getLogger(__name__)
              self.logger.setLevel(logging.INFO)
              formatter = logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
              if os_name == 'nt':
                  handler = logging.StreamHandler(sys_stdout)
              else:
                  handler = TimedRotatingFileHandler(os.path.join('/opt/my_folder', 'my_log.log'), 
                                     when='midnight', backupCount=7)
              handler.setLevel(logging.INFO)
              handler.setFormatter(formatter)
              self.logger.addHandler(handler)
              self.loggerNameTimeStamp = datetime.now().strftime(&quot;%Y-%m-%d&quot;)
              return self.logger
  
  
          def info(self, message:str):
              self.get_logger().info(message)
              
          def debug(self, message:str):
              print(&quot;DEBUG: &quot; + message)
  
          def warning(self, message:str):
              self.get_logger().warning(message)
  
          def error(self, message:str):
              self.get_logger().error(message)
  
      instance = None
  
      def __new__(cls): 
          if not _WrapperLog.instance:
              _WrapperLog.instance = _WrapperLog.__Log()
          return _WrapperLog.instance
  
      def __getattr__(self, name):
          return getattr(self._WrapperLog, name)
  
      def __setattr__(self, name):
          return setattr(self._WrapperLog, name)
  
  Log = _WrapperLog()
  </code></pre>
  <p>example of what remains of the files from yesterday and the day before yesterday:</p>
  <p><strong>my_log.log.2022-04-11</strong>:</p>
  <pre><code>[2022-04-12 00:00:01] INFO: &lt;censored&gt;
  [2022-04-12 00:00:01] INFO: &lt;censored&gt;
  </code></pre>
  <p><strong>my_log.log.2022-04-10</strong>:</p>
  <pre><code>[2022-04-11 00:00:01] INFO: &lt;censored&gt;
  [2022-04-11 00:00:01] INFO: &lt;censored&gt;
  [2022-04-11 00:00:01] INFO: &lt;this line started like this and stopped after INFO&gt;
  </code></pre>