keep moving
This commit is contained in:
parent
5e6157a9df
commit
76e4b4a1cf
@ -6,30 +6,40 @@ version = 1.0
|
|||||||
level = 'DEBUG'
|
level = 'DEBUG'
|
||||||
color = 'main_color'
|
color = 'main_color'
|
||||||
file = 'main_log_file'
|
file = 'main_log_file'
|
||||||
format = 'format.main_format'
|
handlers = ['main_std_handler']
|
||||||
|
|
||||||
[Loggers.client]
|
[Loggers.client]
|
||||||
level = 'TRACE'
|
level = 'TRACE'
|
||||||
color = 'main_color'
|
color = 'main_color'
|
||||||
file = 'main_log_file'
|
file = 'main_log_file'
|
||||||
format = 'format.main_format'
|
handlers = ['main_std_handler']
|
||||||
|
|
||||||
[Loggers.server]
|
[Loggers.server]
|
||||||
level = 'TRACE'
|
level = 'TRACE'
|
||||||
color = 'DiGua_color'
|
color = 'DiGua_color'
|
||||||
file = 'main_log_file'
|
file = 'main_log_file'
|
||||||
format = 'format.main_format'
|
handlers = ['main_std_handler']
|
||||||
|
|
||||||
[Files]
|
[Files]
|
||||||
|
|
||||||
[Files.main_log_file]
|
[Files.main_log_file]
|
||||||
name = './logs/{file_time}_logs.md'
|
name = './logs/{long_time}_logs.md'
|
||||||
level = 'TRACE'
|
level = 'TRACE'
|
||||||
cache_len = 20
|
cache_len = 20
|
||||||
cache_time = 1
|
cache_time = 1
|
||||||
mode = 'a'
|
mode = 'a'
|
||||||
encoding = 'utf-8'
|
encoding = 'utf-8'
|
||||||
|
|
||||||
|
[Handler]
|
||||||
|
|
||||||
|
[Handler.main_std_handler]
|
||||||
|
class = 'str handler'
|
||||||
|
format = 'format.main_format'
|
||||||
|
|
||||||
|
[Handler.main_file_hander]
|
||||||
|
class = 'cached file handler'
|
||||||
|
format = 'format.main_format'
|
||||||
|
|
||||||
[Formatter]
|
[Formatter]
|
||||||
|
|
||||||
main_format = '[{long_time}] [{logger_name}] {level} | {file_name}:{code_line} | {message}'
|
main_format = '[{long_time}] [{logger_name}] {level} | {file_name}:{code_line} | {message}'
|
||||||
|
@ -434,14 +434,39 @@ class CachedFileHandler(StreamHandlerTemplate):
|
|||||||
"""
|
"""
|
||||||
super().__init__(level=level, formatter=formatter)
|
super().__init__(level=level, formatter=formatter)
|
||||||
if file_conf is not None:
|
if file_conf is not None:
|
||||||
self.file_conf = file_conf if type(
|
if isinstance(file_conf, dict):
|
||||||
file_conf) is LogFileConf else LogFileConf(**file_conf) # type: ignore
|
self.file_conf = LogFileConf(**file_conf)
|
||||||
|
elif isinstance(file_conf, LogFileConf):
|
||||||
|
self.file_conf = file_conf
|
||||||
else:
|
else:
|
||||||
...
|
self.file_conf = LogFileConf()
|
||||||
|
# 缓存
|
||||||
self.string_queue = Queue(maxsize=self.file_conf.file_cache_len)
|
self.string_queue = Queue(maxsize=self.file_conf.file_cache_len)
|
||||||
|
# 状态
|
||||||
|
self.started = True
|
||||||
|
self.running = False
|
||||||
|
# 同步锁
|
||||||
|
self.cache_lock = threading.Lock() # 主锁
|
||||||
|
self.time_limit_lock = ThreadLock(self.cache_lock, time_out=1 / 60) # 直接用于 with 的主锁
|
||||||
|
self.threaded_write = threading.Timer(1, self._thread_write, kwargs={'by_thread': True}) # 基于 timer 的多线程
|
||||||
|
|
||||||
|
def _thread_write(self, by_thread: bool) -> None:
|
||||||
|
if not self.string_queue.empty(): # 队列非空
|
||||||
|
if by_thread:
|
||||||
|
with self.time_limit_lock:
|
||||||
|
with open(file=self.file_conf.file_name, mode=self.file_conf.file_mode,
|
||||||
|
encoding=self.file_conf.file_encoding) as log_file:
|
||||||
|
...
|
||||||
|
|
||||||
def write(self, message: str, flush: Optional[bool]) -> bool:
|
def write(self, message: str, flush: Optional[bool]) -> bool:
|
||||||
|
if not flush:
|
||||||
|
if self.string_queue.qsize() + 1 <= self.file_conf.file_cache_len:
|
||||||
|
self.string_queue.put_nowait(message)
|
||||||
|
else:
|
||||||
...
|
...
|
||||||
|
else:
|
||||||
|
...
|
||||||
|
return True
|
||||||
|
|
||||||
def close(self) -> bool:
|
def close(self) -> bool:
|
||||||
...
|
...
|
||||||
@ -458,10 +483,8 @@ class LogFileCache:
|
|||||||
:param file_conf: 日志文件配置
|
:param file_conf: 日志文件配置
|
||||||
"""
|
"""
|
||||||
# 配置相关
|
# 配置相关
|
||||||
self._logfile_name = os.path.abspath(
|
self._logfile_name = os.path.abspath(format_str(file_conf['file_name'])) # log 文件名称
|
||||||
format_str(file_conf['file_name'])) # log 文件名称
|
self.level: logging_level_type = get_key_from_dict(file_conf, 'level', DEBUG)
|
||||||
self.level: logging_level_type = get_key_from_dict(
|
|
||||||
file_conf, 'level', DEBUG)
|
|
||||||
self.file_conf = file_conf
|
self.file_conf = file_conf
|
||||||
self.flush_time = file_conf['cache_time'] # 缓存刷新时长
|
self.flush_time = file_conf['cache_time'] # 缓存刷新时长
|
||||||
self.cache_entries_num = file_conf['cache_len']
|
self.cache_entries_num = file_conf['cache_len']
|
||||||
@ -469,10 +492,8 @@ class LogFileCache:
|
|||||||
self.running = False
|
self.running = False
|
||||||
# 同步锁
|
# 同步锁
|
||||||
self.cache_lock = threading.Lock() # 主锁
|
self.cache_lock = threading.Lock() # 主锁
|
||||||
self.time_limit_lock = ThreadLock(
|
self.time_limit_lock = ThreadLock(self.cache_lock, time_out=1 / 60) # 直接用于 with 的主锁
|
||||||
self.cache_lock, time_out=1 / 60) # 直接用于 with 的主锁
|
self.threaded_write = threading.Timer(1, self._log_file_time_write, kwargs={'thread': True}) # 基于 timer 的多线程
|
||||||
self.threaded_write = threading.Timer(1, self._log_file_time_write, kwargs={
|
|
||||||
'thread': True}) # 基于 timer 的多线程
|
|
||||||
# 日志缓存表
|
# 日志缓存表
|
||||||
self.log_cache = ListCache(self.time_limit_lock)
|
self.log_cache = ListCache(self.time_limit_lock)
|
||||||
self.file_setup()
|
self.file_setup()
|
||||||
|
Loading…
Reference in New Issue
Block a user