Skip to content
Advertisement

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 instead of

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’t been able to figure out why. Answer The logging package

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 about microseconds, but a) I would prefer milliseconds and b) the following doesn’t work on

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 pointed out, sys.excepthook is invoked every time an exception is

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

real time subprocess.Popen via stdout and PIPE

I am trying to grab stdout from a subprocess.Popen call and although I am achieving this easily by doing: I would like to grab stdout in “real time”. With the above method, PIPE is waiting to grab all the stdout and then it returns. So for logging purposes, this doesn’t meet my requirements (e.g. “see” what is going on while

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 $ cat 27493.out function f module name: __main__

Advertisement