迁移了logger
This commit is contained in:
parent
25c26750dd
commit
175ec241c4
5
libs/utils/__init__.py
Normal file
5
libs/utils/__init__.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# -------------------------------
|
||||||
|
# Difficult Rocket
|
||||||
|
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
|
||||||
|
# All rights reserved
|
||||||
|
# -------------------------------
|
24669
libs/utils/logger.c
Normal file
24669
libs/utils/logger.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -2,6 +2,11 @@
|
|||||||
@author shenjackyuanjie
|
@author shenjackyuanjie
|
||||||
@contact 3695888@qq.com
|
@contact 3695888@qq.com
|
||||||
"""
|
"""
|
||||||
|
# -------------------------------
|
||||||
|
# Difficult Rocket
|
||||||
|
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
|
||||||
|
# All rights reserved
|
||||||
|
# -------------------------------
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
@ -147,6 +152,7 @@ logger_configs = {
|
|||||||
|
|
||||||
|
|
||||||
class ThreadLock:
|
class ThreadLock:
|
||||||
|
"""一个用来 with 的线程锁"""
|
||||||
|
|
||||||
def __init__(self, the_lock: threading.Lock, time_out: Union[float, int] = 1 / 60) -> None:
|
def __init__(self, the_lock: threading.Lock, time_out: Union[float, int] = 1 / 60) -> None:
|
||||||
self.lock = the_lock
|
self.lock = the_lock
|
||||||
@ -170,7 +176,7 @@ class ListCache:
|
|||||||
self._cache = []
|
self._cache = []
|
||||||
self.with_thread_lock = lock
|
self.with_thread_lock = lock
|
||||||
|
|
||||||
def append(self, value: Union[str, Iterable]):
|
def append(self, value: Union[str, Iterable[str]]):
|
||||||
with self.with_thread_lock:
|
with self.with_thread_lock:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
self._cache.append(value)
|
self._cache.append(value)
|
||||||
@ -179,7 +185,7 @@ class ListCache:
|
|||||||
else:
|
else:
|
||||||
raise TypeError(f"cache must be string or Iterable. not a {type(value)}")
|
raise TypeError(f"cache must be string or Iterable. not a {type(value)}")
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item) -> str:
|
||||||
assert isinstance(item, int)
|
assert isinstance(item, int)
|
||||||
with self.with_thread_lock:
|
with self.with_thread_lock:
|
||||||
try:
|
try:
|
||||||
@ -188,7 +194,7 @@ class ListCache:
|
|||||||
print(f'cache:{self.cache}')
|
print(f'cache:{self.cache}')
|
||||||
raise IndexError(f'there is no cache at {item}!\ncache:{self.cache}\n{exp}')
|
raise IndexError(f'there is no cache at {item}!\ncache:{self.cache}\n{exp}')
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs) -> List[str]:
|
||||||
return self.cache
|
return self.cache
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
@ -345,11 +351,12 @@ class Logger:
|
|||||||
frame: Optional[FrameType] = None) -> None:
|
frame: Optional[FrameType] = None) -> None:
|
||||||
if level < self.min_level:
|
if level < self.min_level:
|
||||||
return None
|
return None
|
||||||
|
_frame = inspect.currentframe()
|
||||||
if not frame:
|
if not frame:
|
||||||
frame = inspect.currentframe()
|
frame = inspect.currentframe()
|
||||||
frame = frame.f_back.f_back
|
frame = frame.f_back.f_back
|
||||||
elif (frame := inspect.currentframe()) is not None:
|
elif _frame is not None:
|
||||||
frame = frame.f_back
|
frame = _frame.f_back
|
||||||
text = sep.join(i if type(i) is str else str(i) for i in values)
|
text = sep.join(i if type(i) is str else str(i) for i in values)
|
||||||
text = f"{self.colors[level]['message']}{text}{color_reset_suffix}"
|
text = f"{self.colors[level]['message']}{text}{color_reset_suffix}"
|
||||||
print_text = self.format_text(level=level, text=text, frame=frame)
|
print_text = self.format_text(level=level, text=text, frame=frame)
|
||||||
@ -384,49 +391,56 @@ class Logger:
|
|||||||
def trace(self, *values: object,
|
def trace(self, *values: object,
|
||||||
sep: Optional[str] = ' ',
|
sep: Optional[str] = ' ',
|
||||||
end: Optional[str] = '\n',
|
end: Optional[str] = '\n',
|
||||||
flush: Optional[bool] = False) -> None:
|
flush: Optional[bool] = False,
|
||||||
return self.make_log(*values, level=TRACE, sep=sep, end=end, flush=flush)
|
frame: Optional[FrameType] = None) -> None:
|
||||||
|
return self.make_log(*values, level=TRACE, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
def fine(self, *values: object,
|
def fine(self, *values: object,
|
||||||
sep: Optional[str] = ' ',
|
sep: Optional[str] = ' ',
|
||||||
end: Optional[str] = '\n',
|
end: Optional[str] = '\n',
|
||||||
flush: Optional[bool] = False) -> None:
|
flush: Optional[bool] = False,
|
||||||
return self.make_log(*values, level=FINE, sep=sep, end=end, flush=flush)
|
frame: Optional[FrameType] = None) -> None:
|
||||||
|
return self.make_log(*values, level=FINE, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
def debug(self,
|
def debug(self,
|
||||||
*values: object,
|
*values: object,
|
||||||
sep: Optional[str] = ' ',
|
sep: Optional[str] = ' ',
|
||||||
end: Optional[str] = '\n',
|
end: Optional[str] = '\n',
|
||||||
flush: Optional[bool] = False) -> None:
|
flush: Optional[bool] = False,
|
||||||
return self.make_log(*values, level=DEBUG, sep=sep, end=end, flush=flush)
|
frame: Optional[FrameType] = None) -> None:
|
||||||
|
return self.make_log(*values, level=DEBUG, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
def info(self,
|
def info(self,
|
||||||
*values: object,
|
*values: object,
|
||||||
sep: Optional[str] = ' ',
|
sep: Optional[str] = ' ',
|
||||||
end: Optional[str] = '\n',
|
end: Optional[str] = '\n',
|
||||||
flush: Optional[bool] = False) -> None:
|
flush: Optional[bool] = False,
|
||||||
return self.make_log(*values, level=INFO, sep=sep, end=end, flush=flush)
|
frame: Optional[FrameType] = None) -> None:
|
||||||
|
return self.make_log(*values, level=INFO, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
def warning(self,
|
def warning(self,
|
||||||
*values: object,
|
*values: object,
|
||||||
sep: Optional[str] = ' ',
|
sep: Optional[str] = ' ',
|
||||||
end: Optional[str] = '\n',
|
end: Optional[str] = '\n',
|
||||||
flush: Optional[bool] = False) -> None:
|
flush: Optional[bool] = False,
|
||||||
return self.make_log(*values, level=WARNING, sep=sep, end=end, flush=flush)
|
frame: Optional[FrameType] = None) -> None:
|
||||||
|
return self.make_log(*values, level=WARNING, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
def error(self,
|
def error(self,
|
||||||
*values: object,
|
*values: object,
|
||||||
sep: Optional[str] = ' ',
|
sep: Optional[str] = ' ',
|
||||||
end: Optional[str] = '\n',
|
end: Optional[str] = '\n',
|
||||||
flush: Optional[bool] = False) -> None:
|
flush: Optional[bool] = False,
|
||||||
return self.make_log(*values, level=ERROR, sep=sep, end=end, flush=flush)
|
frame: Optional[FrameType] = None) -> None:
|
||||||
|
return self.make_log(*values, level=ERROR, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
def fatal(self,
|
def fatal(self,
|
||||||
*values: object,
|
*values: object,
|
||||||
sep: Optional[str] = ' ',
|
sep: Optional[str] = ' ',
|
||||||
end: Optional[str] = '\n',
|
end: Optional[str] = '\n',
|
||||||
flush: Optional[bool] = False) -> None:
|
flush: Optional[bool] = False,
|
||||||
return self.make_log(*values, level=FATAL, sep=sep, end=end, flush=flush)
|
frame: Optional[FrameType] = None) -> None:
|
||||||
|
return self.make_log(*values, level=FATAL, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
|
|
||||||
def get_key_from_dict(a_dict: Dict, key: Any, default: Any = None) -> Optional[Any]:
|
def get_key_from_dict(a_dict: Dict, key: Any, default: Any = None) -> Optional[Any]:
|
||||||
@ -554,17 +568,15 @@ def test_logger(the_logger: Logger):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
os.chdir('D:/githubs/DR')
|
os.chdir('../../')
|
||||||
logger = get_logger('server')
|
logger = get_logger('server')
|
||||||
|
|
||||||
logger.info('my name is:', logger.name)
|
logger.info('my name is:', logger.name)
|
||||||
a_logger = get_logger('client')
|
a_logger = get_logger('client')
|
||||||
|
|
||||||
a_logger.trace('tracing')
|
a_logger.trace('tracing')
|
||||||
# time.sleep(1.1)
|
|
||||||
a_logger.fine('some fine!')
|
a_logger.fine('some fine!')
|
||||||
a_logger.debug('debugging')
|
a_logger.debug('debugging')
|
||||||
# time.sleep(1.1)
|
|
||||||
a_logger.info("Hello World!!")
|
a_logger.info("Hello World!!")
|
||||||
a_logger.warn('warning')
|
a_logger.warn('warning')
|
||||||
a_logger.error('error haaaa')
|
a_logger.error('error haaaa')
|
46
libs/utils/logger.pyi
Normal file
46
libs/utils/logger.pyi
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# -------------------------------
|
||||||
|
# Difficult Rocket
|
||||||
|
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
|
||||||
|
# All rights reserved
|
||||||
|
# -------------------------------
|
||||||
|
import threading
|
||||||
|
|
||||||
|
from types import FrameType
|
||||||
|
from typing import Union, List, Optional, Iterable
|
||||||
|
|
||||||
|
class ThreadLock:
|
||||||
|
# with ThreadLock
|
||||||
|
def __init__(self, the_lock: threading.Lock, time_out: Union[float, int] = 1 / 60) -> None: ...
|
||||||
|
def __enter__(self) -> "ThreadLock": ...
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb) -> None: ...
|
||||||
|
|
||||||
|
class ListCache:
|
||||||
|
"""一个线程安全的列表缓存"""
|
||||||
|
def __init__(self, lock: ThreadLock) -> None: ...
|
||||||
|
def __call__(self, *args, **kwargs) -> List[str]: ...
|
||||||
|
# ListCache()
|
||||||
|
def __iter__(self) -> "ListCache": ...
|
||||||
|
def __next__(self) -> str: ...
|
||||||
|
# support for x in ListCache
|
||||||
|
def __bool__(self) -> bool: ...
|
||||||
|
# True if have cache
|
||||||
|
def __getitem__(self, item: int) -> str: ...
|
||||||
|
# ListCache[int]
|
||||||
|
def append(self, value: Union[str, Iterable[str]]): ...
|
||||||
|
# ListCache.append('abc' | ['abc'])
|
||||||
|
@property
|
||||||
|
def cache(self) -> List[str]: ...
|
||||||
|
# ListCache.cache
|
||||||
|
def clear(self) -> None: ...
|
||||||
|
# ListCache.clear()
|
||||||
|
|
||||||
|
|
||||||
|
class LogFileCache:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
class Logger:
|
||||||
|
...
|
||||||
|
|
||||||
|
def test_logger(the_logger: Logger) -> None:
|
||||||
|
...
|
11
libs/utils/logger_setup.py
Normal file
11
libs/utils/logger_setup.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# -------------------------------
|
||||||
|
# Difficult Rocket
|
||||||
|
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
|
||||||
|
# All rights reserved
|
||||||
|
# -------------------------------
|
||||||
|
# import setuptools
|
||||||
|
from setuptools import setup
|
||||||
|
from Cython.Build import cythonize
|
||||||
|
setup(name='logger',
|
||||||
|
zip_safe=False,
|
||||||
|
ext_modules=cythonize('logger.py', language_level=3))
|
@ -2,3 +2,8 @@ psutil
|
|||||||
pillow
|
pillow
|
||||||
objprint
|
objprint
|
||||||
# selenium
|
# selenium
|
||||||
|
|
||||||
|
toml
|
||||||
|
setuptools
|
||||||
|
Cython
|
||||||
|
colorama
|
Loading…
Reference in New Issue
Block a user