RIPE-NCC / ripe-atlas-sagan

A parsing library for RIPE Atlas measurement results
GNU General Public License v3.0
47 stars 25 forks source link

Use a named logger instead of the root logger #89

Open vgiotsas opened 6 years ago

vgiotsas commented 6 years ago

Currently (v. 1.2.1) all the warnings are logged using the root logger which isn't very helpful since the logging messages do not identify which module emitted the warning message, which is especially problematic when using sagan in scripts that include other libraries that also don't use named loggers.

For libraries, the best practice for logging is to use Loggers with name __name__ to ensure no name collisions.

danielquinn commented 6 years ago

I have a handy code snippet that I use these days to keep logging easy. Maybe this would help?

import logging

class LogMixin(object):
    """
    Use this mixin to do logging:

      self.logger.debug("My debugging message")
    """
    _logger = None

    @property
    def logger(self):

        if self._logger:
            return self._logger
        self._logger = logging.getLogger(self.__class__.__module__)

        return self.logger