diff --git a/example/logger/formatter.py b/example/logger/formatter.py index 580ecdd..83b9d96 100644 --- a/example/logger/formatter.py +++ b/example/logger/formatter.py @@ -15,7 +15,7 @@ from lib_not_dr.logger.formatter import (StdFormatter, if __name__ == '__main__': - levels = (0, 2, 5, 7, 10, 30, 50, 90) + levels = (0, 5, 7, 10, 20, 30, 40, 50) log_message = LogMessage(messages=['Hello World!'], level=7, @@ -29,7 +29,7 @@ if __name__ == '__main__': std_format = StdFormatter() std_format.default_template = "${log_time}|${logger_name}|${logger_tag}|${log_source}:${log_line}|${log_function}|${level}|${messages}" - test_levels = (0, 2, 5, 7, 10, 30, 50, 90) + test_levels = (0, 5, 7, 10, 20, 30, 40, 50) print("with color") diff --git a/example/logger/outstream.py b/example/logger/outstream.py index b842114..114dbe5 100644 --- a/example/logger/outstream.py +++ b/example/logger/outstream.py @@ -26,9 +26,11 @@ if __name__ == '__main__': file_cache.write_stdout(log_message) stdio.write_stdout(log_message) # wait for 10 sec - print('wait for 11 sec') - time.sleep(11) - print('finish') + print('wait for 10 sec') + time.sleep(10) + print('look at the ./logs/test.log (sleep 5 sec)') + time.sleep(5) + print('go on') # write 10 lines for i in range(10): diff --git a/lib_not_dr/logger/__init__.py b/lib_not_dr/logger/__init__.py index f1dc16d..a94671b 100644 --- a/lib_not_dr/logger/__init__.py +++ b/lib_not_dr/logger/__init__.py @@ -5,6 +5,8 @@ # ------------------------------- import sys +from lib_not_dr.types.options import Options + COLOR_SUPPORT = True if sys.platform == "win32": @@ -17,3 +19,15 @@ if sys.platform == "win32": kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7) except OSError: # pragma: no cover COLOR_SUPPORT = False + + +class LogLevel(Options): + name = 'LogLevel' + notset: int = 0 + trace: int = 5 + fine: int = 7 + debug: int = 10 + info: int = 20 + warn: int = 30 + error: int = 40 + fatal: int = 50 diff --git a/lib_not_dr/logger/formatter/__init__.py b/lib_not_dr/logger/formatter/__init__.py index 99c2a12..220483d 100644 --- a/lib_not_dr/logger/formatter/__init__.py +++ b/lib_not_dr/logger/formatter/__init__.py @@ -9,6 +9,7 @@ from pathlib import Path from string import Template from typing import List, Union, Optional, Dict, Tuple, TYPE_CHECKING +from lib_not_dr.logger import LogLevel from lib_not_dr.types.options import Options from lib_not_dr.logger.structure import LogMessage, FormattingMessage @@ -120,24 +121,24 @@ class LevelFormatter(BaseFormatter): level_get_higher: bool = True level_name_map = { - 0: 'NOTSET', - 5: 'TRACE', - 7: 'FINE', - 10: 'DEBUG', - 20: 'INFO', - 30: 'WARN', - 40: 'ERROR', - 50: 'FATAL' + LogLevel.notset: 'NOTSET', + LogLevel.trace: 'TRACE', + LogLevel.fine: 'FINE', + LogLevel.debug: 'DEBUG', + LogLevel.info: 'INFO', + LogLevel.warn: 'WARN', + LogLevel.error: 'ERROR', + LogLevel.fatal: 'FATAL', } name_level_map = { - 'NOTSET': 0, - 'TRACE': 5, - 'FINE': 7, - 'DEBUG': 10, - 'INFO': 20, - 'WARN': 30, - 'ERROR': 40, - 'FATAL': 50 + 'NOTSET': LogLevel.notset, + 'TRACE': LogLevel.trace, + 'FINE': LogLevel.fine, + 'DEBUG': LogLevel.debug, + 'INFO': LogLevel.info, + 'WARN': LogLevel.warn, + 'ERROR': LogLevel.error, + 'FATAL': LogLevel.fatal, } @classmethod @@ -266,7 +267,7 @@ if __name__ == '__main__': std_format = StdFormatter() std_format.default_template = "${log_time}|${logger_name}|${logger_tag}|${log_source}:${log_line}|${log_function}|${level}|${messages}" - test_levels = (0, 2, 5, 7, 10, 30, 50, 90) + test_levels = (0, 5, 7, 10, 20, 30, 40, 50) print("with color") diff --git a/lib_not_dr/logger/formatter/colors.py b/lib_not_dr/logger/formatter/colors.py index 3de19d4..bb32c2f 100644 --- a/lib_not_dr/logger/formatter/colors.py +++ b/lib_not_dr/logger/formatter/colors.py @@ -6,10 +6,10 @@ from typing import Dict, Tuple +from lib_not_dr.logger import LogLevel from lib_not_dr.logger.formatter import BaseFormatter from lib_not_dr.logger.structure import FormattingMessage - __all__ = [ 'BaseColorFormatter', @@ -30,21 +30,21 @@ class BaseColorFormatter(BaseFormatter): # TODO 迁移老 logger 颜色 color = { # Notset: just black - 0: '', + LogLevel.notset: '', # Trace: blue - 2: '\033[0;34m', + LogLevel.trace: '\033[0;34m', # Fine: green - 5: '\033[0;32m', + LogLevel.fine: '\033[0;32m', # Debug: cyan - 7: '\033[0;36m', + LogLevel.debug: '\033[0;36m', # Info: white - 10: '\033[0;37m', + LogLevel.info: '\033[0;37m', # Warn: yellow - 30: '\033[0;33m', + LogLevel.warn: '\033[0;33m', # Error: red - 50: '\033[0;31m', + LogLevel.error: '\033[0;31m', # Fatal: red background - 90: '\033[0;41m' + LogLevel.fatal: '\033[0;41m' } def get_color(self, message: FormattingMessage) -> str: @@ -61,21 +61,21 @@ class LevelColorFormatter(BaseColorFormatter): # TODO 迁移老 logger 颜色 color = { # Notset: just black - 0: '', + LogLevel.notset: '', # Trace: blue - 2: '\033[0;34m', + LogLevel.trace: '\033[0;34m', # Fine: green - 5: '\033[0;32m', + LogLevel.fine: '\033[0;32m', # Debug: cyan - 7: '\033[0;36m', + LogLevel.debug: '\033[0;36m', # Info: white - 10: '\033[0;37m', + LogLevel.info: '\033[0;37m', # Warn: yellow - 30: '\033[0;33m', + LogLevel.warn: '\033[0;33m', # Error: red - 50: '\033[0;31m', + LogLevel.error: '\033[0;31m', # Fatal: red background - 90: '\033[0;41m' + LogLevel.fatal: '\033[0;41m' } @classmethod @@ -99,21 +99,21 @@ class LoggerColorFormatter(BaseColorFormatter): # TODO 迁移老 logger 颜色 color = { # Notset: just black - 0: '', + LogLevel.notset: '', # Trace: blue - 2: '\033[0;34m', + LogLevel.trace: '\033[0;34m', # Fine: green - 5: '\033[0;32m', + LogLevel.fine: '\033[0;32m', # Debug: cyan - 7: '\033[0;36m', + LogLevel.debug: '\033[0;36m', # Info: white - 10: '\033[0;37m', + LogLevel.info: '\033[0;37m', # Warn: yellow - 30: '\033[0;33m', + LogLevel.warn: '\033[0;33m', # Error: red - 50: '\033[0;31m', + LogLevel.error: '\033[0;31m', # Fatal: red background - 90: '\033[38;2;245;189;230m', + LogLevel.fatal: '\033[38;2;245;189;230m', } @classmethod @@ -139,21 +139,21 @@ class TimeColorFormatter(BaseColorFormatter): # TODO 迁移老 logger 颜色 color = { # Notset: just black - 0: '', + LogLevel.notset: '', # Trace: blue - 2: '\033[0;34m', + LogLevel.trace: '\033[0;34m', # Fine: green - 5: '\033[0;32m', + LogLevel.fine: '\033[0;32m', # Debug: cyan - 7: '\033[0;36m', + LogLevel.debug: '\033[0;36m', # Info: white - 10: '\033[0;37m', + LogLevel.info: '\033[0;37m', # Warn: yellow - 30: '\033[0;33m', + LogLevel.warn: '\033[0;33m', # Error: red - 50: '\033[0;31m', + LogLevel.error: '\033[0;31m', # Fatal: red background - 90: '\033[0;41m', + LogLevel.fatal: '\033[0;41m', } @classmethod @@ -177,21 +177,21 @@ class TraceColorFormatter(BaseColorFormatter): # TODO 迁移老 logger 颜色 color = { # Notset: just black - 0: '\033[38;2;0;255;180m', + LogLevel.notset: '\033[38;2;0;255;180m', # Trace: blue - 2: '\033[38;2;0;255;180m', + LogLevel.trace: '\033[38;2;0;255;180m', # Fine: green - 5: '\033[38;2;0;255;180m', + LogLevel.fine: '\033[38;2;0;255;180m', # Debug: cyan - 7: '\033[38;2;0;255;180m', + LogLevel.debug: '\033[38;2;0;255;180m', # Info: white - 10: '\033[38;2;0;255;180m', + LogLevel.info: '\033[38;2;0;255;180m', # Warn: yellow - 30: '\033[38;2;0;255;180m', + LogLevel.warn: '\033[38;2;0;255;180m', # Error: red - 50: '\033[38;2;0;255;180m', + LogLevel.error: '\033[38;2;0;255;180m', # Fatal: red background - 90: '\033[38;2;0;255;180m', + LogLevel.fatal: '\033[38;2;0;255;180m', } @classmethod @@ -222,21 +222,21 @@ class MessageColorFormatter(BaseColorFormatter): color = { # Notset: just black - 0: '', + LogLevel.notset: '', # Trace: blue - 2: '\033[38;2;138;173;244m', + LogLevel.trace: '\033[38;2;138;173;244m', # Fine: blue - 5: '\033[38;2;138;173;244m', + LogLevel.fine: '\033[38;2;138;173;244m', # Debug: blue - 7: '\033[38;2;138;173;244m', + LogLevel.debug: '\033[38;2;138;173;244m', # Info: no color - 10: '', + LogLevel.info: '', # Warn: yellow - 30: '\033[0;33m', + LogLevel.warn: '\033[0;33m', # Error: red - 50: '\033[0;31m', + LogLevel.error: '\033[0;31m', # Fatal: red background - 90: '\033[38;2;255;255;0;48;2;120;10;10m', + LogLevel.fatal: '\033[38;2;255;255;0;48;2;120;10;10m', } @classmethod diff --git a/lib_not_dr/logger/logger.py b/lib_not_dr/logger/logger.py index 31f5dd2..72506d2 100644 --- a/lib_not_dr/logger/logger.py +++ b/lib_not_dr/logger/logger.py @@ -9,15 +9,17 @@ import inspect from types import FrameType from typing import List, Optional -from lib_not_dr.logger.structure import LogMessage -from lib_not_dr.logger.outstream import BaseOutputStream from lib_not_dr.types.options import Options +from lib_not_dr.logger.structure import LogMessage +from lib_not_dr.logger.outstream import BaseOutputStream, StdioOutputStream class Logger(Options): name = 'Logger-v2' - outputs: List[BaseOutputStream] = [] + outputs: List[BaseOutputStream] = [StdioOutputStream()] + + log_name: str = 'root' enable: bool = True level: int = 20 # info @@ -62,20 +64,19 @@ class Logger(Options): def make_log(self, messages: List[str], + tag: Optional[str] = None, end: str = '\n', split: str = ' ', flush: bool = True, - level: int = 20, - log_time: Optional[float] = None, - logger_name: str = 'root', - logger_tag: Optional[str] = None, + level: int = 20, # info + # log_time: Optional[float] = None, + # logger_name: str = 'root', + # logger_tag: Optional[str] = None, stack_trace: Optional[FrameType] = None) -> None: # 检查是否需要记录 if not self.log_for(level): return - # 处理时间 - if log_time is None: - log_time = time.time_ns() + log_time = time.time_ns() # 处理堆栈信息 if stack_trace is None: # 尝试获取堆栈信息 @@ -95,6 +96,29 @@ class Logger(Options): flush=flush, level=level, log_time=log_time, - logger_name=logger_name, - logger_tag=logger_tag, + logger_name=self.log_name, + logger_tag=tag, stack_trace=stack_trace) + if level >= 30: # WARN + for output in self.outputs: + output.write_stderr(message) + else: + for output in self.outputs: + output.write_stdout(message) + # done? + # 20231106 00:06 + + @staticmethod + def get_logger_by_name(name: str) -> 'Logger': + """ + Get a logger by name. + + Args: + name (str): The name of the logger. + + Returns: + Logger: The logger with the specified name. + """ + + + diff --git a/test/logger/formatter.py b/test/logger/formatter.py index 7cdd067..1bd6763 100644 --- a/test/logger/formatter.py +++ b/test/logger/formatter.py @@ -13,7 +13,7 @@ from lib_not_dr.logger.structure import LogMessage class FormatterTest(unittest.TestCase): - test_levels = (0, 2, 5, 7, 10, 30, 50, 90) + test_levels = (0, 5, 7, 10, 20, 30, 40, 50) def test_create_message(self): message = LogMessage(messages=['test'],