Impl logger

This commit is contained in:
shenjack 2023-11-06 00:31:03 +08:00
parent ae3de0b4b7
commit 669377dcac
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 142 additions and 5 deletions

20
example/logger/logger.py Normal file
View File

@ -0,0 +1,20 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
from lib_not_dr.logger.logger import Logger
if __name__ == '__main__':
logger = Logger.get_logger_by_name('test')
logger.level = 0
logger.info('Hello World!')
logger.fine('Hello World!')
logger.debug('Hello World!')
logger.trace('Hello World!')
logger.warn('Hello World!')
logger.error('Hello World!')
logger.fatal('Hello World!')

View File

@ -4,8 +4,6 @@
# All rights reserved
# -------------------------------
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

View File

@ -9,6 +9,7 @@ import inspect
from types import FrameType
from typing import List, Optional
from lib_not_dr.logger import LogLevel
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
@ -119,6 +120,123 @@ class Logger(Options):
Returns:
Logger: The logger with the specified name.
"""
return Logger(log_name=name)
def info(self,
*message,
tag: Optional[str] = None,
end: str = '\n',
split: str = ' ',
flush: bool = True,
stack_trace: Optional[FrameType] = None) -> None:
if not self.log_for(LogLevel.info):
return
self.make_log(messages=list(message),
tag=tag,
end=end,
split=split,
flush=flush,
level=LogLevel.info,
stack_trace=stack_trace)
def trace(self,
*message,
tag: Optional[str] = None,
end: str = '\n',
split: str = ' ',
flush: bool = True,
stack_trace: Optional[FrameType] = None) -> None:
if not self.log_for(LogLevel.trace):
return
self.make_log(messages=list(message),
tag=tag,
end=end,
split=split,
flush=flush,
level=LogLevel.trace,
stack_trace=stack_trace)
def fine(self,
*message,
tag: Optional[str] = None,
end: str = '\n',
split: str = ' ',
flush: bool = True,
stack_trace: Optional[FrameType] = None) -> None:
if not self.log_for(LogLevel.fine):
return
self.make_log(messages=list(message),
tag=tag,
end=end,
split=split,
flush=flush,
level=LogLevel.fine,
stack_trace=stack_trace)
def debug(self,
*message,
tag: Optional[str] = None,
end: str = '\n',
split: str = ' ',
flush: bool = True,
stack_trace: Optional[FrameType] = None) -> None:
if not self.log_for(LogLevel.debug):
return
self.make_log(messages=list(message),
tag=tag,
end=end,
split=split,
flush=flush,
level=LogLevel.debug,
stack_trace=stack_trace)
def warn(self,
*message,
tag: Optional[str] = None,
end: str = '\n',
split: str = ' ',
flush: bool = True,
stack_trace: Optional[FrameType] = None) -> None:
if not self.log_for(LogLevel.warn):
return
self.make_log(messages=list(message),
tag=tag,
end=end,
split=split,
flush=flush,
level=LogLevel.warn,
stack_trace=stack_trace)
def error(self,
*message,
tag: Optional[str] = None,
end: str = '\n',
split: str = ' ',
flush: bool = True,
stack_trace: Optional[FrameType] = None) -> None:
if not self.log_for(LogLevel.error):
return
self.make_log(messages=list(message),
tag=tag,
end=end,
split=split,
flush=flush,
level=LogLevel.error,
stack_trace=stack_trace)
def fatal(self,
*message,
tag: Optional[str] = None,
end: str = '\n',
split: str = ' ',
flush: bool = True,
stack_trace: Optional[FrameType] = None) -> None:
if not self.log_for(LogLevel.fatal):
return
self.make_log(messages=list(message),
tag=tag,
end=end,
split=split,
flush=flush,
level=LogLevel.fatal,
stack_trace=stack_trace)

View File

@ -14,6 +14,7 @@ import threading
from pathlib import Path
from typing import Optional
from lib_not_dr.logger import LogLevel
from lib_not_dr.types.options import Options
from lib_not_dr.logger.structure import LogMessage
from lib_not_dr.logger.formatter import BaseFormatter, StdFormatter
@ -28,7 +29,7 @@ __all__ = [
class BaseOutputStream(Options):
name = 'BaseOutputStream'
level: int = 20
level: int = LogLevel.info
enable: bool = True
formatter: BaseFormatter
@ -49,7 +50,7 @@ class BaseOutputStream(Options):
class StdioOutputStream(BaseOutputStream):
name = 'StdioOutputStream'
level: int = 20
level: int = LogLevel.info
formatter: BaseFormatter = StdFormatter()
def write_stdout(self, message: LogMessage) -> None:
@ -81,7 +82,7 @@ class StdioOutputStream(BaseOutputStream):
class FileCacheOutputStream(BaseOutputStream):
name = 'FileCacheOutputStream'
level: int = 20
level: int = LogLevel.info
formatter: BaseFormatter = StdFormatter(enable_color=False)
text_cache: io.StringIO = None