title: Need to do a daily log rotation (0utc) using Python
body: <p>I'm an admitted noob to Python. I've written a little logger that takes data from the serial port and writes it to a log file. I've got a small procedure that opens the file for append, writes, then closes. I suspect this might not be the best way to do it, but it's what I've figured out so far.</p>
  
  <p>I'd like to be able to have it automagically perform a log-rotate at 00 UTC, but so far, my attempts to do this with RotatingFileHandler have failed. </p>
  
  <p>Here's what the code looks like:</p>
  
  <pre><code>import time, serial, logging, logging.handlers,os,sys
  from datetime import *
  
  CT12 = serial.Serial()
  CT12.port = "/dev/ct12k"
  CT12.baudrate = 2400
  CT12.parity = 'E'
  CT12.bytesize = 7
  CT12.stopbits = 1
  CT12.timeout = 3
  
  logStart = datetime.now()
  dtg = datetime.strftime(logStart, '%Y-%m-%d %H:%M:%S ')
  ctlA = unichr(1)
  bom = unichr(2)
  eom = unichr(3)
  bel = unichr(7)
  CT12Name = [ctlA, 'CT12-NWC-test']
  CT12Header = ['-Ceilometer Logfile \r\n', '-File created: ', dtg, '\r\n']
  
  def write_ceilo ( text ) :
      f = open ('/data/CT12.log', 'a')
      f.write (text)
      f.close ()
  
  write_ceilo(''.join(CT12Header))
  
  CT12.open()
  
  discard = CT12.readlines()
  #print (discard)
  
  while CT12.isOpen():
      response = CT12.readline()
      if len(response) &gt;= 3:
          if response[0] == '\x02' :
              now=datetime.now()
              dtg=datetime.strftime(now, '-%Y-%m-%d %H:%M:%S\r\n')
              write_ceilo(dtg)
              write_ceilo(''.join(CT12Name))
              write_ceilo(response)
  </code></pre>
  
  <p>What can I do to make this rotate automatically, affixing either a date of rotation, or a serial number, for identification. I'm not looking to rotate any of these out, just keep a daily log file of the data. (or maybe an hourly file?)</p>