mirror of
https://github.com/Nanaloveyuki/py-logiliteal.git
synced 2025-09-03 18:16:22 +00:00
⚡️ 提升了time
类函数的效率和整洁度
This commit is contained in:
@ -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",
|
||||
|
@ -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"},
|
||||
}
|
||||
|
@ -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
|
32
tests/logger_config.json
Normal file
32
tests/logger_config.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user