Spaces:
Sleeping
Sleeping
fix:
Browse files
infra/__pycache__/logger_structlog.cpython-311.pyc
ADDED
|
Binary file (3.45 kB). View file
|
|
|
infra/__pycache__/logger_structlog.cpython-314.pyc
ADDED
|
Binary file (3.82 kB). View file
|
|
|
infra/__pycache__/system_metrics.cpython-311.pyc
ADDED
|
Binary file (1.43 kB). View file
|
|
|
infra/__pycache__/system_metrics.cpython-314.pyc
ADDED
|
Binary file (1.27 kB). View file
|
|
|
infra/logger_structlog.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import structlog
|
| 2 |
+
from config.settings import AppConfig
|
| 3 |
+
from domain.logger import Logger
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Don't forget to keep logs.json file meaningful.
|
| 8 |
+
def setup_logging(logs_path: Path | str):
|
| 9 |
+
log_file = open(logs_path, "a", encoding="utf-8")
|
| 10 |
+
structlog.configure(
|
| 11 |
+
processors = [
|
| 12 |
+
structlog.processors.StackInfoRenderer(), # Stack strace, showing the exact source of errors.
|
| 13 |
+
structlog.processors.format_exc_info, # for Exceptions in JSON
|
| 14 |
+
structlog.processors.add_log_level, # Adding log level (info, warning, error)
|
| 15 |
+
structlog.processors.TimeStamper(fmt="iso", utc=True), # Adding ISO timestamp
|
| 16 |
+
structlog.processors.JSONRenderer(), # Makes JSON outputs
|
| 17 |
+
],
|
| 18 |
+
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO), # Profiling info and higher.
|
| 19 |
+
logger_factory = structlog.WriteLoggerFactory(file=log_file), # Save in file instead of terminal
|
| 20 |
+
cache_logger_on_first_use=True, # Caching being used for optimization
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
class StructLogger(Logger):
|
| 24 |
+
def __init__(self, settings:AppConfig):
|
| 25 |
+
setup_logging(logs_path=settings.paths.logs_dir)
|
| 26 |
+
self._logger = structlog.get_logger()
|
| 27 |
+
|
| 28 |
+
def info(self, message:str, **kwargs):
|
| 29 |
+
print(message)
|
| 30 |
+
self._logger.info(message, **kwargs)
|
| 31 |
+
|
| 32 |
+
def debug(self, message:str, **kwargs):
|
| 33 |
+
print(message)
|
| 34 |
+
self._logger.debug(message, **kwargs)
|
| 35 |
+
|
| 36 |
+
def error(self, message:str, **kwargs):
|
| 37 |
+
print(message)
|
| 38 |
+
self._logger.error(message, **kwargs)
|
| 39 |
+
|
| 40 |
+
def warn(self, message: str, **kwargs):
|
| 41 |
+
# return super().warn(msg, **kwargs)
|
| 42 |
+
print(message)
|
| 43 |
+
self._logger.warn(message, **kwargs)
|
| 44 |
+
|
| 45 |
+
def exception(self, message:str, **kwargs):
|
| 46 |
+
print(message)
|
| 47 |
+
self._logger.exception(message, **kwargs)
|
infra/system_metrics.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from domain.logger import Logger
|
| 2 |
+
import psutil
|
| 3 |
+
import asyncio
|
| 4 |
+
from backend.api.routers.metrics import active_workers, cpu_usage, mem_usage
|
| 5 |
+
|
| 6 |
+
async def log_system_metrics(logger:Logger, logger_interval_sec:float):
|
| 7 |
+
while True:
|
| 8 |
+
cpu = psutil.cpu_percent(interval=1)
|
| 9 |
+
mem = psutil.virtual_memory()
|
| 10 |
+
|
| 11 |
+
# Structlog Logging
|
| 12 |
+
logger.info("System Metrics",
|
| 13 |
+
cpu_percent=cpu,
|
| 14 |
+
memtory_percent=mem.percent,
|
| 15 |
+
memory_used_gb=round(mem.used / (1024**3), 2),
|
| 16 |
+
memory_total_gb=round(mem.total / (1024**3), 2)
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Prometheus
|
| 20 |
+
cpu_usage.set(cpu)
|
| 21 |
+
mem_usage.set(mem.percent)
|
| 22 |
+
|
| 23 |
+
# active_workers.set()
|
| 24 |
+
|
| 25 |
+
await asyncio.sleep(logger_interval_sec)
|