loguru
loguru copied to clipboard
Logging while using multiprocessing
I'm launching multiple processes from one file, in each process I have a separate logger.
def __init__(self, number: str, ....):
...
...
...
logger.add(self.sink)
I have about 16 of these processes per server and when I launch it it's spam of messages and I just want to distinguish which process is sending want message using the number
variable.
If number = S01_01, then something like this:
[S01_01] 2023-03-15 21:21:12.559 | CRITICAL | endpoint:linenumber - message
Is there a way do to this without calling the bind
method each time?
You can configure each logger with bind()
indeed, or add the {process}
token to your format
.
Thanks.
So there's no easy way to do specifically what I want? Also, the format I use is used only in the log file, but not in the terminal. Is this OK?
So there's no easy way to do specifically what I want?
Using bind()
is quite easy, I would say. Just use the bound logger in your module.
Also, the format I use is used only in the log file, but not in the terminal. Is this OK?
It depends how you configured the logger sinks. Maybe you need to remove()
the default handler logging to sys.stderr
, and re-add it with your preferred format.
Also be careful about using multiprocessing
without the enqueue=True
parameter, you may end up with unexpected behavior like interleaving logs or multiple file rotations.