Compare commits
No commits in common. "b45fc0591344cd5e8340a7c28daa42d7802f4420" and "0d97055fad20746ab93e908854fb6d95fe9f27a4" have entirely different histories.
b45fc05913
...
0d97055fad
@ -3,10 +3,11 @@
|
|||||||
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
||||||
# All rights reserved
|
# All rights reserved
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from string import Template
|
from string import Template
|
||||||
from typing import List, Union, Optional, Dict, Tuple
|
from typing import List, Union, Optional
|
||||||
|
|
||||||
from lib_not_dr.types.options import Options
|
from lib_not_dr.types.options import Options
|
||||||
from lib_not_dr.logger.structers import LogMessage, FormattingMessage
|
from lib_not_dr.logger.structers import LogMessage, FormattingMessage
|
||||||
@ -25,10 +26,13 @@ class BaseFormatter(Options):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def info(cls) -> str:
|
def info(cls) -> str:
|
||||||
infos = {BaseFormatter.name: BaseFormatter._info()}
|
infos = {BaseFormatter.name: BaseFormatter._info()}
|
||||||
|
# cache = "## Base Formatter\n"
|
||||||
|
# cache += BaseFormatter._info()
|
||||||
|
# cache += f"## {cls.name}\n"
|
||||||
|
# cache += cls._info()
|
||||||
cache = ''
|
cache = ''
|
||||||
for formatter in cls.sub_formatter:
|
for formatter in cls.sub_formatter:
|
||||||
infos[formatter.name] = formatter._info()
|
infos[formatter.name] = formatter._info()
|
||||||
infos[cls.name] = cls._info()
|
|
||||||
for name, info in infos.items():
|
for name, info in infos.items():
|
||||||
cache += f"## {name}\n"
|
cache += f"## {name}\n"
|
||||||
cache += info
|
cache += info
|
||||||
@ -84,8 +88,7 @@ class TimeFormatter(BaseFormatter):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def _info(cls) -> str:
|
def _info(cls) -> str:
|
||||||
return cls.add_info('log_time', 'formatted time when logging', 'The time format string'
|
return cls.add_info('log_time', 'formatted time when logging', 'The time format string'
|
||||||
'. See https://docs.python.org/3/library/time'
|
'. See https://docs.python.org/3/library/time.html#time.strftime for more information.')
|
||||||
'.html#time.strftime for more information.')
|
|
||||||
|
|
||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||||
time_mark = time.localtime(message[0].log_time / 1000000000)
|
time_mark = time.localtime(message[0].log_time / 1000000000)
|
||||||
@ -105,10 +108,10 @@ class LevelFormatter(BaseFormatter):
|
|||||||
level_get_higher: bool = True
|
level_get_higher: bool = True
|
||||||
|
|
||||||
level_name_map = {
|
level_name_map = {
|
||||||
0: 'NOTSET',
|
0: 'NOTSET',
|
||||||
2: 'TRACE',
|
2: 'TRACE',
|
||||||
5: 'FINE',
|
5: 'FINE',
|
||||||
7: 'DEBUG',
|
7: 'DEBUG',
|
||||||
10: 'INFO',
|
10: 'INFO',
|
||||||
30: 'WARN',
|
30: 'WARN',
|
||||||
50: 'ERROR',
|
50: 'ERROR',
|
||||||
@ -116,13 +119,13 @@ class LevelFormatter(BaseFormatter):
|
|||||||
}
|
}
|
||||||
name_level_map = {
|
name_level_map = {
|
||||||
'NOTSET': 0,
|
'NOTSET': 0,
|
||||||
'TRACE': 2,
|
'TRACE': 2,
|
||||||
'FINE': 5,
|
'FINE': 5,
|
||||||
'DEBUG': 7,
|
'DEBUG': 7,
|
||||||
'INFO': 10,
|
'INFO': 10,
|
||||||
'WARN': 30,
|
'WARN': 30,
|
||||||
'ERROR': 50,
|
'ERROR': 50,
|
||||||
'FATAL': 90
|
'FATAL': 90
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -151,67 +154,23 @@ class LevelFormatter(BaseFormatter):
|
|||||||
return message
|
return message
|
||||||
|
|
||||||
|
|
||||||
class TraceFormatter(BaseFormatter):
|
|
||||||
name = 'TraceFormatter'
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _info(cls) -> str:
|
|
||||||
info = cls.add_info('logging file', 'log_source', 'the logging file name')
|
|
||||||
info += '\n'
|
|
||||||
info += cls.add_info('logging line', 'log_line', 'the logging line number')
|
|
||||||
info += '\n'
|
|
||||||
info += cls.add_info('logging function', 'log_function', 'the logging function name')
|
|
||||||
return info
|
|
||||||
|
|
||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
|
||||||
if message[0].stack_trace is None:
|
|
||||||
return message
|
|
||||||
message[1]['log_source'] = message[0].stack_trace.f_code.co_filename
|
|
||||||
message[1]['log_line'] = message[0].stack_trace.f_lineno
|
|
||||||
message[1]['log_function'] = message[0].stack_trace.f_code.co_name
|
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
||||||
class StdFormatter(BaseFormatter):
|
class StdFormatter(BaseFormatter):
|
||||||
name = 'StdFormatter'
|
name = 'StdFormatter'
|
||||||
|
|
||||||
sub_formatter = [TimeFormatter(),
|
sub_formatter = [TimeFormatter(), LevelFormatter()]
|
||||||
LevelFormatter(),
|
|
||||||
TraceFormatter()]
|
|
||||||
|
|
||||||
from lib_not_dr.logger.formatter.colors import LevelColorFormatter, LoggerColorFormatter, TimeColorFormatter
|
|
||||||
color_formatters = [LevelColorFormatter(),
|
|
||||||
LoggerColorFormatter(),
|
|
||||||
TimeColorFormatter()]
|
|
||||||
|
|
||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
|
||||||
super()._format(message)
|
|
||||||
for formatter in self.color_formatters:
|
|
||||||
message = formatter._format(message)
|
|
||||||
return message
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _info(cls) -> str:
|
def _info(cls) -> str:
|
||||||
return 'None'
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import inspect
|
|
||||||
|
|
||||||
log_message = LogMessage(messages=['Hello World!'],
|
|
||||||
level=10,
|
|
||||||
stack_trace=inspect.currentframe(),
|
|
||||||
logger_tag='tester',
|
|
||||||
logger_name='test')
|
|
||||||
|
|
||||||
print(TimeFormatter.info())
|
print(TimeFormatter.info())
|
||||||
print(TimeFormatter().format_message(log_message))
|
print(TimeFormatter().format_message(LogMessage(messages=['Hello World!'])))
|
||||||
|
|
||||||
print(LevelFormatter.info())
|
print(LevelFormatter.info())
|
||||||
print(LevelFormatter().format_message(log_message))
|
print(LevelFormatter().format_message(LogMessage(messages=['Hello World!'], level=10)))
|
||||||
|
|
||||||
print(TraceFormatter.info())
|
|
||||||
print(TraceFormatter().format_message(log_message))
|
|
||||||
|
|
||||||
print(StdFormatter.info())
|
print(StdFormatter.info())
|
||||||
print(StdFormatter().format_message(log_message))
|
print(StdFormatter().format_message(LogMessage(messages=['Hello World!'], level=10)))
|
@ -1,172 +0,0 @@
|
|||||||
# -------------------------------
|
|
||||||
# Difficult Rocket
|
|
||||||
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
|
||||||
# All rights reserved
|
|
||||||
# -------------------------------
|
|
||||||
|
|
||||||
from typing import Dict, Tuple
|
|
||||||
|
|
||||||
from lib_not_dr.logger.formatter import BaseFormatter
|
|
||||||
from lib_not_dr.logger.structers import FormattingMessage
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'BaseColorFormatter',
|
|
||||||
'LevelColorFormatter',
|
|
||||||
'LoggerColorFormatter',
|
|
||||||
'RESET_COLOR'
|
|
||||||
]
|
|
||||||
|
|
||||||
RESET_COLOR = '\033[0m'
|
|
||||||
|
|
||||||
|
|
||||||
class BaseColorFormatter(BaseFormatter):
|
|
||||||
name = 'BaseColorFormatter'
|
|
||||||
|
|
||||||
color = {
|
|
||||||
# Notset: just black
|
|
||||||
0: '',
|
|
||||||
# Trace: blue
|
|
||||||
2: '\033[0;34m',
|
|
||||||
# Fine: green
|
|
||||||
5: '\033[0;32m',
|
|
||||||
# Debug: cyan
|
|
||||||
7: '\033[0;36m',
|
|
||||||
# Info: white
|
|
||||||
10: '\033[0;37m',
|
|
||||||
# Warn: yellow
|
|
||||||
30: '\033[0;33m',
|
|
||||||
# Error: red
|
|
||||||
50: '\033[0;31m',
|
|
||||||
# Fatal: red background
|
|
||||||
90: '\033[0;41m'
|
|
||||||
}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_level(cls, message: FormattingMessage) -> int:
|
|
||||||
for level in cls.color:
|
|
||||||
if message[0].level < level:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
level = 90
|
|
||||||
return level
|
|
||||||
|
|
||||||
|
|
||||||
class LevelColorFormatter(BaseColorFormatter):
|
|
||||||
name = 'LevelColorFormatter'
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _default_color() -> Dict[int, str]:
|
|
||||||
return {
|
|
||||||
# Notset: just black
|
|
||||||
0: '',
|
|
||||||
# Trace: blue
|
|
||||||
2: '\033[0;34m',
|
|
||||||
# Fine: green
|
|
||||||
5: '\033[0;32m',
|
|
||||||
# Debug: cyan
|
|
||||||
7: '\033[0;36m',
|
|
||||||
# Info: white
|
|
||||||
10: '\033[0;37m',
|
|
||||||
# Warn: yellow
|
|
||||||
30: '\033[0;33m',
|
|
||||||
# Error: red
|
|
||||||
50: '\033[0;31m',
|
|
||||||
# Fatal: red background
|
|
||||||
90: '\033[0;41m'
|
|
||||||
}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _info(cls) -> str:
|
|
||||||
return cls.add_info('colored level', 'level', 'A colored level')
|
|
||||||
|
|
||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
|
||||||
if isinstance(message[1].get('level'), int):
|
|
||||||
return message
|
|
||||||
# 向上寻找等级
|
|
||||||
level = self.get_level(message)
|
|
||||||
# 获取颜色
|
|
||||||
color = self.color[level]
|
|
||||||
# 添加颜色
|
|
||||||
message[1]['level'] = f'{color}{message[1]["level"]}{RESET_COLOR}'
|
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
||||||
class LoggerColorFormatter(BaseColorFormatter):
|
|
||||||
name = 'LoggerColorFormatter'
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _default_color() -> Dict[int, str]:
|
|
||||||
return {
|
|
||||||
# Notset: just black
|
|
||||||
0: '',
|
|
||||||
# Trace: blue
|
|
||||||
2: '\033[0;34m',
|
|
||||||
# Fine: green
|
|
||||||
5: '\033[0;32m',
|
|
||||||
# Debug: cyan
|
|
||||||
7: '\033[0;36m',
|
|
||||||
# Info: white
|
|
||||||
10: '\033[0;37m',
|
|
||||||
# Warn: yellow
|
|
||||||
30: '\033[0;33m',
|
|
||||||
# Error: red
|
|
||||||
50: '\033[0;31m',
|
|
||||||
# Fatal: red background
|
|
||||||
90: '\033[0;41m',
|
|
||||||
}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _info(cls) -> str:
|
|
||||||
return cls.add_info('colored logger name', 'logger name', 'A colored logger name')
|
|
||||||
|
|
||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
|
||||||
if message[1].get('logger_name') is None:
|
|
||||||
return message
|
|
||||||
# 向上寻找等级
|
|
||||||
level = self.get_level(message)
|
|
||||||
# 获取颜色
|
|
||||||
color = self.color[level]
|
|
||||||
# 添加颜色
|
|
||||||
message[1]['logger_name'] = f'{color}{message[1]["logger_name"]}{RESET_COLOR}'
|
|
||||||
if message[1].get('logger_tag') is not None and message[1].get('logger_tag') != ' ':
|
|
||||||
message[1]['logger_tag'] = f'{color}{message[1]["logger_tag"]}{RESET_COLOR}'
|
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
||||||
class TimeColorFormatter(BaseColorFormatter):
|
|
||||||
name = 'TimeColorFormatter'
|
|
||||||
|
|
||||||
color = {
|
|
||||||
# Notset: just black
|
|
||||||
0: '',
|
|
||||||
# Trace: blue
|
|
||||||
2: '\033[0;34m',
|
|
||||||
# Fine: green
|
|
||||||
5: '\033[0;32m',
|
|
||||||
# Debug: cyan
|
|
||||||
7: '\033[0;36m',
|
|
||||||
# Info: white
|
|
||||||
10: '\033[0;37m',
|
|
||||||
# Warn: yellow
|
|
||||||
30: '\033[0;33m',
|
|
||||||
# Error: red
|
|
||||||
50: '\033[0;31m',
|
|
||||||
# Fatal: red background
|
|
||||||
90: '\033[0;41m',
|
|
||||||
}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _info(cls) -> str:
|
|
||||||
return cls.add_info('colored time', 'time', 'A colored time')
|
|
||||||
|
|
||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
|
||||||
if message[1].get('log_time') is None:
|
|
||||||
return message
|
|
||||||
# 向上寻找等级
|
|
||||||
level = self.get_level(message)
|
|
||||||
# 获取颜色
|
|
||||||
color = self.color[level]
|
|
||||||
# 添加颜色
|
|
||||||
message[1]['log_time'] = f'{color}{message[1]["log_time"]}{RESET_COLOR}'
|
|
||||||
return message
|
|
Loading…
Reference in New Issue
Block a user