Skip to content

Break up logging so everything isn't coming off the root #87

@danielquinn

Description

@danielquinn

As discussed earlier, here's the code snippet I was talking about to make logging easier project-wide:

# logger.py

import logging


class LogMixin:
    """
    Use this mixin to do logging:
      self.logger.debug("My debugging message")
    """

    __logger = None

    @property
    def logger(self) -> logging.Logger:

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

        return self.logger

To use it, just do this:

from logger import LogMixin


class MyClass(LogMixin, AnyOtherParent):
    ...
    def my_method(self):
        self.logger.debug("This is my debug message with %s", some_variable)

This will produce log entries attached to the class and file path rather than the root.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions