Difficult-Rocket/libs/msdnicrosoft_logger/shenjack.py

187 lines
5.1 KiB
Python
Raw Normal View History

2022-07-04 15:12:04 +08:00
"""
@author shenjackyuanjie
@contact 3695888@qq.com
"""
2022-06-27 14:42:26 +08:00
import atexit
2022-07-04 15:12:04 +08:00
import logging
2022-06-27 16:22:40 +08:00
import threading
2022-06-27 14:42:26 +08:00
2022-07-04 15:12:04 +08:00
from typing import Optional, Union
from logging import NOTSET, DEBUG, INFO, WARNING, ERROR, FATAL
from Difficult_Rocket.utils.thread import ThreadLock
2022-06-27 16:51:14 +08:00
2022-06-27 14:42:26 +08:00
color_reset_suffix = "\033[0m"
2022-07-04 15:12:04 +08:00
"""
logging.py
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
"""
2022-07-07 18:28:29 +08:00
DETAIL = 5
2022-07-04 15:12:04 +08:00
2022-06-27 14:42:26 +08:00
class LogFileCache:
"""日志文件缓存"""
2022-07-07 18:28:29 +08:00
def __init__(self, file_name: str = 'logs//log.log', flush_time: Optional[Union[int, float]] = 1, log_cache_lens_max: int = 10):
"""
@param file_name: 日志文件名称
@param flush_time: 刷新日志缓存写入文件的时长间隔
@param log_cache_lens_max: 日志缓存在自动写入前的最大缓存长度
"""
2022-06-27 14:42:26 +08:00
# 配置相关
2022-06-27 16:51:14 +08:00
self._logfile_name = file_name # log 文件名称
2022-06-27 14:42:26 +08:00
self.flush_time = flush_time # 缓存刷新时长
2022-07-07 18:28:29 +08:00
self.cache_entries_num = log_cache_lens_max
2022-06-27 14:42:26 +08:00
# 写入缓存数
self.cache_count = 0
2022-06-27 16:22:40 +08:00
# 日志缓存表
2022-06-29 13:45:25 +08:00
self.logs_cache = []
2022-06-27 16:22:40 +08:00
# 同步锁
2022-06-27 16:51:14 +08:00
self.thread_lock = threading.Lock()
2022-07-04 15:12:04 +08:00
self.with_thread_lock = ThreadLock(self.thread_lock)
self.threaded_write = threading.Timer(1, self._log_file_time_write)
def end_thread(self):
2022-07-07 18:28:29 +08:00
"""结束日志写入进程,顺手把目前的缓存写入"""
2022-07-04 15:12:04 +08:00
self.thread_lock.acquire(blocking=True)
self.threaded_write.cancel()
if self.cache_count:
self._log_file_time_write()
def start_thread(self):
self.threaded_write.start()
atexit.register(self.end_thread)
2022-06-27 16:51:14 +08:00
@property
def logfile_name(self) -> str:
return self._logfile_name
2022-06-27 14:42:26 +08:00
2022-06-27 16:51:14 +08:00
@logfile_name.setter
def logfile_name(self, value: str) -> None:
2022-07-04 15:12:04 +08:00
with self.with_thread_lock:
self._logfile_name = value
2022-06-27 14:42:26 +08:00
def _log_file_time_write(self) -> None:
"""使用 threading.Timer 调用的定时写入日志文件的函数"""
2022-07-04 15:12:04 +08:00
with self.with_thread_lock:
if self.cache_count == 0:
return None
2022-06-27 14:42:26 +08:00
...
2022-06-29 13:45:25 +08:00
def write_logs(self, string: str, wait_for_cache: bool = True) -> None:
2022-06-27 16:22:40 +08:00
if wait_for_cache:
2022-06-27 16:51:14 +08:00
with open(file=self.logfile_name, encoding='utf-8', mode='a') as log_file:
2022-06-29 13:45:25 +08:00
log_file.writelines(self.logs_cache)
2022-06-27 16:22:40 +08:00
log_file.write(string)
...
2022-06-27 14:42:26 +08:00
else:
...
class Logger:
"""shenjack logger"""
2022-07-04 15:12:04 +08:00
def __init__(self, config: dict = None, **kwargs) -> None:
"""请注意,如果需要获取一个"""
self.name = 'root'
2022-06-27 14:42:26 +08:00
if config is None:
2022-07-04 15:12:04 +08:00
if name := kwargs.pop('name', default=False):
self.name = name
2022-06-27 14:42:26 +08:00
else:
2022-07-04 15:12:04 +08:00
...
2022-06-27 14:42:26 +08:00
2022-07-07 18:28:29 +08:00
def make_log(self, level: int,
2022-07-04 15:12:04 +08:00
*values: object,
sep: Optional[str] = ' ',
end: Optional[str] = '\n',
flush: Optional[bool] = False) -> None:
...
2022-06-27 14:42:26 +08:00
2022-07-04 15:12:04 +08:00
def detail(self, *values: object,
sep: Optional[str] = ' ',
end: Optional[str] = '\n',
flush: Optional[bool] = False) -> None:
2022-07-07 18:28:29 +08:00
self.make_log(level=DETAIL, *values, sep=sep, end=end, flush=flush)
2022-07-04 15:12:04 +08:00
def debug(self,
*values: object,
sep: Optional[str] = ' ',
end: Optional[str] = '\n',
flush: Optional[bool] = False) -> None:
2022-07-07 18:28:29 +08:00
self.make_log(level=DEBUG, *values, sep=sep, end=end, flush=flush)
2022-06-27 14:42:26 +08:00
2022-07-04 15:12:04 +08:00
def info(self,
*values: object,
sep: Optional[str] = ' ',
end: Optional[str] = '\n',
flush: Optional[bool] = False) -> None:
2022-07-07 18:28:29 +08:00
self.make_log(level=INFO, *values, sep=sep, end=end, flush=flush)
2022-06-27 14:42:26 +08:00
2022-07-04 15:12:04 +08:00
def warning(self,
*values: object,
sep: Optional[str] = ' ',
end: Optional[str] = '\n',
flush: Optional[bool] = False) -> None:
2022-07-07 18:28:29 +08:00
self.make_log(level=WARNING, *values, sep=sep, end=end, flush=flush)
2022-07-04 15:12:04 +08:00
def error(self,
*values: object,
sep: Optional[str] = ' ',
end: Optional[str] = '\n',
flush: Optional[bool] = False) -> None:
2022-07-07 18:28:29 +08:00
self.make_log(level=ERROR, *values, sep=sep, end=end, flush=flush)
2022-07-04 15:12:04 +08:00
def fatal(self,
*values: object,
sep: Optional[str] = ' ',
end: Optional[str] = '\n',
flush: Optional[bool] = False) -> None:
2022-07-07 18:28:29 +08:00
self.make_log(level=FATAL, *values, sep=sep, end=end, flush=flush)
2022-06-27 14:42:26 +08:00
2022-06-29 13:45:25 +08:00
2022-07-07 18:28:29 +08:00
logger_configs = {
2022-07-04 15:12:04 +08:00
'root': {
'level': DEBUG,
'color': {
DEBUG: '\033[0m'
},
'file': {
'mode': 'a',
'encoding': 'utf-8'
},
...: ...
}
}
2022-07-07 18:28:29 +08:00
def add_dict_config_to_global(some_dict: dict, name: str) -> dict:
"""
@param some_dict: 一个你丢进来的 logger 设置
@param name: 这个 logger 设置的名称
@return: 修改过的 logger 配置
"""
logger_configs[name] = some_dict
return logger_configs
def add_kwargs_to_global(**kwargs) -> dict:
"""
@param kwargs:
@return:
"""
...