Download src/bigru_t/utils/logging_utils.py from PowerMachine/BiGRU_T_version: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/PowerMachine/BiGRU_T_version/resolve/main/src/bigru_t/utils/logging_utils.py
- Command line
-
hf download hf://PowerMachine/BiGRU_T_version/src/bigru_t/utils/logging_utils.py
-
curl -L -o logging_utils.py https://huggingface.co/PowerMachine/BiGRU_T_version/resolve/main/src/bigru_t/utils/logging_utils.py
1.25 kB
| """Xavante - logging_utils.py - Logging estruturado com contexto.""" | |
| from __future__ import annotations | |
| import json | |
| import logging | |
| import sys | |
| from datetime import datetime | |
| from typing import Any | |
| class JSONFormatter(logging.Formatter): | |
| def format(self, record: logging.LogRecord) -> str: | |
| log_entry = { | |
| "timestamp": datetime.utcnow().isoformat() + "Z", | |
| "level": record.levelname, | |
| "logger": record.name, | |
| "message": record.getMessage(), | |
| } | |
| if hasattr(record, "context"): | |
| log_entry["context"] = record.context | |
| return json.dumps(log_entry, default=str) | |
| def setup_logging(level: int = logging.INFO, json_output: bool = False) -> None: | |
| root = logging.getLogger() | |
| root.setLevel(level) | |
| for h in list(root.handlers): | |
| root.removeHandler(h) | |
| handler = logging.StreamHandler(sys.stdout) | |
| if json_output: | |
| handler.setFormatter(JSONFormatter()) | |
| else: | |
| handler.setFormatter( | |
| logging.Formatter( | |
| "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s", | |
| datefmt="%Y-%m-%d %H:%M:%S", | |
| ) | |
| ) | |
| root.addHandler(handler) | |
| __all__ = ["JSONFormatter", "setup_logging"] | |