loguru icon indicating copy to clipboard operation
loguru copied to clipboard

Logging while using multiprocessing

Open Alexei17 opened this issue 1 year ago • 3 comments

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?

Alexei17 avatar Mar 16 '23 11:03 Alexei17

You can configure each logger with bind() indeed, or add the {process} token to your format.

Delgan avatar Mar 16 '23 11:03 Delgan

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? image

Alexei17 avatar Mar 16 '23 11:03 Alexei17

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.

Delgan avatar Mar 16 '23 14:03 Delgan