fgcz / bfabricPy

API and commad line tools for b-fabric
https://fgcz.github.io/bfabricPy/
GNU General Public License v3.0
4 stars 1 forks source link

Silencing loguru logger #97

Open CodingKaiser opened 1 month ago

CodingKaiser commented 1 month ago

Hi @leoschwarz

I have been using bfabricpy2 for a few weeks now. Awesome work! I was able to get rid of quite some redundant code on my end.

But one question: is there a way to completely silence the loguru logging? I would have expected verbose=False to do the trick, but it seems to only affect messages on top of what loguru already outputs.

Thanks in advance.

All the best, Falko

leoschwarz commented 1 month ago

Hi Falko, thank you for opening the issue.

Logging has not been refactored completely yet

There are the following ways to silence it:

  1. The messages are sent to standard error, so you could capture that (mainly useful for shell scripts so you also have it in case there was an exception)

  2. Use a variation of this snippet:

from loguru import logger
logger.remove()
logger.add(sys.stderr, filter="bfabric", level="WARNING", colorize=False)
  1. As a special variation of the snippet in (2.) you could also send the messages to the default logging module if you prefer that, you just have to give a logging.handler.*" instance tologger.addinstead ofsys.stderr`.
leoschwarz commented 1 month ago

But this is from their docs, they say we should just disable logging in the library and make it opt in for applications

# For libraries, should be your library's `__name__`
logger.disable("my_library")
logger.info("No matter added sinks, this message is not displayed")

# In your application, enable the logger in the library
logger.enable("my_library")
logger.info("This message however is propagated to the sinks")

The only reason I am a bit hesitant to have no logging by default, is that I then expect logging messages to not be available in general. You could also take the logger.disable("bfabric") approach, if you want no logs at all.