请封装一个复杂的spdlog日志系统,要求功能齐全,在打印到控制台的内容要详细具体到那个文件哪一行,颜色区分,可自由选择打印到控制台还是打印到日志文件,也可以同时打印,程序运行时每隔2秒刷新一次日志文件,保证日志文件的实时性。并且限制文件的大小和数量达到自动轮换效果
时间: 2024-02-18 22:01:25 浏览: 171
CustomRollingFileLogger_Log4报表封装_
好的,以下是一个封装了spdlog的复杂日志系统的示例代码:
```python
import os
import time
from enum import IntEnum
from concurrent.futures import ThreadPoolExecutor
from typing import Optional
import spdlog
class LogLevel(IntEnum):
TRACE = 0
DEBUG = 1
INFO = 2
WARN = 3
ERROR = 4
CRITICAL = 5
class Color(IntEnum):
DEFAULT = 0
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
LIGHT_GRAY = 37
DARK_GRAY = 90
LIGHT_RED = 91
LIGHT_GREEN = 92
LIGHT_YELLOW = 93
LIGHT_BLUE = 94
LIGHT_MAGENTA = 95
LIGHT_CYAN = 96
WHITE = 97
class Log:
def __init__(self, logger_name: str, log_file: Optional[str] = None, console: bool = True, level: LogLevel = LogLevel.DEBUG,
rotation_size: int = 1024 * 1024 * 10, max_files: int = 10):
self.logger_name = logger_name
self.console = console
self.level = level
self.rotation_size = rotation_size
self.max_files = max_files
self.log_file = log_file or f'{self.logger_name}.log'
self.executor = ThreadPoolExecutor(max_workers=1)
self.logger = None
self.init_logger()
def init_logger(self):
self.logger = spdlog.create(self.logger_name, sinks=[], level=self.level)
if self.console:
console_sink = spdlog.stdout_color_sink_mt()
colored_formatter = spdlog.pattern_formatter("%^[%T] [%n] [%l] [%@] %v%$")
console_sink.set_formatter(colored_formatter)
self.logger.sinks().push_back(console_sink)
file_sink = spdlog.rotating_file_sink_mt(self.log_file, self.rotation_size, self.max_files)
file_formatter = spdlog.pattern_formatter("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%@] %v")
file_sink.set_formatter(file_formatter)
self.logger.sinks().push_back(file_sink)
def log(self, level: LogLevel, msg: str, file: Optional[str] = None, line: Optional[int] = None):
if level < self.level:
return
if file and line:
msg = f'{msg} ({os.path.basename(file)}:{line})'
if level == LogLevel.TRACE:
self.logger.trace(msg)
elif level == LogLevel.DEBUG:
self.logger.debug(msg)
elif level == LogLevel.INFO:
self.logger.info(msg)
elif level == LogLevel.WARN:
self.logger.warn(msg)
elif level == LogLevel.ERROR:
self.logger.error(msg)
elif level == LogLevel.CRITICAL:
self.logger.critical(msg)
def trace(self, msg: str, file: Optional[str] = None, line: Optional[int] = None):
self.log(LogLevel.TRACE, msg, file, line)
def debug(self, msg: str, file: Optional[str] = None, line: Optional[int] = None):
self.log(LogLevel.DEBUG, msg, file, line)
def info(self, msg: str, file: Optional[str] = None, line: Optional[int] = None):
self.log(LogLevel.INFO, msg, file, line)
def warn(self, msg: str, file: Optional[str] = None, line: Optional[int] = None):
self.log(LogLevel.WARN, msg, file, line)
def error(self, msg: str, file: Optional[str] = None, line: Optional[int] = None):
self.log(LogLevel.ERROR, msg, file, line)
def critical(self, msg: str, file: Optional[str] = None, line: Optional[int] = None):
self.log(LogLevel.CRITICAL, msg, file, line)
def flush(self):
self.logger.flush()
def async_flush(self):
self.executor.submit(self.flush)
def start_auto_flush(self, interval: int = 2):
while True:
time.sleep(interval)
self.async_flush()
```
使用示例:
```python
log = Log('test', log_file='test.log', console=True, level=LogLevel.DEBUG, rotation_size=1024 * 1024 * 10, max_files=10)
log.trace('Trace message')
log.debug('Debug message')
log.info('Info message')
log.warn('Warn message')
log.error('Error message')
log.critical('Critical message')
# 带文件名和行号
log.info('Info message', __file__, 20)
# 刷新日志
log.flush()
# 异步刷新日志
log.async_flush()
# 开启自动刷新日志
log.start_auto_flush()
```
说明:
1. 日志系统使用了`spdlog`库,支持打印到控制台和日志文件,支持彩色输出。
2. 可以自由选择是否打印到控制台,设置日志级别,设置日志文件大小和数量,实现日志文件的轮换。
3. 日志输出可详细到文件名和行号,并支持异步刷新和自动刷新日志文件。
阅读全文