Compare commits

..

No commits in common. "37b8bd60a16a61a92dc522e88ef756fc2cb927c3" and "89669f8a1594c1e0b1acbf0aa329386172410df2" have entirely different histories.

2 changed files with 17 additions and 29 deletions

View File

@ -5,7 +5,6 @@ import logging.handlers
import os
from pathlib import Path
import sys
from zoneinfo import ZoneInfo
def setup_logging(
@ -26,7 +25,6 @@ def setup_logging(
microseconds: int = 3, # 0, 3, or 6
include_pid: bool = True,
include_thread: bool = True,
time_zone_name: str = "UTC",
) -> None:
"""
Script-friendly logging:
@ -108,7 +106,6 @@ def setup_logging(
include_name=include_name,
include_pid=include_pid,
include_thread=include_thread,
time_zone_name=time_zone_name,
)
else:
formatter = _TextFormatter(
@ -119,15 +116,14 @@ def setup_logging(
include_name=include_name,
include_pid=include_pid,
include_thread=include_thread,
time_zone_name=time_zone_name,
)
# ---- Console handler ----
if use_rich and not json_logs:
console = RichHandler(
level=console_level,
rich_tracebacks=False,
tracebacks_show_locals=False,
rich_tracebacks=True,
tracebacks_show_locals=True,
show_time=False, # YOU already handle time
show_level=False, # YOU already handle level
show_path=False, # avoid noise
@ -185,7 +181,7 @@ def _level_from_env(val: str | None, *, default: int) -> int:
return getattr(logging, name, default)
def _format_timestamp(*, calendar: str, microseconds: int, time_zone_name: str) -> str:
def _format_timestamp(*, calendar: str, microseconds: int) -> str:
"""
microseconds:
- 0: YYYY-mm-dd HH:MM:SS
@ -195,7 +191,7 @@ def _format_timestamp(*, calendar: str, microseconds: int, time_zone_name: str)
from datetime import datetime
if calendar == "gregorian":
now = datetime.now(tz=ZoneInfo(time_zone_name))
now = datetime.now()
base = now.strftime("%Y-%m-%d %H:%M:%S")
if microseconds == 0:
return base
@ -209,7 +205,7 @@ def _format_timestamp(*, calendar: str, microseconds: int, time_zone_name: str)
import jdatetime
except Exception:
# fallback
now = datetime.now(tz=ZoneInfo(time_zone_name))
now = datetime.now()
base = now.strftime("%Y-%m-%d %H:%M:%S")
if microseconds == 0:
return base + " (gregorian-fallback)"
@ -218,7 +214,7 @@ def _format_timestamp(*, calendar: str, microseconds: int, time_zone_name: str)
return f"{base}.{us // 1000:03d} (gregorian-fallback)"
return f"{base}.{us:06d} (gregorian-fallback)"
jnow = jdatetime.datetime.now(tz=ZoneInfo(time_zone_name))
jnow = jdatetime.datetime.now()
base = jnow.strftime("%Y-%m-%d %H:%M:%S")
if microseconds == 0:
return base
@ -250,7 +246,6 @@ class _TextFormatter(logging.Formatter):
include_name: bool,
include_pid: bool,
include_thread: bool,
time_zone_name: str,
):
super().__init__()
self.app_name = app_name
@ -260,10 +255,9 @@ class _TextFormatter(logging.Formatter):
self.include_name = include_name
self.include_pid = include_pid
self.include_thread = include_thread
self.time_zone_name = time_zone_name
def format(self, record: logging.LogRecord) -> str:
ts = _format_timestamp(calendar=self.calendar, microseconds=self.microseconds, time_zone_name=self.time_zone_name)
ts = _format_timestamp(calendar=self.calendar, microseconds=self.microseconds)
# best practice: keep a stable, grep-friendly prefix
prefix_parts = [ts, record.levelname]
@ -295,7 +289,6 @@ class _JsonFormatter(logging.Formatter):
include_name: bool,
include_pid: bool,
include_thread: bool,
time_zone_name: str,
):
super().__init__()
self.app_name = app_name
@ -305,15 +298,12 @@ class _JsonFormatter(logging.Formatter):
self.include_name = include_name
self.include_pid = include_pid
self.include_thread = include_thread
self.time_zone_name = time_zone_name
def format(self, record: logging.LogRecord) -> str:
import json
payload: dict[str, object] = {
"ts": _format_timestamp(
calendar=self.calendar, microseconds=self.microseconds, time_zone_name=self.time_zone_name
),
"ts": _format_timestamp(calendar=self.calendar, microseconds=self.microseconds),
"level": record.levelname,
"msg": record.getMessage(),
}
@ -339,9 +329,8 @@ if __name__ == "__main__":
include_name=True,
include_pid=False,
include_thread=False,
console_level=logging.INFO,
console_level=logging.DEBUG,
use_rich=True,
time_zone_name="Asia/Tehran",
)
logging.getLogger("elasticsearch").setLevel(logging.WARNING)
logging.getLogger("elastic_transport").setLevel(logging.WARNING)
@ -349,7 +338,6 @@ if __name__ == "__main__":
logging.getLogger("tortoise.db_client").setLevel(logging.WARNING)
logging.getLogger("asyncio").setLevel(logging.WARNING)
logging.getLogger("tortoise").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)
log = logging.getLogger(__name__)
log.info("script_started")
# time.sleep(2)

View File

@ -1,6 +1,4 @@
from collections.abc import Hashable
from datetime import datetime
import logging
from rich.console import Console
@ -8,22 +6,23 @@ console = Console()
timers_started_ats = dict()
log = logging.getLogger(__name__)
def start_timer(name: Hashable = 1, do_print=True):
def start_timer(name=1, do_print=True):
started_at = datetime.now()
timers_started_ats[name] = started_at
if do_print:
log.info("%s_started", name)
console.print(f"{name:<{len(str(name)) + 4}} ", end="")
print(f"started at: {started_at.strftime('%H:%M:%S.%f')}")
return name, started_at
def stop_timer(name: Hashable = 1, do_print=True):
def stop_timer(name=1, do_print=True):
ended_at = datetime.now()
took = ended_at - timers_started_ats[name]
if do_print:
log.info("%s_ended took=%s", name, took.total_seconds())
console.print(f"{name:<{len(str(name)) + 4}} ", end="")
print(f"{'ended at:':>11} {ended_at.strftime('%H:%M:%S.%f')} ", end="")
console.print(f"took: {took}")
return name, ended_at, took
@ -33,3 +32,4 @@ if __name__ == "__main__":
start_timer()
sleep(1)
stop_timer()