Compare commits
2 Commits
89669f8a15
...
37b8bd60a1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37b8bd60a1 | ||
|
|
4f8b67a794 |
@ -5,6 +5,7 @@ import logging.handlers
|
|||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
|
|
||||||
def setup_logging(
|
def setup_logging(
|
||||||
@ -25,6 +26,7 @@ def setup_logging(
|
|||||||
microseconds: int = 3, # 0, 3, or 6
|
microseconds: int = 3, # 0, 3, or 6
|
||||||
include_pid: bool = True,
|
include_pid: bool = True,
|
||||||
include_thread: bool = True,
|
include_thread: bool = True,
|
||||||
|
time_zone_name: str = "UTC",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Script-friendly logging:
|
Script-friendly logging:
|
||||||
@ -106,6 +108,7 @@ def setup_logging(
|
|||||||
include_name=include_name,
|
include_name=include_name,
|
||||||
include_pid=include_pid,
|
include_pid=include_pid,
|
||||||
include_thread=include_thread,
|
include_thread=include_thread,
|
||||||
|
time_zone_name=time_zone_name,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
formatter = _TextFormatter(
|
formatter = _TextFormatter(
|
||||||
@ -116,14 +119,15 @@ def setup_logging(
|
|||||||
include_name=include_name,
|
include_name=include_name,
|
||||||
include_pid=include_pid,
|
include_pid=include_pid,
|
||||||
include_thread=include_thread,
|
include_thread=include_thread,
|
||||||
|
time_zone_name=time_zone_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---- Console handler ----
|
# ---- Console handler ----
|
||||||
if use_rich and not json_logs:
|
if use_rich and not json_logs:
|
||||||
console = RichHandler(
|
console = RichHandler(
|
||||||
level=console_level,
|
level=console_level,
|
||||||
rich_tracebacks=True,
|
rich_tracebacks=False,
|
||||||
tracebacks_show_locals=True,
|
tracebacks_show_locals=False,
|
||||||
show_time=False, # YOU already handle time
|
show_time=False, # YOU already handle time
|
||||||
show_level=False, # YOU already handle level
|
show_level=False, # YOU already handle level
|
||||||
show_path=False, # avoid noise
|
show_path=False, # avoid noise
|
||||||
@ -181,7 +185,7 @@ def _level_from_env(val: str | None, *, default: int) -> int:
|
|||||||
return getattr(logging, name, default)
|
return getattr(logging, name, default)
|
||||||
|
|
||||||
|
|
||||||
def _format_timestamp(*, calendar: str, microseconds: int) -> str:
|
def _format_timestamp(*, calendar: str, microseconds: int, time_zone_name: str) -> str:
|
||||||
"""
|
"""
|
||||||
microseconds:
|
microseconds:
|
||||||
- 0: YYYY-mm-dd HH:MM:SS
|
- 0: YYYY-mm-dd HH:MM:SS
|
||||||
@ -191,7 +195,7 @@ def _format_timestamp(*, calendar: str, microseconds: int) -> str:
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
if calendar == "gregorian":
|
if calendar == "gregorian":
|
||||||
now = datetime.now()
|
now = datetime.now(tz=ZoneInfo(time_zone_name))
|
||||||
base = now.strftime("%Y-%m-%d %H:%M:%S")
|
base = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
if microseconds == 0:
|
if microseconds == 0:
|
||||||
return base
|
return base
|
||||||
@ -205,7 +209,7 @@ def _format_timestamp(*, calendar: str, microseconds: int) -> str:
|
|||||||
import jdatetime
|
import jdatetime
|
||||||
except Exception:
|
except Exception:
|
||||||
# fallback
|
# fallback
|
||||||
now = datetime.now()
|
now = datetime.now(tz=ZoneInfo(time_zone_name))
|
||||||
base = now.strftime("%Y-%m-%d %H:%M:%S")
|
base = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
if microseconds == 0:
|
if microseconds == 0:
|
||||||
return base + " (gregorian-fallback)"
|
return base + " (gregorian-fallback)"
|
||||||
@ -214,7 +218,7 @@ def _format_timestamp(*, calendar: str, microseconds: int) -> str:
|
|||||||
return f"{base}.{us // 1000:03d} (gregorian-fallback)"
|
return f"{base}.{us // 1000:03d} (gregorian-fallback)"
|
||||||
return f"{base}.{us:06d} (gregorian-fallback)"
|
return f"{base}.{us:06d} (gregorian-fallback)"
|
||||||
|
|
||||||
jnow = jdatetime.datetime.now()
|
jnow = jdatetime.datetime.now(tz=ZoneInfo(time_zone_name))
|
||||||
base = jnow.strftime("%Y-%m-%d %H:%M:%S")
|
base = jnow.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
if microseconds == 0:
|
if microseconds == 0:
|
||||||
return base
|
return base
|
||||||
@ -246,6 +250,7 @@ class _TextFormatter(logging.Formatter):
|
|||||||
include_name: bool,
|
include_name: bool,
|
||||||
include_pid: bool,
|
include_pid: bool,
|
||||||
include_thread: bool,
|
include_thread: bool,
|
||||||
|
time_zone_name: str,
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.app_name = app_name
|
self.app_name = app_name
|
||||||
@ -255,9 +260,10 @@ class _TextFormatter(logging.Formatter):
|
|||||||
self.include_name = include_name
|
self.include_name = include_name
|
||||||
self.include_pid = include_pid
|
self.include_pid = include_pid
|
||||||
self.include_thread = include_thread
|
self.include_thread = include_thread
|
||||||
|
self.time_zone_name = time_zone_name
|
||||||
|
|
||||||
def format(self, record: logging.LogRecord) -> str:
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
ts = _format_timestamp(calendar=self.calendar, microseconds=self.microseconds)
|
ts = _format_timestamp(calendar=self.calendar, microseconds=self.microseconds, time_zone_name=self.time_zone_name)
|
||||||
|
|
||||||
# best practice: keep a stable, grep-friendly prefix
|
# best practice: keep a stable, grep-friendly prefix
|
||||||
prefix_parts = [ts, record.levelname]
|
prefix_parts = [ts, record.levelname]
|
||||||
@ -289,6 +295,7 @@ class _JsonFormatter(logging.Formatter):
|
|||||||
include_name: bool,
|
include_name: bool,
|
||||||
include_pid: bool,
|
include_pid: bool,
|
||||||
include_thread: bool,
|
include_thread: bool,
|
||||||
|
time_zone_name: str,
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.app_name = app_name
|
self.app_name = app_name
|
||||||
@ -298,12 +305,15 @@ class _JsonFormatter(logging.Formatter):
|
|||||||
self.include_name = include_name
|
self.include_name = include_name
|
||||||
self.include_pid = include_pid
|
self.include_pid = include_pid
|
||||||
self.include_thread = include_thread
|
self.include_thread = include_thread
|
||||||
|
self.time_zone_name = time_zone_name
|
||||||
|
|
||||||
def format(self, record: logging.LogRecord) -> str:
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
import json
|
import json
|
||||||
|
|
||||||
payload: dict[str, object] = {
|
payload: dict[str, object] = {
|
||||||
"ts": _format_timestamp(calendar=self.calendar, microseconds=self.microseconds),
|
"ts": _format_timestamp(
|
||||||
|
calendar=self.calendar, microseconds=self.microseconds, time_zone_name=self.time_zone_name
|
||||||
|
),
|
||||||
"level": record.levelname,
|
"level": record.levelname,
|
||||||
"msg": record.getMessage(),
|
"msg": record.getMessage(),
|
||||||
}
|
}
|
||||||
@ -329,8 +339,9 @@ if __name__ == "__main__":
|
|||||||
include_name=True,
|
include_name=True,
|
||||||
include_pid=False,
|
include_pid=False,
|
||||||
include_thread=False,
|
include_thread=False,
|
||||||
console_level=logging.DEBUG,
|
console_level=logging.INFO,
|
||||||
use_rich=True,
|
use_rich=True,
|
||||||
|
time_zone_name="Asia/Tehran",
|
||||||
)
|
)
|
||||||
logging.getLogger("elasticsearch").setLevel(logging.WARNING)
|
logging.getLogger("elasticsearch").setLevel(logging.WARNING)
|
||||||
logging.getLogger("elastic_transport").setLevel(logging.WARNING)
|
logging.getLogger("elastic_transport").setLevel(logging.WARNING)
|
||||||
@ -338,6 +349,7 @@ if __name__ == "__main__":
|
|||||||
logging.getLogger("tortoise.db_client").setLevel(logging.WARNING)
|
logging.getLogger("tortoise.db_client").setLevel(logging.WARNING)
|
||||||
logging.getLogger("asyncio").setLevel(logging.WARNING)
|
logging.getLogger("asyncio").setLevel(logging.WARNING)
|
||||||
logging.getLogger("tortoise").setLevel(logging.WARNING)
|
logging.getLogger("tortoise").setLevel(logging.WARNING)
|
||||||
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
log.info("script_started")
|
log.info("script_started")
|
||||||
# time.sleep(2)
|
# time.sleep(2)
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
|
from collections.abc import Hashable
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import logging
|
||||||
|
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
@ -6,23 +8,22 @@ console = Console()
|
|||||||
|
|
||||||
timers_started_ats = dict()
|
timers_started_ats = dict()
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
def start_timer(name=1, do_print=True):
|
|
||||||
|
def start_timer(name: Hashable = 1, do_print=True):
|
||||||
started_at = datetime.now()
|
started_at = datetime.now()
|
||||||
timers_started_ats[name] = started_at
|
timers_started_ats[name] = started_at
|
||||||
if do_print:
|
if do_print:
|
||||||
console.print(f"{name:<{len(str(name)) + 4}} ", end="")
|
log.info("%s_started", name)
|
||||||
print(f"started at: {started_at.strftime('%H:%M:%S.%f')}")
|
|
||||||
return name, started_at
|
return name, started_at
|
||||||
|
|
||||||
|
|
||||||
def stop_timer(name=1, do_print=True):
|
def stop_timer(name: Hashable = 1, do_print=True):
|
||||||
ended_at = datetime.now()
|
ended_at = datetime.now()
|
||||||
took = ended_at - timers_started_ats[name]
|
took = ended_at - timers_started_ats[name]
|
||||||
if do_print:
|
if do_print:
|
||||||
console.print(f"{name:<{len(str(name)) + 4}} ", end="")
|
log.info("%s_ended took=%s", name, took.total_seconds())
|
||||||
print(f"{'ended at:':>11} {ended_at.strftime('%H:%M:%S.%f')} ", end="")
|
|
||||||
console.print(f"took: {took}")
|
|
||||||
return name, ended_at, took
|
return name, ended_at, took
|
||||||
|
|
||||||
|
|
||||||
@ -32,4 +33,3 @@ if __name__ == "__main__":
|
|||||||
start_timer()
|
start_timer()
|
||||||
sleep(1)
|
sleep(1)
|
||||||
stop_timer()
|
stop_timer()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user