Enable analytix's preconfigured logger.
Parameters:
Name |
Type |
Description |
Default |
level |
int
|
|
INFO
|
Returns:
Type |
Description |
StreamHandler object
|
|
Examples:
>>> analytix.enable_logging(logging.DEBUG)
Enable the logger in DEBUG mode.
>>> analytix.enable_logging(logging.DEBUG)
Source code in analytix/ux.py
| def enable_logging(level: int = logging.INFO) -> "logging.StreamHandler[TextIO]":
"""Enable analytix's preconfigured logger.
Parameters
----------
level
The log level to use.
Returns
-------
StreamHandler object
The created log handler.
Examples
--------
>>> analytix.enable_logging(logging.DEBUG)
Enable the logger in DEBUG mode.
>>> analytix.enable_logging(logging.DEBUG)
"""
fmt = "{asctime}.{msecs:03.0f} [ {levelname:<7} ] {name}: {message}"
formats = {
logging.DEBUG: f"\33[38;5;244m{fmt}\33[0m",
logging.INFO: f"\33[38;5;248m{fmt}\33[0m",
logging.WARNING: f"\33[1m\33[38;5;178m{fmt}\33[0m",
logging.ERROR: f"\33[1m\33[38;5;196m{fmt}\33[0m",
logging.CRITICAL: f"\33[1m\33[48;5;196m{fmt}\33[0m",
}
class CustomFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
log_fmt = formats[record.levelno]
formatter = logging.Formatter(log_fmt, "%F %X", style="{")
return formatter.format(record)
handler = logging.StreamHandler()
handler.setFormatter(CustomFormatter())
logging.basicConfig(level=level, handlers=[handler])
logging._srcfile = None # noqa: SLF001
logging.logThreads = False
logging.logProcesses = False
logging.logMultiprocessing = False
def showwarning(
message: Union[Warning, str],
category: Type[Warning],
filename: str,
lineno: int,
file: Optional["TextIO"] = None,
line: Optional[str] = None,
) -> None:
for _module_name, module in sys.modules.items():
module_path = getattr(module, "__file__", None)
if module_path and os.path.samefile(module_path, filename):
break
else:
_module_name = os.path.splitext(os.path.split(filename)[1])[0]
log = logging.getLogger(_module_name)
log.warning(message)
warnings.simplefilter("always", DeprecationWarning)
warnings.showwarning = showwarning
return handler
|