napalm-automation/napalm-yang

NullHandler logging

Closed this issue · 2 comments

Should logging be done through the NullHandler instead of the StreamHandler?

The recommendation is here: https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library

ch = logging.StreamHandler(stream)

Using NullHandler would eliminate the No handlers could be found for logger "napalm-yang" messages.

So the recommendation reads as follows:

It is strongly advised that you do not add any handlers other than NullHandler to your library’s loggers. This is because the configuration of handlers is the prerogative of the application developer who uses your library

Which we don't do, there is just a convenient function you can call yourself to do so. The idea of that helper method is to have a simple method to configure logging, it's mostly useful when developing/testing profiles. It is not aimed to be called by default by the library itself (the user is welcome to do so, in which case it's the user who is doing it, not us).

events of severity WARNING and greater will be printed to sys.stderr. This is regarded as the best default behaviour (...) If for some reason you don’t want these messages printed in the absence of any logging configuration, you can attach a do-nothing handler to the top-level logger for your library

This is another story and something we could add to any of the __init__.py files if we think warnings and errors shouldn't be redirected to stderr by default. I am open to discuss that.

Thanks! I'll keep this in mind. Closing.