-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8de19c
commit 4bea8e6
Showing
8 changed files
with
273 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,43 @@ | ||
import os | ||
import logging | ||
from typing import Any, MutableMapping | ||
|
||
from colorama import Fore, Style # type: ignore | ||
|
||
|
||
class PrefixedLogger(logging.LoggerAdapter): | ||
"""A logger adapter to prefix log messages.""" | ||
|
||
def __init__(self, prefix: str, logger: logging.Logger) -> None: | ||
""" | ||
Initialize the logger adapter. | ||
Arguments: | ||
prefix: The string to insert in front of every message. | ||
logger: The logger instance. | ||
""" | ||
super().__init__(logger, {}) | ||
self.prefix = prefix | ||
|
||
def process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, Any]: | ||
""" | ||
Process the message. | ||
Arguments: | ||
msg: The message: | ||
kwargs: Remaining arguments. | ||
Returns: | ||
The processed message. | ||
""" | ||
return f"{self.prefix}: {msg}", kwargs | ||
|
||
|
||
def get_plugin_logger(name: str) -> PrefixedLogger: | ||
""" | ||
Return a logger for plugins. | ||
Arguments: | ||
name: The name to use with `logging.getLogger`. | ||
Returns: | ||
A logger configured to work well in MkDocs, | ||
prefixing each message with the plugin package name. | ||
Example: | ||
```python | ||
from mkdocs.plugins import get_plugin_logger | ||
log = get_plugin_logger(__name__) | ||
log.info("My plugin message") | ||
``` | ||
""" | ||
DEBUG = os.getenv("DEBUG", "false").lower() == "true" | ||
print(f"{Fore.GREEN}DEBUG: {DEBUG}") | ||
|
||
class Logger(logging.Logger): | ||
def __init__(self, logger: logging.Logger): | ||
self.logger = logger | ||
super().__init__(logger.name, logger.level) | ||
|
||
def info(self, msg, *args, **kwargs): | ||
if DEBUG: | ||
self.debug(msg, *args, **kwargs) | ||
else: | ||
super().info(msg, *args, **kwargs) | ||
|
||
def debug(self, msg, *args, **kwargs): | ||
if DEBUG: | ||
print("-"*100) | ||
print(msg, *args, **kwargs) | ||
clear_line(2) | ||
else: | ||
super().debug(msg, *args, **kwargs) | ||
|
||
def get_plugin_logger(name: str) -> Logger: | ||
logger = logging.getLogger(f"mkdocs.plugins.{name}") | ||
setattr(logger, "info", lambda msg: clear_screen() and getattr(logger, "info")(msg)) | ||
return PrefixedLogger(name.split(".", 1)[0], logger) | ||
|
||
setattr(logger, "info", lambda msg: getattr(logger, "info")(msg)) | ||
return Logger(logger) | ||
|
||
log = get_plugin_logger(f"{Fore.BLUE}juvix_mkdocs{Style.RESET_ALL}") | ||
|
||
|
||
def clear_screen(): | ||
print("\033[H\033[J", end="", flush=True) | ||
|
||
|
||
def clear_line(): | ||
print("\033[A", end="", flush=True) | ||
print("\033[K", end="\r", flush=True) | ||
if os.getenv("DEBUG", "false").lower() != "true": | ||
print("\033[H\033[J", end="", flush=True) | ||
|
||
def clear_line(n=1): | ||
if os.getenv("DEBUG", "false").lower() != "true": | ||
for _ in range(n): | ||
print("\033[A", end="", flush=True) | ||
print("\033[K", end="\r", flush=True) |
Oops, something went wrong.