Skip to content

Commit

Permalink
[logging] Add methods for getting logger names and expose
Browse files Browse the repository at this point in the history
  • Loading branch information
nhairs committed Jan 23, 2024
1 parent 1dc0eb4 commit bd3383f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pillar"
version = "0.1.0"
version = "0.2.0"
description = "Building Blocks for Python Applications"
authors = [
{name = "Nicholas Hairs", email = "info+pillar@nicholashairs.com"},
Expand Down Expand Up @@ -74,4 +74,4 @@ docs = [
]

[tool.setuptools.package-data]
python_template = ["py.typed"]
pillar = ["py.typed"]
2 changes: 1 addition & 1 deletion src/pillar/config.py
Expand Up @@ -110,7 +110,7 @@ def __init__(
default_config: Set initial `self.config` to this.
default_parsers: Use these parsers instead of the `DEFAULT_PARSERS`.
"""
self.logger: logging.Logger = self._get_logger()
self.logger: logging.Logger = self.get_logger()

self.merger: deepmerge.Merger = deepmerge.Merger(
[(dict, ["merge"]), (list, ["override"])], ["override"], ["override"]
Expand Down
47 changes: 38 additions & 9 deletions src/pillar/logging.py
Expand Up @@ -3,7 +3,7 @@
## Standard Library
import logging
import sys
from typing import Dict
from typing import Dict, Type

import __main__

Expand Down Expand Up @@ -94,21 +94,50 @@ def logging_file_handler_errors_kwargs(errors: str) -> Dict[str, str]:
return {}


def get_logger_name_for_instance(instance: object, prefix: str = "") -> str:
"""Get a logger name based on the qualified name of this instance's class.
Args:
instance: object to inspect
prefix: optional prefix for the logger name.
"""
return get_logger_name_for_class(instance.__class__, prefix)


def get_logger_name_for_class(cls: Type[object], prefix: str = "") -> str:
"""Get a logger name based on the qualified name of a class.
Args:
cls: object to inspect
prefix: optional prefix for the logger name.
"""
if cls.__module__ == "__main__":
module_name = __main__.__spec__.name
else:
module_name = cls.__module__
logger_name = f"{module_name}.{cls.__qualname__}"

if prefix:
return f"{prefix}.{logger_name}"

return logger_name


### CLASSES
### ============================================================================
class LoggingMixinBase: # pylint: disable=too-few-public-methods
"""Base class for logging mixins"""

logger: logging.Logger

def _get_logger(self) -> logging.Logger:
"""Get a `logging.Logger` based on the full class name"""
if self.__class__.__module__ == "__main__":
module_name = __main__.__spec__.name
else:
module_name = self.__class__.__module__
logger_name = f"{module_name}.{self.__class__.__qualname__}"
return logging.getLogger(logger_name)
@classmethod
def get_logger(cls, prefix: str = "") -> logging.Logger:
"""Get a `logging.Logger` for this class
Args:
prefix: optional prefix for the logger name
"""
return logging.getLogger(get_logger_name_for_class(cls, prefix))


class LoggingMixin(LoggingMixinBase):
Expand Down

0 comments on commit bd3383f

Please sign in to comment.