loguru
loguru copied to clipboard
Configure in one module, use in multiple modules
Hi,
Firstly, thanks for relieving "usual python logging" pain significantly.
I faced this issue in the usual loggin way as well and was wondering in loguru has a solution --
I have a common LogManager that runs as a backend daemon in a kafka environment. This manager imports and configures the logger.
from loguru import logger as lg
lg.remove(0)
lg.add("Logs/sys_debug.log", level='DEBUG', enqueue=True, mode='w')
I want other modules to simply import the lg var from the LogManager modules and simply log to a common file ...
from <somemodule-chain>.LogManager import lg
lg.info("Starting something...")
when I try this, nothing is logged to the file. I also tried importing the just the logger (from loguru import logger as lg
) in multiple files but configure it once in the LogManager modules, and that did not work either. Note that I remove the stderr sink in the beginning but if I don't, the log is sent to stderr!!
Any help is appreciated!!
PS :
I looked up the other issues and found a similar one but somehow could not find something answering my queries.
Hey @j4l-Shvn. :)
Loguru should perfectly fit your use case, because one of its properties is that there is is only one logger
instance. You can configure it in one module, and all others files will benefit from the added handlers just by importing the logger
.
I will need more examples about your configuration, because I created two files on my computer containing the code you shared, and it's working just fine!
I think this might be caused by kafka
using multiprocessing
which result in logger
copies in some cases, while spawning a child process. If the logger
is inadvertently copied, handlers added to one instance will not be available in the other one.
Hey @Delgan ,
Yup!! I realised that too. You did a great job. When you say working did you mean logging to a file or just stderr? Ill try to create two separate files and upload to reproduce the issue (i should not just term it as an issue as of yet :) )...
When you say working did you mean logging to a file or just stderr?
It was logging to "Logs/sys_debug.log"
and only to this file. However, if a child process is spawned before configuring the logger
, it might result in unexpected behavior (due to the child inheriting a logger
with the default sys.stderr
handler).
Ill try to create two separate files and upload to reproduce the issue (i should not just term it as an issue as of yet :) )...
Yeah, if you're able to make a reproducible example that would be super useful to investigate this issue (we can term it like this, it's not a bug yet :smile: )!
Hey @j4l-Shvn, did you figured out a solution?
Hey @Delgan, I have been off that project for a while .. but Ill be back soon. I guess you can close this issue for now and ill update when I have the next update... Thanks!!!
Thanks for the answer. If you think the problem still persists, then I'll keep this ticket open for now. :+1:
Closing this 2 years old ticket, but feel free to re-open if needed. :grin: