2022-07-04 15:12:04 +08:00
|
|
|
|
"""
|
|
|
|
|
@author shenjackyuanjie
|
|
|
|
|
@contact 3695888@qq.com
|
|
|
|
|
"""
|
2022-09-06 21:47:46 +08:00
|
|
|
|
# -------------------------------
|
|
|
|
|
# Difficult Rocket
|
2023-01-20 14:08:12 +08:00
|
|
|
|
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
2022-09-06 21:47:46 +08:00
|
|
|
|
# All rights reserved
|
|
|
|
|
# -------------------------------
|
2022-11-26 21:48:55 +08:00
|
|
|
|
import io
|
2022-08-12 21:07:36 +08:00
|
|
|
|
import os
|
2022-11-08 20:18:01 +08:00
|
|
|
|
import re
|
2022-11-11 09:33:34 +08:00
|
|
|
|
import sys
|
2022-09-03 22:35:57 +08:00
|
|
|
|
import time
|
2022-12-25 23:15:57 +08:00
|
|
|
|
import enum
|
2022-06-27 14:42:26 +08:00
|
|
|
|
import atexit
|
2022-09-03 22:35:57 +08:00
|
|
|
|
import inspect
|
2022-12-29 10:13:26 +08:00
|
|
|
|
import warnings
|
2022-06-27 16:22:40 +08:00
|
|
|
|
import threading
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-09-03 22:35:57 +08:00
|
|
|
|
from types import FrameType
|
2023-01-03 15:34:22 +08:00
|
|
|
|
from dataclasses import dataclass
|
2022-11-11 09:33:34 +08:00
|
|
|
|
from logging import NOTSET, DEBUG
|
2023-03-02 13:15:29 +08:00
|
|
|
|
from typing import NamedTuple, Optional, Type, Union, Dict, Iterable, List
|
2022-11-01 18:34:02 +08:00
|
|
|
|
|
2022-12-29 10:13:26 +08:00
|
|
|
|
Version = '1.1.0'
|
2022-11-15 06:30:33 +08:00
|
|
|
|
|
2022-12-09 21:06:15 +08:00
|
|
|
|
# os.system('')
|
|
|
|
|
color_support = True
|
|
|
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
|
try:
|
|
|
|
|
# https://stackoverflow.com/questions/36760127/...
|
|
|
|
|
# how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console
|
|
|
|
|
from ctypes import windll
|
2022-12-25 23:15:57 +08:00
|
|
|
|
|
2022-12-09 21:06:15 +08:00
|
|
|
|
kernel32 = windll.kernel32
|
|
|
|
|
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
|
|
|
|
|
except OSError: # pragma: no cover
|
|
|
|
|
color_support = False
|
|
|
|
|
|
2022-09-04 13:02:58 +08:00
|
|
|
|
# print(os.path.abspath(os.curdir))
|
2022-11-08 20:18:01 +08:00
|
|
|
|
# TODO 这个文件就是个大TODO
|
2022-10-02 22:24:38 +08:00
|
|
|
|
"""
|
|
|
|
|
如果想要直接使用 logger 来 logging
|
|
|
|
|
直接调用 logger.debug() 即可
|
|
|
|
|
默认配置会有
|
|
|
|
|
----------
|
|
|
|
|
配置方式一
|
|
|
|
|
直接使用 logger.Logger()
|
|
|
|
|
将会创建一个空 logger
|
|
|
|
|
可以自行通过
|
|
|
|
|
配置方式二
|
|
|
|
|
logger = logger.get_logger(name)
|
|
|
|
|
直接获取一个配置好的logger
|
|
|
|
|
"""
|
2022-08-16 13:25:44 +08:00
|
|
|
|
|
2022-06-27 14:42:26 +08:00
|
|
|
|
color_reset_suffix = "\033[0m"
|
2022-07-25 18:28:42 +08:00
|
|
|
|
""" 只是用来重置颜色的后缀 """
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-09-03 22:35:57 +08:00
|
|
|
|
re_find_color_code = r'\033\[[^\f\n\r\t\vm]*m'
|
2022-09-06 21:47:46 +08:00
|
|
|
|
re_color_code = re.compile(re_find_color_code)
|
2022-12-29 10:13:26 +08:00
|
|
|
|
re_find_formats = re.compile(r'\{\w+}')
|
|
|
|
|
# re_find_level_code = r''
|
2022-10-29 19:13:00 +08:00
|
|
|
|
|
2022-07-04 15:12:04 +08:00
|
|
|
|
"""
|
2022-07-25 18:28:42 +08:00
|
|
|
|
OFF > FATAL > ERROR > WARN > INFO > FINE > FINER > DEBUG > TRACE > ALL
|
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-08-03 20:22:36 +08:00
|
|
|
|
FINE = 7
|
2022-10-02 22:24:38 +08:00
|
|
|
|
TRACE = 5
|
|
|
|
|
ALL = NOTSET
|
2022-07-07 18:28:29 +08:00
|
|
|
|
|
2022-10-01 12:47:48 +08:00
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
class LoggingLevel:
|
2022-10-02 22:24:38 +08:00
|
|
|
|
"""定义LoggingLevel属性(即是变量) """
|
2022-10-01 12:47:48 +08:00
|
|
|
|
CRITICAL = 50
|
|
|
|
|
FATAL = CRITICAL
|
|
|
|
|
ERROR = 40
|
|
|
|
|
WARNING = 30
|
|
|
|
|
WARN = WARNING
|
|
|
|
|
INFO = 20
|
|
|
|
|
DEBUG = 10
|
|
|
|
|
FINE = 7
|
|
|
|
|
TRACE = 5
|
|
|
|
|
NOTSET = 0
|
|
|
|
|
ALL = NOTSET
|
2022-11-01 18:34:02 +08:00
|
|
|
|
CRITICAL_t = 'CRITICAL'
|
|
|
|
|
FATAL_t = 'FATAL'
|
|
|
|
|
ERROR_t = 'ERROR'
|
|
|
|
|
WARNING_t = 'WARNING'
|
|
|
|
|
WARN_t = 'WARN'
|
|
|
|
|
INFO_t = 'INFO'
|
|
|
|
|
DEBUG_t = 'DEBUG'
|
|
|
|
|
FINE_t = 'FINE'
|
|
|
|
|
TRACE_t = 'TRACE'
|
|
|
|
|
NOTSET_t = 'NOTSET'
|
|
|
|
|
ALL_t = 'ALL'
|
2022-10-01 12:47:48 +08:00
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
@staticmethod
|
2022-11-01 18:34:02 +08:00
|
|
|
|
def type() -> Type:
|
2022-10-11 21:53:55 +08:00
|
|
|
|
return int
|
|
|
|
|
|
2022-10-01 12:47:48 +08:00
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
logging_level_type = int
|
|
|
|
|
|
|
|
|
|
level_name_map: Dict[logging_level_type, str] = {
|
2022-12-25 23:15:57 +08:00
|
|
|
|
LoggingLevel.ALL: 'ALL', # NOTSET
|
|
|
|
|
LoggingLevel.TRACE: 'TRACE',
|
|
|
|
|
LoggingLevel.FINE: 'FINE',
|
|
|
|
|
LoggingLevel.DEBUG: 'DEBUG',
|
|
|
|
|
LoggingLevel.INFO: 'INFO',
|
2022-10-02 22:24:38 +08:00
|
|
|
|
LoggingLevel.WARNING: 'WARNING', # WARN
|
2022-12-25 23:15:57 +08:00
|
|
|
|
LoggingLevel.ERROR: 'ERROR',
|
|
|
|
|
LoggingLevel.FATAL: 'FATAL'
|
2022-08-12 21:07:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
name_level_map: Dict[str, logging_level_type] = {
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'NOTSET': LoggingLevel.ALL,
|
|
|
|
|
'ALL': LoggingLevel.ALL,
|
|
|
|
|
'TRACE': LoggingLevel.TRACE,
|
|
|
|
|
'FINE': LoggingLevel.FINE,
|
|
|
|
|
'DEBUG': LoggingLevel.DEBUG,
|
|
|
|
|
'INFO': LoggingLevel.INFO,
|
|
|
|
|
'WARNING': LoggingLevel.WARNING,
|
|
|
|
|
'WARN': LoggingLevel.WARNING,
|
|
|
|
|
'ERROR': LoggingLevel.ERROR,
|
2022-10-02 22:24:38 +08:00
|
|
|
|
'CRITICAL': LoggingLevel.FATAL,
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'FATAL': LoggingLevel.FATAL
|
2022-08-12 21:07:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
|
|
|
|
|
def get_level_by_name(name: str) -> logging_level_type:
|
|
|
|
|
return name_level_map[name.upper()]
|
|
|
|
|
|
|
|
|
|
|
2022-11-01 18:34:02 +08:00
|
|
|
|
def get_name_by_level(level: logging_level_type) -> str:
|
|
|
|
|
return level_name_map[level]
|
2022-10-02 22:24:38 +08:00
|
|
|
|
|
|
|
|
|
|
2022-09-03 22:35:57 +08:00
|
|
|
|
logger_configs = {
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'Logger': {
|
|
|
|
|
'root': {
|
2022-10-29 19:13:00 +08:00
|
|
|
|
'level': DEBUG,
|
2022-09-03 22:35:57 +08:00
|
|
|
|
'color': 'main_color',
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'file': 'main_log_file',
|
2022-09-03 22:35:57 +08:00
|
|
|
|
},
|
2022-09-04 01:26:20 +08:00
|
|
|
|
'client': {
|
2022-10-29 19:13:00 +08:00
|
|
|
|
'level': TRACE,
|
2022-11-27 13:45:59 +08:00
|
|
|
|
'color': 'fancy_main_color',
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'file': 'main_log_file',
|
2022-09-04 01:26:20 +08:00
|
|
|
|
},
|
|
|
|
|
'server': {
|
2022-10-29 19:13:00 +08:00
|
|
|
|
'level': TRACE,
|
2022-09-04 13:53:09 +08:00
|
|
|
|
'color': 'DiGua_color',
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'file': 'main_log_file',
|
2022-09-04 01:26:20 +08:00
|
|
|
|
},
|
2022-11-01 18:34:02 +08:00
|
|
|
|
|
2022-09-03 22:35:57 +08:00
|
|
|
|
},
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'Color': {
|
|
|
|
|
'main_color': {
|
|
|
|
|
'main_time': '\033[38;2;201;222;56m',
|
|
|
|
|
'file_name': '\033[38;2;0;255;180m',
|
|
|
|
|
'code_line': '\033[38;2;0;255;180m',
|
|
|
|
|
'info': '\033[0m',
|
|
|
|
|
'message': '\033[0m',
|
|
|
|
|
'logger': '\033[0m',
|
|
|
|
|
'marker': '\033[0m',
|
|
|
|
|
LoggingLevel.TRACE_t: {'info': '\033[38;2;138;173;244m'},
|
|
|
|
|
LoggingLevel.FINE_t: {'info': '\033[35;48;2;44;44;54m'},
|
|
|
|
|
LoggingLevel.DEBUG_t: {'info': '\033[38;2;133;138;149m'},
|
|
|
|
|
LoggingLevel.INFO_t: {'info': '\033[0m'},
|
2022-11-01 18:34:02 +08:00
|
|
|
|
LoggingLevel.WARNING_t: {'info': '\033[33m'},
|
2022-12-25 23:15:57 +08:00
|
|
|
|
LoggingLevel.ERROR_t: {'info': '\033[31m'},
|
|
|
|
|
LoggingLevel.FATAL_t: {'info': '\033[38;2;255;255;0;48;2;120;10;10m', 'logger': '\033[38;2;245;189;230m'}
|
2022-10-29 19:13:00 +08:00
|
|
|
|
},
|
|
|
|
|
'fancy_main_color': {
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'main_time': '\033[38;2;201;222;56m',
|
|
|
|
|
'file_name': '\033[38;2;0;255;180m',
|
|
|
|
|
'code_line': '\033[38;2;0;255;180m',
|
|
|
|
|
'info': '\033[0m',
|
|
|
|
|
'message': '\033[0m',
|
|
|
|
|
'logger': '\033[0m',
|
|
|
|
|
'marker': '\033[0m',
|
|
|
|
|
LoggingLevel.TRACE_t: {'info': '\033[38;2;138;173;244m', 'message': '\033[38;2;138;173;244m'},
|
|
|
|
|
LoggingLevel.FINE_t: {'info': '\033[35;48;2;44;44;54m', 'message': '\033[35m'},
|
|
|
|
|
LoggingLevel.DEBUG_t: {'info': '\033[38;2;133;138;149m', 'message': '\033[38;2;133;138;149m'},
|
|
|
|
|
LoggingLevel.INFO_t: {'info': '\033[0m', 'message': '\033[0m'},
|
2022-11-01 18:34:02 +08:00
|
|
|
|
LoggingLevel.WARNING_t: {'info': '\033[33m', 'message': '\033[33m'},
|
2022-12-25 23:15:57 +08:00
|
|
|
|
LoggingLevel.ERROR_t: {'info': '\033[31m', 'message': '\033[31m'},
|
|
|
|
|
LoggingLevel.FATAL_t: {'info': '\033[38;2;255;255;0;48;2;120;10;10m',
|
|
|
|
|
'message': '\033[38;2;255;255;0;48;2;120;10;10m', 'logger': '\033[38;2;245;189;230m'}
|
2022-09-04 13:02:58 +08:00
|
|
|
|
},
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'DiGua_color': {
|
2022-09-04 13:53:09 +08:00
|
|
|
|
# catppuccin Macchiato
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'main_time': '\033[38;2;202;211;245m',
|
|
|
|
|
'file_name': '\033[38;2;139;213;202m',
|
|
|
|
|
'code_line': '\033[38;2;166;218;149m',
|
|
|
|
|
'info': '\033[0m',
|
|
|
|
|
'logger': '\033[0m',
|
|
|
|
|
'message': '\033[0m',
|
|
|
|
|
'marker': '\033[0m',
|
|
|
|
|
LoggingLevel.TRACE_t: {'info': '\033[38;2;138;173;244m', 'message': '\033[38;2;138;173;244m'},
|
|
|
|
|
LoggingLevel.FINE_t: {'info': '\033[38;2;198;160;246m', 'message': '\033[38;2;198;160;246m'},
|
|
|
|
|
LoggingLevel.DEBUG_t: {'info': '\033[38;2;133;138;149m', 'message': '\033[38;2;133;138;149m'},
|
|
|
|
|
LoggingLevel.INFO_t: {'info': '\033[0m', 'message': '\033[0m'},
|
2022-11-01 18:34:02 +08:00
|
|
|
|
LoggingLevel.WARNING_t: {'info': '\033[38;2;245;169;127m', 'message': '\033[38;2;245;169;127m'},
|
2022-12-25 23:15:57 +08:00
|
|
|
|
LoggingLevel.ERROR_t: {'info': '\033[38;2;237;135;150m', 'message': '\033[38;2;237;135;150m'},
|
|
|
|
|
LoggingLevel.FATAL_t: {'info': '\033[38;2;255;255;0;48;2;120;10;10m',
|
|
|
|
|
'message': '\033[38;2;255;255;0;48;2;120;10;10m', 'logger': '\033[38;2;245;189;230m'}
|
2022-09-03 22:35:57 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'File': {
|
2022-09-03 22:35:57 +08:00
|
|
|
|
'main_log_file': {
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'mode': 'a',
|
|
|
|
|
'encoding': 'utf-8',
|
|
|
|
|
'level': TRACE,
|
|
|
|
|
'file_name': './logs/{long_time}_logs.md',
|
|
|
|
|
'cache_len': 10,
|
2022-09-04 13:02:58 +08:00
|
|
|
|
'cache_time': 1
|
2022-09-03 22:35:57 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
'Formatter': {
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'MESSAGE': {
|
2022-11-01 18:34:02 +08:00
|
|
|
|
'format': '[{long_time}] [{logger_name}] {level} | {file_name}:{code_line} | {message}'
|
2022-09-03 22:35:57 +08:00
|
|
|
|
},
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'file_name': 'no frame',
|
|
|
|
|
'code_line': 'no frame',
|
2022-11-01 18:34:02 +08:00
|
|
|
|
'short_time': '%Y-%m-%d %H-%M-%S',
|
2022-12-25 23:15:57 +08:00
|
|
|
|
'long_time': '%Y-%m-%d %H-%M-%S:%%S',
|
2022-09-03 22:35:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 21:07:36 +08:00
|
|
|
|
|
2023-01-03 15:34:22 +08:00
|
|
|
|
@dataclass
|
2022-10-29 19:13:00 +08:00
|
|
|
|
class LogFileConf:
|
2022-11-11 09:33:34 +08:00
|
|
|
|
file_name: str = 'logs/log.txt'
|
|
|
|
|
file_mode: str = 'a'
|
|
|
|
|
file_encoding: str = 'utf-8'
|
|
|
|
|
file_level: logging_level_type = LoggingLevel.DEBUG
|
|
|
|
|
file_cache_len: int = 20
|
|
|
|
|
file_cache_time: Union[int, float] = 1
|
|
|
|
|
|
|
|
|
|
|
2022-12-29 10:13:26 +08:00
|
|
|
|
class Message_content(NamedTuple):
|
2022-11-11 09:33:34 +08:00
|
|
|
|
"""用于存储 log 信息的不可变元组"""
|
|
|
|
|
log_time: float
|
|
|
|
|
text: str
|
|
|
|
|
level: int
|
2023-01-25 20:13:36 +08:00
|
|
|
|
logger_name: Optional[str] = 'root'
|
2022-11-11 09:33:34 +08:00
|
|
|
|
marker: Optional[str] = None
|
|
|
|
|
end: Optional[str] = '\n'
|
|
|
|
|
flush: Optional[bool] = False
|
|
|
|
|
frame: Optional[FrameType] = None
|
2022-10-29 19:13:00 +08:00
|
|
|
|
|
2022-11-27 13:45:59 +08:00
|
|
|
|
def __str__(self):
|
2023-01-27 20:32:14 +08:00
|
|
|
|
return (
|
|
|
|
|
f"Message Content at {self.log_time}|by logger {self.logger_name}|in level {self.level}"
|
|
|
|
|
f"|with marker {self.marker}|ends as {self.end}|"
|
|
|
|
|
f"by frame {self.frame}|{'and will flush' if self.flush else 'and will not flush'}|"
|
|
|
|
|
f"text are: {self.text}"
|
|
|
|
|
)
|
2022-11-27 13:45:59 +08:00
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
|
2022-09-04 13:02:58 +08:00
|
|
|
|
class ThreadLock:
|
2022-09-06 21:47:46 +08:00
|
|
|
|
"""一个用来 with 的线程锁"""
|
2022-09-04 13:02:58 +08:00
|
|
|
|
|
|
|
|
|
def __init__(self, the_lock: threading.Lock, time_out: Union[float, int] = 1 / 60) -> None:
|
2022-10-02 22:24:38 +08:00
|
|
|
|
"""
|
|
|
|
|
:param the_lock: 用于 with 的线程锁
|
|
|
|
|
:param time_out: with 的超时时间
|
|
|
|
|
"""
|
2022-09-04 13:02:58 +08:00
|
|
|
|
self.lock = the_lock
|
|
|
|
|
self.time_out = time_out
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
|
self.lock.acquire(timeout=self.time_out)
|
|
|
|
|
if not self.lock.locked():
|
|
|
|
|
raise RuntimeError(f'Lock time Out with {self.time_out}')
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
|
if self.lock.locked():
|
|
|
|
|
self.lock.release()
|
|
|
|
|
|
|
|
|
|
|
2022-08-12 21:07:36 +08:00
|
|
|
|
class ListCache:
|
|
|
|
|
"""一个线程安全的列表缓存"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, lock: ThreadLock):
|
|
|
|
|
self._cache = []
|
|
|
|
|
self.with_thread_lock = lock
|
|
|
|
|
|
2022-09-06 21:47:46 +08:00
|
|
|
|
def append(self, value: Union[str, Iterable[str]]):
|
2022-09-04 01:26:20 +08:00
|
|
|
|
with self.with_thread_lock:
|
|
|
|
|
if isinstance(value, str):
|
2022-08-12 21:07:36 +08:00
|
|
|
|
self._cache.append(value)
|
2022-09-04 01:26:20 +08:00
|
|
|
|
elif isinstance(value, Iterable):
|
2022-08-12 21:07:36 +08:00
|
|
|
|
self._cache.append(*value)
|
2022-09-04 01:26:20 +08:00
|
|
|
|
else:
|
2022-11-01 23:22:39 +08:00
|
|
|
|
raise TypeError(
|
|
|
|
|
f"cache must be string or Iterable. not a {type(value)}")
|
2022-08-12 21:07:36 +08:00
|
|
|
|
|
2022-09-06 21:47:46 +08:00
|
|
|
|
def __getitem__(self, item) -> str:
|
2022-08-12 21:07:36 +08:00
|
|
|
|
assert isinstance(item, int)
|
|
|
|
|
with self.with_thread_lock:
|
|
|
|
|
try:
|
|
|
|
|
return self._cache[item]
|
|
|
|
|
except IndexError as exp:
|
|
|
|
|
print(f'cache:{self.cache}')
|
2022-11-01 23:22:39 +08:00
|
|
|
|
raise IndexError(
|
2023-01-27 20:32:14 +08:00
|
|
|
|
f'there is no cache at {item}!\n'
|
|
|
|
|
f'cache:{self.cache}\n'
|
|
|
|
|
f'{exp}'
|
|
|
|
|
) from exp
|
2022-08-12 21:07:36 +08:00
|
|
|
|
|
2022-09-06 21:47:46 +08:00
|
|
|
|
def __call__(self, *args, **kwargs) -> List[str]:
|
2022-08-12 21:07:36 +08:00
|
|
|
|
return self.cache
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
2022-09-04 01:26:20 +08:00
|
|
|
|
self._iter_len = len(self.cache)
|
2022-08-12 21:07:36 +08:00
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def __next__(self):
|
2022-09-04 01:26:20 +08:00
|
|
|
|
if self._iter_len == -1:
|
|
|
|
|
del self._iter_len
|
2022-09-03 22:35:57 +08:00
|
|
|
|
raise StopIteration('there is no more cache')
|
2022-09-04 01:26:20 +08:00
|
|
|
|
returns = self.cache[-self._iter_len]
|
|
|
|
|
self._iter_len -= 1
|
2022-08-12 21:07:36 +08:00
|
|
|
|
return returns
|
|
|
|
|
|
|
|
|
|
def __bool__(self):
|
2023-01-27 20:32:14 +08:00
|
|
|
|
return len(self.cache) > 0
|
2022-08-12 21:07:36 +08:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def cache(self):
|
|
|
|
|
return self._cache
|
|
|
|
|
|
2022-09-04 01:26:20 +08:00
|
|
|
|
def clear(self):
|
|
|
|
|
with self.with_thread_lock:
|
|
|
|
|
self.cache.clear()
|
|
|
|
|
|
2022-07-04 15:12:04 +08:00
|
|
|
|
|
2022-12-25 23:15:57 +08:00
|
|
|
|
class ColorCodeEnum(enum.Enum):
|
|
|
|
|
main_time = "main_time"
|
|
|
|
|
code_line = "code_line"
|
|
|
|
|
file_name = "file_name"
|
|
|
|
|
info = "info"
|
|
|
|
|
message = "message"
|
|
|
|
|
logger = "logger"
|
|
|
|
|
marker = "marker"
|
|
|
|
|
|
|
|
|
|
|
2022-12-29 10:13:26 +08:00
|
|
|
|
class FormatCodeEnum(enum.Enum):
|
|
|
|
|
long_time: str = '%Y-%m-%d %H-%M-%S:%%S'
|
|
|
|
|
short_time: str = '%Y-%m-%d %H-%M-%S'
|
|
|
|
|
logger_name: str = '{logger_name}'
|
|
|
|
|
level: str = '{level}'
|
|
|
|
|
fine_name: str = '{file_name}'
|
|
|
|
|
code_line: str = '{code_line}'
|
|
|
|
|
marker: str = '{marker}'
|
|
|
|
|
message: str = '{message}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FormatterConfig(NamedTuple):
|
|
|
|
|
""" Named Tuple 真好用 """
|
|
|
|
|
support_color: bool
|
|
|
|
|
format: str
|
|
|
|
|
formats: Dict[str, str]
|
|
|
|
|
color: Dict[str, Union[str, Dict[str, str]]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FormatterTemplate:
|
|
|
|
|
"""用于格式化 log 信息的模板类"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, configs: FormatterConfig):
|
|
|
|
|
self.configs = configs
|
|
|
|
|
|
|
|
|
|
def format(self, message: Message_content) -> str:
|
|
|
|
|
raise NotImplementedError('There is a formatter that not implemented')
|
|
|
|
|
|
|
|
|
|
|
2022-11-11 09:33:34 +08:00
|
|
|
|
class StdFormatter(FormatterTemplate):
|
|
|
|
|
""" 一个标准的格式化类 """
|
|
|
|
|
|
2022-12-29 10:13:26 +08:00
|
|
|
|
def __init__(self, configs: FormatterConfig):
|
|
|
|
|
super().__init__(configs=configs)
|
2022-11-11 09:33:34 +08:00
|
|
|
|
|
2022-11-27 13:45:59 +08:00
|
|
|
|
def format_time(self, input_time: Optional[float] = None) -> Dict[str, str]:
|
|
|
|
|
millisecond = str((input_time - int(input_time)) * 1000)
|
2022-12-29 10:13:26 +08:00
|
|
|
|
long_time = time.strftime(self.configs.formats['long_time'].replace('%%S', millisecond))
|
|
|
|
|
short_time = time.strftime(self.configs.formats['short_time'].replace('%%S', millisecond))
|
2023-01-25 20:13:36 +08:00
|
|
|
|
return {'{long_time}': long_time, '{short_time}': short_time}
|
2022-11-27 13:45:59 +08:00
|
|
|
|
|
2022-12-25 23:15:57 +08:00
|
|
|
|
def get_color_code(self, level: str, content: ColorCodeEnum) -> str:
|
|
|
|
|
assert content in ColorCodeEnum
|
2022-12-29 10:13:26 +08:00
|
|
|
|
if content in self.configs.color[level]:
|
2023-01-27 20:32:14 +08:00
|
|
|
|
return self.configs.color[level][content.name].replace(r"\u001b", '\u001b')
|
|
|
|
|
return self.configs.color[content.name].replace(r'\u001b', '\u001b')
|
2022-12-29 10:13:26 +08:00
|
|
|
|
|
|
|
|
|
"""
|
2023-01-25 20:13:36 +08:00
|
|
|
|
format 支持的内容:
|
2022-12-29 10:13:26 +08:00
|
|
|
|
{long_time}: 长时间
|
|
|
|
|
{short_time}: 短时间
|
|
|
|
|
{logger_name}: logger 名称
|
|
|
|
|
{level}: 记录等级
|
|
|
|
|
{file_name}: 文件名
|
|
|
|
|
{code_line}: 代码行
|
|
|
|
|
{marker}: 标记
|
|
|
|
|
{message}: 消息
|
|
|
|
|
"""
|
2022-12-25 23:15:57 +08:00
|
|
|
|
|
2022-12-29 10:13:26 +08:00
|
|
|
|
def color_format(self, message: Message_content) -> str:
|
|
|
|
|
if not self.configs.support_color:
|
|
|
|
|
return self.format(message=message)
|
2022-12-25 23:15:57 +08:00
|
|
|
|
times = self.format_time(input_time=message.log_time)
|
2023-01-27 20:32:14 +08:00
|
|
|
|
need_colors = list(re_find_formats.findall(self.configs.format))
|
2023-01-25 20:13:36 +08:00
|
|
|
|
new_config = self.configs.format
|
2022-12-29 10:13:26 +08:00
|
|
|
|
for need_color in need_colors:
|
|
|
|
|
if not hasattr(FormatCodeEnum, need_color[1:-1]):
|
|
|
|
|
warnings.warn(f'logger config wrong! get {need_color}')
|
|
|
|
|
continue
|
|
|
|
|
color_code = color_reset_suffix
|
|
|
|
|
if need_color[1:-1] in self.configs.color[level_name_map[message.level]]:
|
|
|
|
|
color_code = self.configs.color[level_name_map[message.level]][need_color[1:-1]]
|
|
|
|
|
elif need_color[1:-1] in self.configs.color:
|
|
|
|
|
color_code = self.configs.color[need_color[1:-1]]
|
2023-01-25 20:13:36 +08:00
|
|
|
|
new_config.replace(need_color, f'{color_code}{need_color}{color_reset_suffix}')
|
2022-12-29 10:13:26 +08:00
|
|
|
|
|
|
|
|
|
def format(self, message: Message_content) -> str:
|
2023-01-25 20:13:36 +08:00
|
|
|
|
"""
|
|
|
|
|
将传入得 message 中的信息格式化之后输出为单一字符串
|
|
|
|
|
:param message:
|
|
|
|
|
:return: str
|
|
|
|
|
"""
|
2022-11-27 13:45:59 +08:00
|
|
|
|
times = self.format_time(input_time=message.log_time)
|
2023-01-25 20:13:36 +08:00
|
|
|
|
level = level_name_map[message.level]
|
|
|
|
|
formatted_str = self.configs.format
|
|
|
|
|
for time_text, value in times.items():
|
|
|
|
|
formatted_str.replace(time_text, value)
|
2023-01-27 20:32:14 +08:00
|
|
|
|
|
|
|
|
|
formatted_str.replace("{marker}", message.marker) \
|
|
|
|
|
.replace("{message}", message.text) \
|
|
|
|
|
.replace("{level}", level)
|
2023-01-25 20:13:36 +08:00
|
|
|
|
file_name = '*'
|
|
|
|
|
code_line = '*'
|
|
|
|
|
if message.frame is not None:
|
|
|
|
|
file_name = message.frame.f_code.co_filename
|
|
|
|
|
code_line = message.frame.f_lineno
|
2023-01-27 20:32:14 +08:00
|
|
|
|
formatted_str.replace("{file_name}", file_name) \
|
|
|
|
|
.replace("{code_line}", code_line)
|
|
|
|
|
# 注意:类似这种“链式调用”不应超过 5 行,否则将降低可读性
|
|
|
|
|
# 关键词 MUST、MUST NOT、REQUIRED、SHALL、SHALL NOT、SHOULD、SHOULD NOT、 RECOMMENDED、MAY、OPTIONAL
|
|
|
|
|
# 依照 RFC 2119 的叙述解读
|
2023-01-25 20:13:36 +08:00
|
|
|
|
return formatted_str
|
2022-10-11 21:53:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
logger a
|
|
|
|
|
.enable = True
|
|
|
|
|
.level = 0
|
|
|
|
|
handler b -> shell
|
|
|
|
|
.enable = True
|
|
|
|
|
.level = 30
|
|
|
|
|
handler c -> file
|
|
|
|
|
.enable = True
|
|
|
|
|
.level = 0
|
2023-01-27 20:32:14 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
a.info('abc')
|
|
|
|
|
|
|
|
|
|
b -> none
|
|
|
|
|
c -> [info]'abc'
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StreamHandlerTemplate:
|
|
|
|
|
""" 一个一个一个 stream template 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 """
|
|
|
|
|
name = "handler temple"
|
2022-10-02 22:24:38 +08:00
|
|
|
|
|
2022-11-11 09:33:34 +08:00
|
|
|
|
def __init__(self, level: int, formatter: Optional[FormatterTemplate] = None):
|
2022-10-11 21:53:55 +08:00
|
|
|
|
"""
|
|
|
|
|
:param level: 处理器输出等级
|
|
|
|
|
:param formatter: 格式化处理器
|
|
|
|
|
"""
|
|
|
|
|
self.enable = True
|
|
|
|
|
self.level = level
|
2022-11-11 09:33:34 +08:00
|
|
|
|
self.formatter = formatter
|
2022-10-11 21:53:55 +08:00
|
|
|
|
|
2022-11-11 09:33:34 +08:00
|
|
|
|
def write(self, message: Message_content) -> bool:
|
2022-10-11 21:53:55 +08:00
|
|
|
|
"""
|
|
|
|
|
向 输出 文件/stdio 写入信息
|
2022-11-11 09:33:34 +08:00
|
|
|
|
:param message: 要写入的信息包
|
2022-10-11 21:53:55 +08:00
|
|
|
|
:return: 是否写入成功
|
|
|
|
|
"""
|
|
|
|
|
raise NotImplementedError("You Need to Implement the 'write' method")
|
|
|
|
|
|
|
|
|
|
def flush(self) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
刷新缓冲区
|
|
|
|
|
:return: 是否刷新成功
|
|
|
|
|
"""
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def close(self) -> bool:
|
2023-01-25 20:13:36 +08:00
|
|
|
|
""":return: stream 是否关闭成功"""
|
2022-10-11 21:53:55 +08:00
|
|
|
|
raise NotImplementedError("You Need to Implement the 'close' method")
|
|
|
|
|
|
2022-11-11 09:33:34 +08:00
|
|
|
|
def setFormatter(self, fmt: FormatterTemplate) -> None:
|
2022-10-11 21:53:55 +08:00
|
|
|
|
"""
|
|
|
|
|
用于与 logging 兼容
|
|
|
|
|
:param fmt: 要设置的格式化处理器
|
|
|
|
|
:return: None
|
|
|
|
|
"""
|
|
|
|
|
self.formatter = fmt
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
"""
|
|
|
|
|
:return: 自己的名字 等级
|
|
|
|
|
"""
|
|
|
|
|
return f"{self.name}{{level={self.level}}}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StdHandler(StreamHandlerTemplate):
|
2022-10-14 13:49:09 +08:00
|
|
|
|
""" 向标准输入输出流输出信息 """
|
2022-10-11 21:53:55 +08:00
|
|
|
|
name = "std handler"
|
|
|
|
|
|
2022-11-11 09:33:34 +08:00
|
|
|
|
def __init__(self, level: int, formatter: Optional[FormatterTemplate] = None):
|
2022-10-11 21:53:55 +08:00
|
|
|
|
"""
|
2022-10-14 13:49:09 +08:00
|
|
|
|
:param level: 级别
|
|
|
|
|
:param formatter: 格式器
|
2022-10-11 21:53:55 +08:00
|
|
|
|
"""
|
|
|
|
|
super().__init__(level=level, formatter=formatter)
|
|
|
|
|
|
2022-11-11 09:33:34 +08:00
|
|
|
|
def write(self, message: Message_content) -> bool:
|
2022-12-29 10:13:26 +08:00
|
|
|
|
print(self.formatter.format(message), end=message.end, flush=message.flush)
|
2022-10-11 21:53:55 +08:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def close(self) -> bool:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def flush(self) -> bool:
|
|
|
|
|
print('', end='', flush=True)
|
|
|
|
|
return True
|
|
|
|
|
|
2022-11-26 21:48:55 +08:00
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f'StdHandler(level={self.level}, formatter={self.formatter})'
|
|
|
|
|
|
|
|
|
|
|
2022-10-14 13:49:09 +08:00
|
|
|
|
class CachedFileHandler(StreamHandlerTemplate):
|
|
|
|
|
""" 缓存文件的处理器 """
|
|
|
|
|
name = 'cached file handler'
|
|
|
|
|
|
2022-12-25 23:15:57 +08:00
|
|
|
|
def __init__(self, level: int, formatter: Optional[FormatterTemplate] = None,
|
|
|
|
|
file_conf: Union[dict, LogFileConf, None] = None):
|
2022-10-14 13:49:09 +08:00
|
|
|
|
"""
|
|
|
|
|
:param level:
|
|
|
|
|
:param formatter:
|
2022-10-29 19:13:00 +08:00
|
|
|
|
:param file_conf: 文件配置
|
2022-10-14 13:49:09 +08:00
|
|
|
|
"""
|
|
|
|
|
super().__init__(level=level, formatter=formatter)
|
2023-01-27 20:32:14 +08:00
|
|
|
|
if file_conf is None:
|
2022-11-03 20:37:30 +08:00
|
|
|
|
self.file_conf = LogFileConf()
|
2023-01-27 20:32:14 +08:00
|
|
|
|
elif isinstance(file_conf, dict):
|
|
|
|
|
self.file_conf = LogFileConf(**file_conf)
|
|
|
|
|
elif isinstance(file_conf, LogFileConf):
|
|
|
|
|
self.file_conf = file_conf
|
2022-11-03 20:37:30 +08:00
|
|
|
|
# 缓存
|
2022-11-26 21:48:55 +08:00
|
|
|
|
self.len = 0
|
|
|
|
|
self.cache_stream = io.StringIO()
|
2022-11-03 20:37:30 +08:00
|
|
|
|
# 状态
|
|
|
|
|
self.started = True
|
2022-11-11 09:33:34 +08:00
|
|
|
|
self.thread_started = False
|
2022-11-26 21:48:55 +08:00
|
|
|
|
self.flushing = False
|
2022-11-03 20:37:30 +08:00
|
|
|
|
# 同步锁
|
2022-11-26 21:48:55 +08:00
|
|
|
|
self.threaded_write = threading.Timer(1, self.flush, kwargs={'by_thread': True}) # 基于 timer 的多线程
|
2022-11-03 20:37:30 +08:00
|
|
|
|
|
2022-11-11 09:33:34 +08:00
|
|
|
|
def _start_thread(self) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
如果成功启动 返回 True
|
|
|
|
|
已经启动则返回 False
|
|
|
|
|
:return: 是否启动
|
|
|
|
|
"""
|
|
|
|
|
if self.thread_started:
|
|
|
|
|
return False
|
2022-11-26 21:48:55 +08:00
|
|
|
|
self.threaded_write = threading.Timer(1, self.flush, kwargs={'by_thread': True})
|
2022-11-11 09:33:34 +08:00
|
|
|
|
self.threaded_write.start()
|
|
|
|
|
self.thread_started = True
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _stop_thread(self) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
成功关闭 返回 True
|
|
|
|
|
未开启/关闭失败 返回 False
|
|
|
|
|
:return: 是否成功关闭
|
|
|
|
|
"""
|
|
|
|
|
if not self.threaded_write.is_alive():
|
|
|
|
|
self.thread_started = False
|
|
|
|
|
return False
|
|
|
|
|
self.threaded_write.cancel()
|
|
|
|
|
|
|
|
|
|
def write(self, message: Message_content) -> bool:
|
2022-11-26 21:48:55 +08:00
|
|
|
|
self.len += 1
|
2023-01-27 20:32:14 +08:00
|
|
|
|
formatted_message = f'{self.formatter.format(message)}{message.end}'
|
2022-12-29 10:13:26 +08:00
|
|
|
|
self.cache_stream.write(formatted_message)
|
2022-11-26 21:48:55 +08:00
|
|
|
|
if message.flush or self.len >= self.file_conf.file_cache_len:
|
|
|
|
|
if not self.flush():
|
|
|
|
|
self.flush()
|
|
|
|
|
elif not self.thread_started:
|
|
|
|
|
self._start_thread()
|
2022-11-03 20:37:30 +08:00
|
|
|
|
return True
|
2022-10-14 13:49:09 +08:00
|
|
|
|
|
|
|
|
|
def close(self) -> bool:
|
2022-11-26 21:48:55 +08:00
|
|
|
|
self.cache_stream.close()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def flush(self, by_thread: Optional[bool] = False) -> bool:
|
|
|
|
|
if by_thread:
|
|
|
|
|
self.threaded_write = threading.Timer(1, self.flush, kwargs={'by_thread': True})
|
|
|
|
|
self.threaded_write.start()
|
|
|
|
|
if self.flushing:
|
|
|
|
|
return False
|
|
|
|
|
self.flushing = True
|
|
|
|
|
if cache := self.cache_stream.getvalue():
|
|
|
|
|
self.flushing = True
|
|
|
|
|
self.cache_stream.close()
|
|
|
|
|
self.cache_stream = io.StringIO()
|
|
|
|
|
self.len = 0
|
|
|
|
|
with open(file=self.file_conf.file_name, mode=self.file_conf.file_mode,
|
|
|
|
|
encoding=self.file_conf.file_encoding) as log_file:
|
|
|
|
|
log_file.write(cache)
|
|
|
|
|
self.flushing = False
|
|
|
|
|
return True
|
2022-10-14 13:49:09 +08:00
|
|
|
|
|
|
|
|
|
|
2022-06-27 14:42:26 +08:00
|
|
|
|
class LogFileCache:
|
|
|
|
|
"""日志文件缓存"""
|
|
|
|
|
|
2022-09-04 17:20:36 +08:00
|
|
|
|
def __init__(self, file_conf: dict):
|
2022-07-07 18:28:29 +08:00
|
|
|
|
"""
|
2022-10-02 22:24:38 +08:00
|
|
|
|
:param file_conf: 日志文件配置
|
2022-07-07 18:28:29 +08:00
|
|
|
|
"""
|
2022-06-27 14:42:26 +08:00
|
|
|
|
# 配置相关
|
2022-11-03 20:37:30 +08:00
|
|
|
|
self._logfile_name = os.path.abspath(format_str(file_conf['file_name'])) # log 文件名称
|
2023-01-25 20:13:36 +08:00
|
|
|
|
self.level: logging_level_type = file_conf.get('level', DEBUG)
|
2022-09-04 01:26:20 +08:00
|
|
|
|
self.file_conf = file_conf
|
2022-09-04 13:02:58 +08:00
|
|
|
|
self.flush_time = file_conf['cache_time'] # 缓存刷新时长
|
|
|
|
|
self.cache_entries_num = file_conf['cache_len']
|
|
|
|
|
self.started = True
|
|
|
|
|
self.running = False
|
2022-06-27 16:22:40 +08:00
|
|
|
|
# 同步锁
|
2022-07-16 20:20:23 +08:00
|
|
|
|
self.cache_lock = threading.Lock() # 主锁
|
2022-11-03 20:37:30 +08:00
|
|
|
|
self.time_limit_lock = ThreadLock(self.cache_lock, time_out=1 / 60) # 直接用于 with 的主锁
|
|
|
|
|
self.threaded_write = threading.Timer(1, self._log_file_time_write, kwargs={'thread': True}) # 基于 timer 的多线程
|
2022-08-12 21:07:36 +08:00
|
|
|
|
# 日志缓存表
|
2022-09-03 22:35:57 +08:00
|
|
|
|
self.log_cache = ListCache(self.time_limit_lock)
|
2022-09-04 01:26:20 +08:00
|
|
|
|
self.file_setup()
|
|
|
|
|
|
|
|
|
|
def file_setup(self):
|
|
|
|
|
cache_time = 0
|
|
|
|
|
file_type = self.logfile_name[self.logfile_name.rfind('.'):]
|
|
|
|
|
file_pure_name = self.logfile_name[:self.logfile_name.rfind('.')]
|
|
|
|
|
while os.path.isfile(self.logfile_name):
|
|
|
|
|
cache_time += 1
|
|
|
|
|
self.logfile_name = f'{file_pure_name}-{cache_time}{file_type}'
|
2022-07-04 15:12:04 +08:00
|
|
|
|
|
2022-07-16 20:20:23 +08:00
|
|
|
|
def end_thread(self) -> None:
|
2022-07-07 18:28:29 +08:00
|
|
|
|
"""结束日志写入进程,顺手把目前的缓存写入"""
|
2022-07-16 20:20:23 +08:00
|
|
|
|
self.cache_lock.acquire(blocking=True)
|
2022-09-04 13:02:58 +08:00
|
|
|
|
if self.running:
|
|
|
|
|
self.threaded_write.cancel()
|
|
|
|
|
self.running = False
|
2022-08-12 21:07:36 +08:00
|
|
|
|
self.started = False
|
2022-07-16 20:20:23 +08:00
|
|
|
|
self._log_file_time_write()
|
2022-08-12 21:07:36 +08:00
|
|
|
|
atexit.unregister(self.end_thread)
|
2022-07-04 15:12:04 +08:00
|
|
|
|
|
2022-07-16 20:20:23 +08:00
|
|
|
|
def start_thread(self) -> None:
|
2022-07-04 15:12:04 +08:00
|
|
|
|
self.threaded_write.start()
|
2022-08-12 21:07:36 +08:00
|
|
|
|
self.started = True
|
2022-09-04 13:02:58 +08:00
|
|
|
|
self.running = True
|
2022-07-04 15:12:04 +08:00
|
|
|
|
atexit.register(self.end_thread)
|
2022-06-27 16:51:14 +08:00
|
|
|
|
|
|
|
|
|
@property
|
2022-09-04 13:02:58 +08:00
|
|
|
|
def logfile_name(self) -> str:
|
|
|
|
|
self._logfile_name: str
|
2022-06-27 16:51:14 +08:00
|
|
|
|
return self._logfile_name
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-06-27 16:51:14 +08:00
|
|
|
|
@logfile_name.setter
|
2022-09-04 13:02:58 +08:00
|
|
|
|
def logfile_name(self, value: str) -> None:
|
2022-09-03 22:35:57 +08:00
|
|
|
|
with self.time_limit_lock:
|
2022-07-04 15:12:04 +08:00
|
|
|
|
self._logfile_name = value
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-09-04 13:02:58 +08:00
|
|
|
|
def _log_file_time_write(self, thread: bool = False) -> None:
|
2022-06-27 14:42:26 +08:00
|
|
|
|
"""使用 threading.Timer 调用的定时写入日志文件的函数"""
|
2022-08-12 21:07:36 +08:00
|
|
|
|
if self.log_cache:
|
2022-09-03 22:35:57 +08:00
|
|
|
|
with self.time_limit_lock:
|
2022-08-12 21:07:36 +08:00
|
|
|
|
if self.log_cache:
|
2022-09-04 01:26:20 +08:00
|
|
|
|
with open(file=self.logfile_name,
|
2023-01-25 20:13:36 +08:00
|
|
|
|
encoding=self.file_conf.get('encoding', 'utf-8'),
|
|
|
|
|
mode=self.file_conf.get('mode', 'a')) as log_file:
|
2022-09-04 01:26:20 +08:00
|
|
|
|
log_file.writelines(self.log_cache.cache.copy())
|
|
|
|
|
self.log_cache.clear()
|
2022-09-04 13:02:58 +08:00
|
|
|
|
if thread:
|
|
|
|
|
self.running = False
|
2022-09-04 01:26:20 +08:00
|
|
|
|
|
|
|
|
|
def write_logs(self, string: str, flush: bool = False) -> None:
|
|
|
|
|
self.log_cache.append(string)
|
|
|
|
|
if len(self.log_cache.cache) >= 10:
|
|
|
|
|
self._log_file_time_write()
|
|
|
|
|
return None
|
|
|
|
|
if flush:
|
|
|
|
|
self._log_file_time_write()
|
2022-09-04 13:02:58 +08:00
|
|
|
|
if self.started and not self.running:
|
2022-11-01 23:22:39 +08:00
|
|
|
|
self.threaded_write = threading.Timer(1, self._log_file_time_write, kwargs={
|
|
|
|
|
'thread': True}) # 基于 timer 的多线程
|
2022-09-04 13:02:58 +08:00
|
|
|
|
self.threaded_write.start()
|
|
|
|
|
self.running = True
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Logger:
|
2022-10-29 19:13:00 +08:00
|
|
|
|
"""shenjackのlogger"""
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-09-04 13:02:58 +08:00
|
|
|
|
def __init__(self,
|
2022-09-06 21:47:46 +08:00
|
|
|
|
name: str = 'root',
|
|
|
|
|
level: int = DEBUG,
|
2022-11-01 18:34:02 +08:00
|
|
|
|
file_conf: Optional[List[LogFileCache]] = None,
|
2022-11-01 23:22:39 +08:00
|
|
|
|
colors: Optional[Dict[Union[int, str],
|
2022-12-25 23:15:57 +08:00
|
|
|
|
Dict[str, str]]] = None,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
formats=None
|
|
|
|
|
) -> None:
|
2022-07-16 20:20:23 +08:00
|
|
|
|
"""
|
|
|
|
|
配置模式: 使用 kwargs 配置
|
2022-10-02 22:24:38 +08:00
|
|
|
|
:param name: logger 名称 默认为 root
|
|
|
|
|
:param level: logging 输出等级 默认为 DEBUG(10)
|
|
|
|
|
:param file_conf: logger 的文件处理配置
|
|
|
|
|
:param colors: dict 颜色配置
|
|
|
|
|
:param formats: 格式化配置
|
2022-07-16 20:20:23 +08:00
|
|
|
|
"""
|
2022-10-11 21:53:55 +08:00
|
|
|
|
self.enable = True
|
2022-09-06 21:47:46 +08:00
|
|
|
|
self.name = name
|
|
|
|
|
self.level = level
|
2022-11-27 13:45:59 +08:00
|
|
|
|
self.min_level = self.level
|
2022-09-03 22:35:57 +08:00
|
|
|
|
self.colors = colors or logger_configs['Color']['main_color']
|
2022-09-04 13:02:58 +08:00
|
|
|
|
self.formats = formats or logger_configs['Formatter'].copy()
|
2023-01-25 20:13:36 +08:00
|
|
|
|
self.streams: List[StreamHandlerTemplate] = []
|
|
|
|
|
self.handler: List[StdHandler] = []
|
2022-09-04 01:26:20 +08:00
|
|
|
|
if file_conf:
|
2022-09-04 17:20:36 +08:00
|
|
|
|
self.file_cache = file_conf
|
2023-01-25 20:13:36 +08:00
|
|
|
|
self.min_level = min(min(file.level for file in file_conf), self.level)
|
2022-06-27 14:42:26 +08:00
|
|
|
|
else:
|
2022-09-04 17:20:36 +08:00
|
|
|
|
self.file_cache = []
|
2022-07-16 20:20:23 +08:00
|
|
|
|
self.warn = self.warning
|
2022-11-01 23:22:39 +08:00
|
|
|
|
|
|
|
|
|
def format_formats(self) -> None:
|
|
|
|
|
if 'long_time' not in self.formats:
|
|
|
|
|
self.formats['long_time'] = '%Y-%m-%d %H-%M-%S:%%S'
|
|
|
|
|
if 'short_time' not in self.formats:
|
|
|
|
|
self.formats['short_time'] = '%Y-%m-%d %H-%M-%S'
|
2022-07-16 20:20:23 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
def add_stream(self, stream: StreamHandlerTemplate) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
向 logger 添加一个输出方法 (stream handler)
|
|
|
|
|
:param stream: 将要添加的 stream handler
|
|
|
|
|
:return: 是否添加成功 如果 stream 已经存在则返回 False
|
|
|
|
|
"""
|
|
|
|
|
if stream in self.streams:
|
|
|
|
|
return False
|
|
|
|
|
self.streams.append(stream)
|
|
|
|
|
return True
|
|
|
|
|
|
2022-11-01 18:34:02 +08:00
|
|
|
|
def enabled_for(self, level: logging_level_type) -> bool:
|
2023-01-27 20:32:14 +08:00
|
|
|
|
return level >= self.min_level if self.enable else False
|
2022-11-01 23:22:39 +08:00
|
|
|
|
|
2022-11-27 13:45:59 +08:00
|
|
|
|
# def format_time(self, input_time: Optional[float] = None) -> Dict[str, str]:
|
|
|
|
|
# # 毫秒
|
|
|
|
|
# get_time: float = input_time or time.time()
|
|
|
|
|
# millisecond = str((get_time - int(get_time)) * 1000)
|
|
|
|
|
# long_time = time.strftime(
|
|
|
|
|
# self.formats['long_time'].replace('%%S', millisecond))
|
|
|
|
|
# short_time = time.strftime(
|
|
|
|
|
# self.formats['short_time'].replace('%%S', millisecond))
|
|
|
|
|
# return {'long_time': long_time, 'short_time': short_time}
|
2022-09-04 17:20:36 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
# def filter_and_make_log(): make_log(formatted_string)
|
2022-07-16 20:20:23 +08:00
|
|
|
|
def make_log(self, *values: object,
|
|
|
|
|
level: int,
|
2022-10-11 21:53:55 +08:00
|
|
|
|
marker: Optional[str] = None,
|
2022-07-04 15:12:04 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-03 22:35:57 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-09-06 21:47:46 +08:00
|
|
|
|
if frame is None:
|
|
|
|
|
if (frame := inspect.currentframe()) is not None:
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame = (
|
|
|
|
|
frame
|
|
|
|
|
if frame.f_back is None
|
|
|
|
|
else (
|
|
|
|
|
frame.f_back
|
|
|
|
|
if frame.f_back.f_back is None
|
|
|
|
|
else frame.f_back.f_back
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
message = Message_content(log_time=time.time(),
|
|
|
|
|
text=sep.join(i if type(i) is str else str(i) for i in values),
|
2023-01-25 20:13:36 +08:00
|
|
|
|
level=level, logger_name=self.name, marker=marker, end=end, flush=flush, frame=frame)
|
2022-11-27 13:45:59 +08:00
|
|
|
|
|
|
|
|
|
# 调用 steams
|
|
|
|
|
for stream in self.streams:
|
|
|
|
|
stream.write(message)
|
|
|
|
|
|
2022-11-01 23:22:39 +08:00
|
|
|
|
message_color = self.colors[get_name_by_level(
|
|
|
|
|
level)]['message'] if 'message' in self.colors[get_name_by_level(level)] else self.colors['message']
|
2022-10-29 19:13:00 +08:00
|
|
|
|
text = f"{message_color}{sep.join(i if type(i) is str else str(i) for i in values)}{color_reset_suffix}"
|
2022-10-11 21:53:55 +08:00
|
|
|
|
# print('abc', 'abc', marker='123')
|
2022-09-03 22:35:57 +08:00
|
|
|
|
print_text = self.format_text(level=level, text=text, frame=frame)
|
2022-09-04 01:26:20 +08:00
|
|
|
|
if level >= self.level:
|
|
|
|
|
print(print_text, end=end)
|
2022-09-04 17:20:36 +08:00
|
|
|
|
for file in self.file_cache:
|
|
|
|
|
file: LogFileCache
|
|
|
|
|
if level < file.level:
|
|
|
|
|
continue
|
2023-01-27 20:32:14 +08:00
|
|
|
|
file.write_logs(f"{re.sub(re_find_color_code, '', print_text)}{end}", flush=flush)
|
2022-10-02 22:24:38 +08:00
|
|
|
|
return print_text
|
2022-09-03 22:35:57 +08:00
|
|
|
|
|
|
|
|
|
def format_text(self, level: int, text: str, frame: Optional[FrameType]) -> str:
|
2023-01-27 20:32:14 +08:00
|
|
|
|
level_with_color = (
|
|
|
|
|
f"[{self.colors[get_name_by_level(level)].get('info')}"
|
|
|
|
|
f"{get_name_by_level(level)}{color_reset_suffix}]"
|
|
|
|
|
)
|
2022-09-03 22:35:57 +08:00
|
|
|
|
level_with_color = f"{level_with_color}{' ' * (9 - len_without_color_maker(level_with_color))}"
|
|
|
|
|
formats = self.formats.copy()
|
2022-09-04 13:02:58 +08:00
|
|
|
|
if frame is not None:
|
2023-01-27 20:32:14 +08:00
|
|
|
|
formats['file_name'] = (
|
|
|
|
|
f"{self.colors[get_name_by_level(level)].get('file_name', self.colors['file_name'])}"
|
|
|
|
|
f"{os.path.split(frame.f_code.co_filename)[-1]}{color_reset_suffix}"
|
|
|
|
|
)
|
|
|
|
|
formats['code_line'] = (
|
|
|
|
|
f"{self.colors[get_name_by_level(level)].get('code_line', self.colors['code_line'])}"
|
|
|
|
|
f"{frame.f_lineno}{color_reset_suffix}"
|
|
|
|
|
)
|
|
|
|
|
formats['logger_name'] = (
|
|
|
|
|
f'{self.colors[get_name_by_level(level)].get("logger", self.colors["logger"])}'
|
|
|
|
|
f'{self.name}{color_reset_suffix}'
|
|
|
|
|
)
|
2022-09-03 22:35:57 +08:00
|
|
|
|
now_time = str(time.time())
|
|
|
|
|
for key, value in formats.items():
|
2023-01-27 20:32:14 +08:00
|
|
|
|
if isinstance(value, dict) and 'strftime' in value:
|
|
|
|
|
value['strftime']: str
|
|
|
|
|
formats[key] = (
|
|
|
|
|
f"{self.colors[get_name_by_level(level)].get(key, self.colors[key])}"
|
|
|
|
|
f"{time.strftime(value['strftime'].replace('%%S', now_time[now_time.find('.') + 1:now_time.find('.') + 5]))}"
|
|
|
|
|
f"{color_reset_suffix}"
|
|
|
|
|
)
|
|
|
|
|
return self.formats['MESSAGE']['format'].format(
|
|
|
|
|
level_with_color=level_with_color,
|
|
|
|
|
level=level_with_color,
|
|
|
|
|
message=text,
|
|
|
|
|
**formats
|
|
|
|
|
)
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-08-03 20:22:36 +08:00
|
|
|
|
def trace(self, *values: object,
|
2022-10-11 21:53:55 +08:00
|
|
|
|
marker: Optional[str] = None,
|
2022-08-03 20:22:36 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-06 21:47:46 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return self.make_log(*values, level=LoggingLevel.TRACE, marker=marker, sep=sep, end=end, flush=flush,
|
|
|
|
|
frame=frame)
|
2022-08-03 20:22:36 +08:00
|
|
|
|
|
|
|
|
|
def fine(self, *values: object,
|
2022-10-11 21:53:55 +08:00
|
|
|
|
marker: Optional[str] = None,
|
2022-08-03 20:22:36 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-06 21:47:46 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return self.make_log(*values, level=LoggingLevel.FINE, marker=marker, sep=sep, end=end, flush=flush,
|
|
|
|
|
frame=frame)
|
2022-07-04 15:12:04 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
def debug(self, *values: object,
|
|
|
|
|
marker: Optional[str] = None,
|
2022-07-04 15:12:04 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-06 21:47:46 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return self.make_log(*values, level=LoggingLevel.DEBUG, marker=marker, sep=sep, end=end, flush=flush,
|
|
|
|
|
frame=frame)
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
def info(self, *values: object,
|
|
|
|
|
marker: Optional[str] = None,
|
2022-07-04 15:12:04 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-06 21:47:46 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return self.make_log(*values, level=LoggingLevel.INFO, marker=marker, sep=sep, end=end, flush=flush,
|
|
|
|
|
frame=frame)
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
def warning(self, *values: object,
|
|
|
|
|
marker: Optional[str] = None,
|
2022-07-04 15:12:04 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-06 21:47:46 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return self.make_log(*values, level=LoggingLevel.WARNING, marker=marker, sep=sep, end=end, flush=flush,
|
|
|
|
|
frame=frame)
|
2022-07-04 15:12:04 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
def error(self, *values: object,
|
|
|
|
|
marker: Optional[str] = None,
|
2022-07-04 15:12:04 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-06 21:47:46 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return self.make_log(*values, level=LoggingLevel.ERROR, marker=marker, sep=sep, end=end, flush=flush,
|
|
|
|
|
frame=frame)
|
2022-07-04 15:12:04 +08:00
|
|
|
|
|
2022-10-11 21:53:55 +08:00
|
|
|
|
def fatal(self, *values: object,
|
|
|
|
|
marker: Optional[str] = None,
|
2022-07-04 15:12:04 +08:00
|
|
|
|
sep: Optional[str] = ' ',
|
|
|
|
|
end: Optional[str] = '\n',
|
2022-09-06 21:47:46 +08:00
|
|
|
|
flush: Optional[bool] = False,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
frame: Optional[FrameType] = None
|
|
|
|
|
) -> Optional[str]:
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return self.make_log(*values, level=LoggingLevel.FATAL, marker=marker, sep=sep, end=end, flush=flush,
|
|
|
|
|
frame=frame)
|
2022-06-27 14:42:26 +08:00
|
|
|
|
|
2022-06-29 13:45:25 +08:00
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
_logger_class = Logger
|
2022-10-15 17:38:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RootLogger(Logger):
|
|
|
|
|
""" 默认的 logger """
|
2022-10-29 19:13:00 +08:00
|
|
|
|
|
2022-10-15 17:38:35 +08:00
|
|
|
|
def __init__(self, level: int = LoggingLevel.WARNING, *args, **kwargs):
|
|
|
|
|
super().__init__(level=level, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root_logger = RootLogger()
|
|
|
|
|
|
|
|
|
|
|
2022-10-29 19:13:00 +08:00
|
|
|
|
def basic_config() -> None:
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
2023-01-25 20:13:36 +08:00
|
|
|
|
trace = root_logger.trace
|
|
|
|
|
fine = root_logger.fine
|
|
|
|
|
debug = root_logger.debug
|
|
|
|
|
info = root_logger.info
|
|
|
|
|
warning = root_logger.warning
|
|
|
|
|
error = root_logger.error
|
|
|
|
|
fatal = root_logger.fatal
|
2022-09-04 01:26:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_str(text: str) -> str:
|
|
|
|
|
formats = logger_configs['Formatter'].copy()
|
|
|
|
|
now_time = str(time.time())
|
|
|
|
|
for key, value in formats.items():
|
2023-01-27 20:32:14 +08:00
|
|
|
|
if isinstance(value, dict) and 'strftime' in value:
|
|
|
|
|
value['strftime']: str
|
|
|
|
|
formats[key] = (
|
|
|
|
|
time.strftime(value['strftime'].replace('%%S', now_time[now_time.find('.') + 1:now_time.find('.') + 5]))
|
|
|
|
|
)
|
2022-09-04 01:26:20 +08:00
|
|
|
|
return text.format(**formats)
|
|
|
|
|
|
|
|
|
|
|
2022-09-03 22:35:57 +08:00
|
|
|
|
def len_without_color_maker(text: str) -> int:
|
|
|
|
|
with_out_text = re.sub(re_find_color_code, '', text)
|
|
|
|
|
return len(with_out_text)
|
|
|
|
|
|
|
|
|
|
|
2022-09-04 13:02:58 +08:00
|
|
|
|
def gen_file_conf(file_name: str,
|
|
|
|
|
file_level: int = DEBUG,
|
|
|
|
|
file_mode: str = 'a',
|
|
|
|
|
file_encoding: str = 'utf-8',
|
|
|
|
|
file_cache_len: int = 10,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
file_cache_time: Union[int, float] = 1
|
|
|
|
|
) -> dict:
|
2022-09-06 21:47:46 +08:00
|
|
|
|
"""
|
|
|
|
|
生成一个文件配置
|
2022-10-02 22:24:38 +08:00
|
|
|
|
:param file_name: 日志文件名
|
|
|
|
|
:param file_level: 日志文件记录级别
|
|
|
|
|
:param file_mode: 文件模式
|
|
|
|
|
:param file_encoding: 文件编码
|
|
|
|
|
:param file_cache_len: 文件缓存长度
|
|
|
|
|
:param file_cache_time: 文件缓存时间
|
|
|
|
|
:return: 生成的配置
|
2022-09-06 21:47:46 +08:00
|
|
|
|
"""
|
2022-12-25 23:15:57 +08:00
|
|
|
|
return {'file_name': file_name,
|
|
|
|
|
'level': file_level,
|
|
|
|
|
'mode': file_mode,
|
|
|
|
|
'encoding': file_encoding,
|
|
|
|
|
'cache_len': file_cache_len,
|
2022-09-04 13:02:58 +08:00
|
|
|
|
'cache_time': file_cache_time}
|
|
|
|
|
|
|
|
|
|
|
2022-11-01 18:34:02 +08:00
|
|
|
|
def gen_color_conf(color_name: Optional[str] = None, **colors) -> dict:
|
2023-01-25 20:13:36 +08:00
|
|
|
|
default_color = logger_configs['Color']['main_color' if color_name is None else color_name].copy()
|
2022-09-04 13:02:58 +08:00
|
|
|
|
default_color.update(colors)
|
|
|
|
|
return default_color
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def logger_with_default_settings(name: str,
|
|
|
|
|
level: int = DEBUG,
|
2022-11-01 18:34:02 +08:00
|
|
|
|
file_conf: Optional[dict] = None,
|
|
|
|
|
colors: Optional[dict] = None,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
formats: Optional[dict] = None
|
|
|
|
|
) -> Logger:
|
2022-09-04 13:02:58 +08:00
|
|
|
|
return Logger(name=name,
|
|
|
|
|
level=level,
|
2022-09-04 17:20:36 +08:00
|
|
|
|
file_conf=[LogFileCache(gen_file_conf(**file_conf))],
|
2022-09-04 13:02:58 +08:00
|
|
|
|
colors=gen_color_conf(**colors),
|
|
|
|
|
formats=logger_configs['Formatter'].copy().update(formats))
|
2022-07-07 18:28:29 +08:00
|
|
|
|
|
|
|
|
|
|
2022-09-06 21:47:46 +08:00
|
|
|
|
def add_file_config(conf_name: str,
|
|
|
|
|
file_name: str,
|
|
|
|
|
file_level: int = DEBUG,
|
|
|
|
|
file_mode: str = 'a',
|
|
|
|
|
file_encoding: str = 'utf-8',
|
|
|
|
|
file_cache_len: int = 10,
|
2023-01-27 20:32:14 +08:00
|
|
|
|
file_cache_time: Union[int, float] = 1
|
|
|
|
|
) -> None:
|
2022-09-06 21:47:46 +08:00
|
|
|
|
"""
|
|
|
|
|
向 logger config 里添加一个文件配置
|
2022-10-02 22:24:38 +08:00
|
|
|
|
:param conf_name: 文件配置名称
|
|
|
|
|
:param file_name: 日志文件名
|
|
|
|
|
:param file_level: 日志文件记录级别
|
|
|
|
|
:param file_mode: 文件模式
|
|
|
|
|
:param file_encoding: 文件编码
|
|
|
|
|
:param file_cache_len: 文件缓存长度
|
|
|
|
|
:param file_cache_time: 文件缓存时间
|
|
|
|
|
:return: None
|
2022-09-06 21:47:46 +08:00
|
|
|
|
"""
|
2022-12-25 23:15:57 +08:00
|
|
|
|
logger_configs['File'][conf_name] = {'file_name': file_name,
|
|
|
|
|
'level': file_level,
|
|
|
|
|
'mode': file_mode,
|
|
|
|
|
'encoding': file_encoding,
|
|
|
|
|
'cache_len': file_cache_len,
|
2022-09-06 21:47:46 +08:00
|
|
|
|
'cache_time': file_cache_time}
|
|
|
|
|
|
|
|
|
|
|
2022-09-04 01:26:20 +08:00
|
|
|
|
def get_logger(name: str = 'root') -> Logger:
|
2022-07-16 20:20:23 +08:00
|
|
|
|
"""
|
|
|
|
|
此函数用于从 global_config 中取出对应的配置建立一个相应的 logger
|
2022-10-02 22:24:38 +08:00
|
|
|
|
:param name: logger的名称 默认为 root
|
|
|
|
|
:return: 创建好的 logger
|
2022-07-16 20:20:23 +08:00
|
|
|
|
"""
|
2022-09-04 01:26:20 +08:00
|
|
|
|
if name in logger_configs['Logger']:
|
|
|
|
|
the_config = logger_configs['Logger'][name]
|
|
|
|
|
else:
|
|
|
|
|
the_config = logger_configs['Logger']['root']
|
2022-09-04 17:20:36 +08:00
|
|
|
|
file_handler = None
|
|
|
|
|
if 'file' in the_config:
|
2022-11-01 23:22:39 +08:00
|
|
|
|
file_handler = [LogFileCache(
|
|
|
|
|
logger_configs['File'][the_config['file']])]
|
2022-10-29 19:13:00 +08:00
|
|
|
|
color = the_config['color'] if 'color' in the_config else 'main_color'
|
2022-09-04 13:02:58 +08:00
|
|
|
|
return Logger(name=name,
|
|
|
|
|
level=the_config['level'],
|
2022-09-04 17:20:36 +08:00
|
|
|
|
file_conf=file_handler,
|
2022-10-29 19:13:00 +08:00
|
|
|
|
colors=logger_configs['Color'][color],
|
2022-09-04 13:53:09 +08:00
|
|
|
|
formats=logger_configs['Formatter'].copy())
|
|
|
|
|
|
|
|
|
|
|
2022-09-04 13:02:58 +08:00
|
|
|
|
def test_logger(the_logger: Logger):
|
|
|
|
|
the_logger.trace('tracing')
|
2022-11-27 13:45:59 +08:00
|
|
|
|
the_logger.trace('tracing')
|
|
|
|
|
the_logger.trace('tracing')
|
|
|
|
|
the_logger.fine('some fine!')
|
2022-09-04 13:02:58 +08:00
|
|
|
|
the_logger.fine('some fine!')
|
2022-11-27 13:45:59 +08:00
|
|
|
|
the_logger.fine('some fine!')
|
|
|
|
|
the_logger.debug('debugging')
|
|
|
|
|
the_logger.debug('debugging')
|
2022-09-04 13:02:58 +08:00
|
|
|
|
the_logger.debug('debugging')
|
|
|
|
|
the_logger.info("Hello World!!")
|
2022-11-27 13:45:59 +08:00
|
|
|
|
the_logger.info("Hello World!!")
|
2022-09-04 13:02:58 +08:00
|
|
|
|
the_logger.warn('warning')
|
2022-11-27 13:45:59 +08:00
|
|
|
|
the_logger.warn('warning')
|
|
|
|
|
the_logger.error('error haaaa')
|
2022-09-04 13:02:58 +08:00
|
|
|
|
the_logger.error('error haaaa')
|
|
|
|
|
the_logger.fatal('oh no')
|
2022-07-16 20:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-09-06 21:47:46 +08:00
|
|
|
|
os.chdir('../../')
|
2022-09-04 13:53:09 +08:00
|
|
|
|
logger = get_logger('server')
|
|
|
|
|
|
2022-09-04 13:02:58 +08:00
|
|
|
|
logger.info('my name is:', logger.name)
|
2022-09-04 01:26:20 +08:00
|
|
|
|
a_logger = get_logger('client')
|
2022-09-04 13:53:09 +08:00
|
|
|
|
|
2022-09-04 01:26:20 +08:00
|
|
|
|
a_logger.trace('tracing')
|
|
|
|
|
a_logger.fine('some fine!')
|
|
|
|
|
a_logger.debug('debugging')
|
|
|
|
|
a_logger.info("Hello World!!")
|
|
|
|
|
a_logger.warn('warning')
|
|
|
|
|
a_logger.error('error haaaa')
|
|
|
|
|
a_logger.fatal('oh no')
|
2022-09-04 13:02:58 +08:00
|
|
|
|
logger.info('my name is:', logger.name)
|
2023-10-14 15:36:43 +08:00
|
|
|
|
# for _ in range(5):
|
|
|
|
|
test_logger(logger)
|
|
|
|
|
test_logger(a_logger)
|
2022-12-25 23:15:57 +08:00
|
|
|
|
print(Message_content(log_time=time.time(), text='aaa', level=4, marker='abc', end='abc', flush=False,
|
|
|
|
|
frame=inspect.currentframe()))
|
|
|
|
|
print(ColorCodeEnum.code_line.name)
|