Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : clear a log file

Tags:

I develop a client-server application and I have log in the server, so I use the logging module. I would like to create a command in the server to clear the file.

I have test with os.remove() but after, the log doesn't work.

Do you have an idea?

Thanks.

like image 787
Guillaume Avatar asked Jan 17 '12 17:01

Guillaume


People also ask

How do you clear a log file in python?

You'd have to signal to the daemon that the log file's been removed so it can close/reopen the log file handle and start using the new log file. Otherwise it'll just keep writing to the old one, which will still exist somewhere until all filehandles on it are closed. Give mode="w" to logging. FileHandler .

How do you clear a logger?

Go to the Admin Logs tab. Click/Tap Clear Log. Note: For hosts/superusers, the selected site's log is cleared. If All is selected, the logs of all sites in the installation are cleared.

What is StreamHandler Python?

StreamHandler. The StreamHandler class, located in the core logging package, sends logging output to streams such as sys. stdout, sys. stderr or any file-like object (or, more precisely, any object which supports write() and flush() methods).


1 Answers

It might be better to truncate the file instead of removing it. The easiest solution is to reopen the file for writing from your clearing function and close it:

with open('yourlog.log', 'w'):     pass 
like image 131
newtover Avatar answered Oct 24 '22 11:10

newtover