-1

I have a file handler:

logger = logging.getLogger(__name__)
fh = logging.FileHandler('file_name.log',mode='w')
logger.addHandler(fh)

After some time I want to copy the file and clear the original file so that the logger will keep adding data to the original file. Something like this:

shutil.dopy('file_name.log','another_file.log')
os.remove('file_name.log')
open('file_name.log','w')

This, of course doesn't work. I'm working on python 3.2. Can it be done?

orenma
  • 1,163
  • 2
  • 10
  • 19
  • Do you really want to copy the original file, clear it out, and start overwriting it? It's simpler, and more common to just move the file and start writing a new one. It's especially more simpler and more common when you're doing it in Python, which has that functionality built in to the `logger` module. – abarnert Oct 06 '13 at 07:16

1 Answers1

2

Use RotatingFileHandler or TimedRotatingFileHandler instead.

http://docs.python.org/3/library/logging.handlers.html#rotatingfilehandler

Ivo
  • 5,378
  • 2
  • 18
  • 18