Impl logger
This commit is contained in:
parent
ae3de0b4b7
commit
669377dcac
20
example/logger/logger.py
Normal file
20
example/logger/logger.py
Normal 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!')
|
@ -4,8 +4,6 @@
|
|||||||
# All rights reserved
|
# All rights reserved
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
from typing import Dict, Tuple
|
|
||||||
|
|
||||||
from lib_not_dr.logger import LogLevel
|
from lib_not_dr.logger import LogLevel
|
||||||
from lib_not_dr.logger.formatter import BaseFormatter
|
from lib_not_dr.logger.formatter import BaseFormatter
|
||||||
from lib_not_dr.logger.structure import FormattingMessage
|
from lib_not_dr.logger.structure import FormattingMessage
|
||||||
|
@ -9,6 +9,7 @@ import inspect
|
|||||||
from types import FrameType
|
from types import FrameType
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from lib_not_dr.logger import LogLevel
|
||||||
from lib_not_dr.types.options import Options
|
from lib_not_dr.types.options import Options
|
||||||
from lib_not_dr.logger.structure import LogMessage
|
from lib_not_dr.logger.structure import LogMessage
|
||||||
from lib_not_dr.logger.outstream import BaseOutputStream, StdioOutputStream
|
from lib_not_dr.logger.outstream import BaseOutputStream, StdioOutputStream
|
||||||
@ -119,6 +120,123 @@ class Logger(Options):
|
|||||||
Returns:
|
Returns:
|
||||||
Logger: The logger with the specified name.
|
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)
|
||||||
|
@ -14,6 +14,7 @@ import threading
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from lib_not_dr.logger import LogLevel
|
||||||
from lib_not_dr.types.options import Options
|
from lib_not_dr.types.options import Options
|
||||||
from lib_not_dr.logger.structure import LogMessage
|
from lib_not_dr.logger.structure import LogMessage
|
||||||
from lib_not_dr.logger.formatter import BaseFormatter, StdFormatter
|
from lib_not_dr.logger.formatter import BaseFormatter, StdFormatter
|
||||||
@ -28,7 +29,7 @@ __all__ = [
|
|||||||
class BaseOutputStream(Options):
|
class BaseOutputStream(Options):
|
||||||
name = 'BaseOutputStream'
|
name = 'BaseOutputStream'
|
||||||
|
|
||||||
level: int = 20
|
level: int = LogLevel.info
|
||||||
enable: bool = True
|
enable: bool = True
|
||||||
|
|
||||||
formatter: BaseFormatter
|
formatter: BaseFormatter
|
||||||
@ -49,7 +50,7 @@ class BaseOutputStream(Options):
|
|||||||
class StdioOutputStream(BaseOutputStream):
|
class StdioOutputStream(BaseOutputStream):
|
||||||
name = 'StdioOutputStream'
|
name = 'StdioOutputStream'
|
||||||
|
|
||||||
level: int = 20
|
level: int = LogLevel.info
|
||||||
formatter: BaseFormatter = StdFormatter()
|
formatter: BaseFormatter = StdFormatter()
|
||||||
|
|
||||||
def write_stdout(self, message: LogMessage) -> None:
|
def write_stdout(self, message: LogMessage) -> None:
|
||||||
@ -81,7 +82,7 @@ class StdioOutputStream(BaseOutputStream):
|
|||||||
class FileCacheOutputStream(BaseOutputStream):
|
class FileCacheOutputStream(BaseOutputStream):
|
||||||
name = 'FileCacheOutputStream'
|
name = 'FileCacheOutputStream'
|
||||||
|
|
||||||
level: int = 20
|
level: int = LogLevel.info
|
||||||
formatter: BaseFormatter = StdFormatter(enable_color=False)
|
formatter: BaseFormatter = StdFormatter(enable_color=False)
|
||||||
text_cache: io.StringIO = None
|
text_cache: io.StringIO = None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user