Skip to content

Tag: logging

Python : clear a log file

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. Answer It might be better to truncate the file ins…

In python, why use logging instead of print?

For simple debugging in a complex project is there a reason to use the python logger instead of print? What about other use-cases? Is there an accepted best use-case for each (especially when you’re only looking for stdout)? I’ve always heard that this is a “best practice” but I haven&…

Python logging: use milliseconds in time format

By default logging.Formatter(‘%(asctime)s’) prints with the following format: where 638 is the millisecond. I need to change the comma to a dot: To format the time I can use: however the documentation doesn’t specify how to format milliseconds. I’ve found this SO question which talks a…

Logging uncaught exceptions in Python

How do you cause uncaught exceptions to output via the logging module rather than to stderr? I realize the best way to do this would be: But my situation is such that it would be really nice if logging.exception(…) were invoked automatically whenever an exception isn’t caught. Answer As Ned pointe…

Python logger dynamic filename

I want to configure my Python logger in such a way so that each instance of logger should log in a file having the same name as the name of the logger itself. e.g.: I want to keep it generic and dont want to hardcode all kind of logger names I can have. Is that possible? Answer

Log output of multiprocessing.Process

Is there a way to log the stdout output from a given Process when using the multiprocessing.Process class in python? Answer The easiest way might be to just override sys.stdout. Slightly modifying an example from the multiprocessing manual: And running it: $ ls m.py $ python m.py $ ls 27493.out 27494.out m.py…