logger.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import sys
  5. import logging
  6. import os
  7. import shutil
  8. from datetime import datetime, timedelta, date
  9. from io import TextIOBase
  10. from pathlib import Path
  11. from rich.console import Console, ConsoleOptions, ConsoleRenderable, NewLine, RenderResult
  12. from rich.highlighter import NullHighlighter
  13. from rich.logging import RichHandler
  14. from rich.rule import Rule
  15. from typing import Callable, List
  16. def cleanup_logs(log_dir: str = "./log", keep_days: int = 7):
  17. """删除 log_dir 下所有早于 keep_days 的文件夹和文件"""
  18. log_path = Path(log_dir)
  19. if not log_path.exists():
  20. return # 目录都没有,直接退出
  21. keep_days_ago_ts = (datetime.now() - timedelta(days=keep_days)).timestamp()
  22. for name in os.listdir(log_path):
  23. full_path = os.path.join(log_path, name)
  24. # 忽略软链接,仅处理文件和目录
  25. if not os.path.exists(full_path):
  26. continue
  27. if os.path.isfile(full_path):
  28. # 处理 log 根目录下超过keep_days的文件
  29. try:
  30. if os.path.getmtime(full_path) < keep_days_ago_ts:
  31. os.remove(full_path)
  32. except OSError as e:
  33. logger.error(f"delete file '{full_path}' error: {e}")
  34. elif os.path.isdir(full_path):
  35. # 检查是否为 error 目录
  36. if name != 'error':
  37. continue
  38. for error_dir_name in os.listdir(full_path):
  39. error_dir_path = os.path.join(full_path, error_dir_name)
  40. if not os.path.isdir(error_dir_path):
  41. continue
  42. # 处理 log/error 根目录下超过keep_days的文件夹
  43. try:
  44. if os.path.getmtime(error_dir_path) < keep_days_ago_ts:
  45. # 递归删除整个目录及其内容
  46. shutil.rmtree(error_dir_path)
  47. except OSError as e:
  48. logger.error(f"delete dir '{error_dir_path}' error: {e}")
  49. def empty_function(*args, **kwargs):
  50. pass
  51. # Ensure running in Alas root folder
  52. os.chdir(os.path.join(os.path.dirname(__file__), '../'))
  53. # cnocr will set root logger in cnocr.utils
  54. # Delete logging.basicConfig to avoid logging the same message twice.
  55. logging.basicConfig = empty_function
  56. logging.raiseExceptions = True # Set True if wanna see encode errors on console
  57. # Remove HTTP keywords (GET, POST etc.)
  58. # RichHandler.KEYWORDS = []
  59. # def show_handlers(handlers):
  60. # # 获取并打印日志记录器中处理器的信息
  61. # for handler in logger.handlers:
  62. # # 获取处理器的类名
  63. # handler_class = handler.__class__.__name__
  64. # print(f"Handler class: {handler_class}")
  65. #
  66. # # 获取处理器的级别
  67. # handler_level = logging.getLevelName(handler.level)
  68. # print(f"Handler level: {handler_level}")
  69. #
  70. # # 获取处理器的格式化器
  71. # formatter = handler.formatter
  72. # if formatter is not None:
  73. # formatter_class = formatter.__class__.__name__
  74. # print(f"Formatter class: {formatter_class}")
  75. #
  76. # # 其他处理器的属性和方法,根据需要进行获取和打印
  77. # print() # 打印空行,用于分隔处理器的信息
  78. # Logger init
  79. logger_debug = False
  80. logger = logging.getLogger('oas')
  81. logger.setLevel(logging.DEBUG if logger_debug else logging.INFO)
  82. file_formatter = logging.Formatter(
  83. fmt='%(asctime)s.%(msecs)03d | %(filename)20s:%(lineno)04d | %(levelname)8s | %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
  84. console_formatter = logging.Formatter(
  85. fmt='%(asctime)s.%(msecs)03d │ %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
  86. flutter_formatter = logging.Formatter(
  87. fmt='| %(asctime)s.%(msecs)03d | %(message)08s', datefmt='%H:%M:%S')
  88. # ======================================================================================================================
  89. # Set console logger
  90. # ======================================================================================================================
  91. console_hdlr = RichHandler(
  92. console=Console(
  93. width=120
  94. ),
  95. show_path=False,
  96. show_time=False,
  97. rich_tracebacks=True,
  98. tracebacks_show_locals=True,
  99. tracebacks_extra_lines=3,
  100. tracebacks_width=160
  101. )
  102. console_hdlr.setFormatter(console_formatter)
  103. logger.addHandler(console_hdlr)
  104. # ======================================================================================================================
  105. # Set file
  106. # ======================================================================================================================
  107. class RichFileHandler(RichHandler):
  108. # Rename
  109. pass
  110. # Add file logger
  111. pyw_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
  112. def set_file_logger(name=pyw_name, *, do_cleanup=False):
  113. if '_' in name:
  114. name = name.split('_', 1)[0]
  115. log_file = f'./log/{date.today()}_{name}.txt'
  116. try:
  117. file = open(log_file, mode='a', encoding='utf-8')
  118. except FileNotFoundError:
  119. os.mkdir('./log')
  120. file = open(log_file, mode='a', encoding='utf-8')
  121. file_console = Console(
  122. file=file,
  123. no_color=True,
  124. highlight=False,
  125. width=160,
  126. )
  127. hdlr = RichFileHandler(
  128. console=file_console,
  129. show_path=False,
  130. show_time=False,
  131. show_level=False,
  132. rich_tracebacks=True,
  133. tracebacks_show_locals=True,
  134. tracebacks_extra_lines=3,
  135. tracebacks_width=160,
  136. highlighter=NullHighlighter(),
  137. )
  138. hdlr.setFormatter(file_formatter)
  139. logger.handlers = [h for h in logger.handlers if not isinstance(
  140. h, (logging.FileHandler, RichFileHandler))]
  141. logger.addHandler(hdlr)
  142. logger.log_file = log_file
  143. # ---------- 可选:清理旧文件 ----------
  144. if do_cleanup:
  145. cleanup_logs()
  146. logger.info("Log cleanup finished")
  147. # ======================================================================================================================
  148. # Set flutter
  149. # ======================================================================================================================
  150. class FlutterHandler(RichHandler):
  151. # Rename
  152. pass
  153. class FlutterConsole(Console):
  154. """
  155. Force full feature console
  156. but not working lol :(
  157. """
  158. @property
  159. def options(self) -> ConsoleOptions:
  160. return ConsoleOptions(
  161. max_height=self.size.height,
  162. size=self.size,
  163. legacy_windows=False,
  164. min_width=1,
  165. max_width=self.width,
  166. encoding='utf-8',
  167. is_terminal=False,
  168. )
  169. class FlutterLogStream(TextIOBase):
  170. def __init__(self, *args, func: Callable[[ConsoleRenderable], None] = None, **kwargs):
  171. super().__init__(*args, **kwargs)
  172. self._func = func
  173. def write(self, msg: str) -> int:
  174. if isinstance(msg, bytes):
  175. msg = msg.decode("utf-8")
  176. self._func(msg)
  177. return len(msg)
  178. def set_func_logger(func):
  179. stream = FlutterLogStream(func=func)
  180. stream_console = Console(
  181. file=stream,
  182. force_terminal=False,
  183. force_interactive=False,
  184. no_color=True,
  185. highlight=False,
  186. width=80,
  187. )
  188. hdlr = FlutterHandler(
  189. console=stream_console,
  190. show_path=False,
  191. show_time=False,
  192. show_level=True,
  193. rich_tracebacks=True,
  194. tracebacks_show_locals=True,
  195. tracebacks_extra_lines=3,
  196. highlighter=NullHighlighter(),
  197. )
  198. hdlr.setFormatter(flutter_formatter)
  199. logger.addHandler(hdlr)
  200. # ======================================================================================================================
  201. # Set print format
  202. # ======================================================================================================================
  203. def _get_renderables(
  204. self: Console, *objects, sep=" ", end="\n", justify=None, emoji=None, markup=None, highlight=None,
  205. ) -> List[ConsoleRenderable]:
  206. """
  207. Refer to rich.console.Console.print()
  208. """
  209. if not objects:
  210. objects = (NewLine(),)
  211. render_hooks = self._render_hooks[:]
  212. with self:
  213. renderables = self._collect_renderables(
  214. objects,
  215. sep,
  216. end,
  217. justify=justify,
  218. emoji=emoji,
  219. markup=markup,
  220. highlight=highlight,
  221. )
  222. for hook in render_hooks:
  223. renderables = hook.process_renderables(renderables)
  224. return renderables
  225. def print(*objects: ConsoleRenderable, **kwargs):
  226. for hdlr in logger.handlers:
  227. if isinstance(hdlr, FlutterHandler):
  228. for renderable in _get_renderables(hdlr.console, *objects, **kwargs):
  229. hdlr.console.file._func(str(renderable))
  230. elif isinstance(hdlr, RichHandler):
  231. hdlr.console.print(*objects)
  232. class GuiRule(Rule):
  233. def __rich_console__(
  234. self, console: Console, options: ConsoleOptions
  235. ) -> RenderResult:
  236. options.max_width = 80
  237. return super().__rich_console__(console, options)
  238. def __str__(self):
  239. total_width = 80
  240. cell_len = len(self.title) + 2
  241. aside_len = (total_width - cell_len) // 2
  242. left = self.characters * aside_len
  243. right = self.characters * (total_width - cell_len - aside_len)
  244. if self.title:
  245. space = ' '
  246. else:
  247. space = self.characters
  248. return f"{left}{space}{self.title}{space}{right}\n"
  249. def __repr__(self):
  250. return self.__str__()
  251. def rule(title="", *, characters="─", style="rule.line", end="\n", align="center"):
  252. rule = GuiRule(title=title, characters=characters,
  253. style=style, end=end)
  254. print(rule)
  255. def hr(title, level=3):
  256. title = str(title).upper()
  257. if level == 1:
  258. logger.rule(title, characters='═')
  259. logger.info(title)
  260. if level == 2:
  261. logger.rule(title, characters='─')
  262. logger.info(title)
  263. if level == 3:
  264. logger.info(f"[bold]<<< {title} >>>[/bold]", extra={"markup": True})
  265. if level == 0:
  266. logger.rule(characters='═')
  267. logger.rule(title, characters='─')
  268. logger.rule(characters='═')
  269. def attr(name, text):
  270. logger.info('[%s] %s' % (str(name), str(text)))
  271. def attr_align(name, text, front='', align=22):
  272. name = str(name).rjust(align)
  273. if front:
  274. name = front + name[len(front):]
  275. logger.info('%s: %s' % (name, str(text)))
  276. def show():
  277. logger.info('INFO')
  278. logger.warning('WARNING')
  279. logger.debug('DEBUG')
  280. logger.error('ERROR')
  281. logger.critical('CRITICAL')
  282. logger.hr('hr0', 0)
  283. logger.hr('hr1', 1)
  284. logger.hr('hr2', 2)
  285. logger.hr('hr3', 3)
  286. logger.info(r'Brace { [ ( ) ] }')
  287. logger.info(r'True, False, None')
  288. logger.info(r'E:/path\\to/alas/alas.exe, /root/alas/, ./relative/path/log.txt')
  289. logger.info('Tests very long strings. Tests very long strings. Tests very long strings. Tests very long strings. Tests very long strings.')
  290. local_var1 = 'This is local variable'
  291. # Line before exception
  292. raise Exception("Exception")
  293. # Line below exception
  294. def error_convert(func):
  295. def error_wrapper(msg, *args, **kwargs):
  296. if isinstance(msg, Exception):
  297. msg = f'{type(msg).__name__}: {msg}'
  298. return func(msg, *args, **kwargs)
  299. return error_wrapper
  300. logger.error = error_convert(logger.error)
  301. logger.hr = hr
  302. logger.attr = attr
  303. logger.attr_align = attr_align
  304. logger.set_file_logger = set_file_logger
  305. logger.set_func_logger = set_func_logger
  306. logger.rule = rule
  307. logger.print = print
  308. logger.log_file: str
  309. logger.set_file_logger()
  310. logger.hr('Start', level=0)