From d3d9613acc3cafd6b0ad9d6a1ce1428c07774121 Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Mon, 28 Jul 2025 17:35:51 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E4=BA=86`time`=E7=B1=BB=E5=87=BD=E6=95=B0=E7=9A=84=E6=95=88?= =?UTF-8?q?=E7=8E=87=E5=92=8C=E6=95=B4=E6=B4=81=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger_config.json | 5 +++- src/logiliteal/utils/configs.py | 3 ++ src/logiliteal/utils/time.py | 49 +++++++++++++++++---------------- tests/logger_config.json | 32 +++++++++++++++++++++ 4 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 tests/logger_config.json diff --git a/logger_config.json b/logger_config.json index 2d06806..5194963 100644 --- a/logger_config.json +++ b/logger_config.json @@ -7,11 +7,14 @@ "enable_console": true, "enable_file": true, "console_color": true, - "console_level": "DEBUG", + "console_level": "INFO", "console_format": "{time} {levelname} | {prefix}{message}", "console_prefix": "Auto", "console_encoding": "utf-8", "asctime_format": "%Y-%m-%d %H:%M:%S", + "time_format": "%H:%M:%S", + "date_format": "%Y-%m-%d", + "weekday_format": "%A", "level_name": { "DEBUG": "DEBUG", "INFO": "INFO", diff --git a/src/logiliteal/utils/configs.py b/src/logiliteal/utils/configs.py index 57ab510..97a1c07 100644 --- a/src/logiliteal/utils/configs.py +++ b/src/logiliteal/utils/configs.py @@ -28,6 +28,9 @@ DEFAULT_CONFIG = { "console_prefix": "Auto", "console_encoding": "utf-8", "asctime_format": "%Y-%m-%d %H:%M:%S", + "time_format": "%H:%M:%S", + "date_format": "%Y-%m-%d", + "weekday_format": "%A", "level_name": {"DEBUG": "DEBUG", "INFO": "INFO", "WARN": "WARN", "ERRO": "ERRO", "CRIT": "CRIT"}, "level_color": {"DEBUG": "#c1d5ff", "INFO": "#c1ffff", "WARN": "#fff600", "ERRO": "#ffa000", "CRIT": "#ff8181"}, } diff --git a/src/logiliteal/utils/time.py b/src/logiliteal/utils/time.py index 86fbb8d..be8b5d8 100644 --- a/src/logiliteal/utils/time.py +++ b/src/logiliteal/utils/time.py @@ -9,10 +9,11 @@ Time utility module, used for time formatting and output, caching time formattin from datetime import datetime from .configs import get_config +import time + cache_time: str = "" -cache_asctime: str = "" -cache_date: str = "" -cache_weekday: str = "" +cache_time_ts: float = 0.0 +cache_fmt: str | None = None def get_asctime() -> str: """ @@ -20,11 +21,7 @@ def get_asctime() -> str: Get current time(YYYY-MM-DD HH:MM:SS) and cache formatted result :return: 格式化后的时间 Formatted time """ - global cache_asctime - if cache_asctime: - return cache_asctime - cache_asctime = datetime.now().strftime(get_config("asctime_format")) - return cache_asctime + return _get_time(get_config("asctime_format")) def get_time() -> str: """ @@ -32,24 +29,15 @@ def get_time() -> str: Get current time(HH:MM:SS) and cache formatted result :return: 格式化后的时间 Formatted time """ - global cache_time - if cache_time: - return cache_time - cache_time = datetime.now().strftime("%H:%M:%S") - return cache_time + return _get_time(get_config("time_format")) def get_date() -> str: """ 获取当前日期(YYYY-MM-DD),并缓存格式化结果 - 获取当前日期(星期几),并缓存格式化结果 Get current date(YYYY-MM-DD) and cache formatted result :return: 格式化后的日期 Formatted date """ - global cache_date - if cache_date: - return cache_date - cache_date = datetime.now().strftime("%Y-%m-%d") - return cache_date + return _get_time(get_config("date_format")) def get_weekday() -> str: """ @@ -57,8 +45,21 @@ def get_weekday() -> str: Get current date(weekday) and cache formatted result :return: 格式化后的星期几 Formatted weekday """ - global cache_weekday - if cache_weekday: - return cache_weekday - cache_weekday = datetime.now().strftime("%A") - return cache_weekday + return _get_time(get_config("weekday_format")) + +def _get_time(fmt: str) -> str: + """ + 获取当前时间(根据指定格式),并缓存格式化结果 + Get current time(based on specified format) and cache formatted result + :param fmt: 时间格式 Time format + :return: 格式化后的时间 Formatted time + """ + global cache_time, cache_time_ts, cache_fmt + if cache_fmt is None: + cache_fmt = fmt + now = time.time() + if cache_time and (now - cache_time_ts < 1) and (cache_fmt == fmt): + return cache_time + cache_time = datetime.now().strftime(fmt) + cache_time_ts = now + return cache_time \ No newline at end of file diff --git a/tests/logger_config.json b/tests/logger_config.json new file mode 100644 index 0000000..5194963 --- /dev/null +++ b/tests/logger_config.json @@ -0,0 +1,32 @@ +{ + "file_level": "DEBUG", + "file_name": "latest.log", + "file_path": "./logs", + "file_format": "{asctime} {levelname} | {prefix}{message}", + "file_encoding": "utf-8", + "enable_console": true, + "enable_file": true, + "console_color": true, + "console_level": "INFO", + "console_format": "{time} {levelname} | {prefix}{message}", + "console_prefix": "Auto", + "console_encoding": "utf-8", + "asctime_format": "%Y-%m-%d %H:%M:%S", + "time_format": "%H:%M:%S", + "date_format": "%Y-%m-%d", + "weekday_format": "%A", + "level_name": { + "DEBUG": "DEBUG", + "INFO": "INFO", + "WARN": "WARN", + "ERRO": "ERRO", + "CRIT": "CRIT" + }, + "level_color": { + "DEBUG": "#c1d5ff", + "INFO": "#c1ffff", + "WARN": "#fff600", + "ERRO": "#ffa000", + "CRIT": "#ff8181" + } +} \ No newline at end of file