snip3rnick / PyHardwareMonitor

Thin Python layer for LibreHardwareMonitor
BSD 3-Clause "New" or "Revised" License
9 stars 5 forks source link

Avoid logging to root logger #5

Closed giuliofoletto closed 7 months ago

giuliofoletto commented 7 months ago

This PR removes the use of the root logger from the logging module and introduces instead a specific logger named as the package itself. The reason is that PyHardwareMonitor is a library, and logging configuration is typically a prerogative of the application code, as explained here and here.

Using the root logger in library code is especially problematic in __init__ modules, as they are typically called very early by the application code, possibly before the configuration of the logger. This means that the configuration (even if it was the default one) of the library, is likely to override the one of the application.

The logging formatting is also removed by this PR, as it is discouraged to add handlers and formatters in library code, even if a specific logger is used. Anyway, it is relatively easy to add it with something like:

handler = logging.StreamHandler()
formatter = logging.Formatter(fmt='%(levelname)s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

just after the definition of the logger, currently in line 9 of the main __init__.py.

snip3rnick commented 7 months ago

Good remark on the global logging, changes will be merged. The logger format is definitely not a requirement, the default will work perfectly fine in this case.