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?

有帮助吗?

解决方案

Use RotatingFileHandler or TimedRotatingFileHandler instead.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top