Skip to content

Commit

Permalink
refactor: Allow specifying name of template loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 27, 2024
1 parent 7ff1681 commit c5b5f69
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/mkdocstrings/handlers/base.py
Expand Up @@ -74,6 +74,7 @@ class BaseHandler:
To add custom CSS, add an `extra_css` variable or create an 'style.css' file beside the templates.
"""

# TODO: Make name mandatory?
name: str = ""
"""The handler's name, for example "python"."""
domain: str = "default"
Expand Down Expand Up @@ -132,7 +133,7 @@ def __init__(self, handler: str, theme: str, custom_templates: str | None = None
auto_reload=False, # Editing a template in the middle of a build is not useful.
)
self.env.filters["any"] = do_any
self.env.globals["log"] = get_template_logger()
self.env.globals["log"] = get_template_logger(self.name)

self._headings: list[Element] = []
self._md: Markdown = None # type: ignore[assignment] # To be populated in `update_env`.
Expand Down
8 changes: 6 additions & 2 deletions src/mkdocstrings/loggers.py
Expand Up @@ -177,10 +177,14 @@ def get_logger(name: str) -> LoggerAdapter:
return LoggerAdapter(name.split(".", 1)[0], logger)


def get_template_logger() -> TemplateLogger:
def get_template_logger(handler_name: str | None = None) -> TemplateLogger:
"""Return a logger usable in templates.
Parameters:
handler_name: The name of the handler.
Returns:
A template logger.
"""
return TemplateLogger(get_logger("mkdocstrings.templates"))
handler_name = handler_name or "base"
return TemplateLogger(get_logger(f"mkdocstrings_handlers.{handler_name}.templates"))

0 comments on commit c5b5f69

Please sign in to comment.