Question

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?

Était-ce utile?

La solution

Use RotatingFileHandler or TimedRotatingFileHandler instead.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top