Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] 可配置的 logger #73

Closed
hikariyo opened this issue Jan 9, 2023 · 17 comments
Closed

[Feature Request] 可配置的 logger #73

hikariyo opened this issue Jan 9, 2023 · 17 comments
Labels
enhancement New feature or request

Comments

@hikariyo
Copy link
Contributor

hikariyo commented Jan 9, 2023

我需要什么功能

由于原有 loggerformat 是硬编码的,我觉得可以在 aiotieba.toml 中新增一个配置项 Logger 来配置日志的 formatbackup count 等,或许还可以加上颜色。

@hikariyo hikariyo changed the title 可配置的 logger [Feature Request] 可配置的 logger Jan 9, 2023
@n0099
Copy link

n0099 commented Jan 9, 2023

建议sed+lnav

@hikariyo
Copy link
Contributor Author

hikariyo commented Jan 9, 2023

建议sed+lnav

你说的是应该日志查看吧,我说的是更改日志的配置项。

@lumina37
Copy link
Owner

lumina37 commented Jan 9, 2023

标准库logger还有颜色的说法?这个logger确实很老了,一年多没动过。我会考虑的,不过至少要一周时间,因为更紧迫的任务太多了

@hikariyo
Copy link
Contributor Author

hikariyo commented Jan 9, 2023

主要是我看 uvicorn 的 logger 也是标准库的,而且有颜色

@lumina37
Copy link
Owner

lumina37 commented Jan 9, 2023

logging.py#L27
他们用了click
如果要颜色的话就应该做可插拔设计吧,允许自己写一个logger然后换上去

@n0099
Copy link

n0099 commented Jan 9, 2023

建议sed+lnav

你说的是应该日志查看吧,我说的是更改日志的配置项。

只有cli/gui里查看log文件时才可能进行文本着色,您不可能让纯文本log文件里带有颜色(除非您把ansi escape code直接写进文件,然后又直接cat出来)
linux人tty环境着色基于 https://en.wikipedia.org/wiki/ANSI_escape_code
win人conhost.exe环境着色基于dos遗留的api,经典windows 16色

@hikariyo
Copy link
Contributor Author

hikariyo commented Jan 9, 2023

只有cli/gui里查看log文件时才可能进行文本着色,您不可能让纯文本log文件里带有颜色(除非您把ansi escape code直接写进文件,然后又直接cat出来) linux人tty环境着色基于 https://en.wikipedia.org/wiki/ANSI_escape_code win人conhost.exe环境着色基于dos遗留的api,经典windows 16色

就算无法着色,我也想有一个配置 format 的功能。

@n0099
Copy link

n0099 commented Jan 9, 2023

不是无法着色
而是着色不是在写入log文件这个流程上进行的(除非您真的想让log文件里一大堆ansi escape code)
而是在logger将log输出到stdout时进行的,logger应该判断当前系统是win还是linux,从而选择 操作conhost.exe的api(win) 还是 在即将stdout的文本中嵌入ascii escape code(linux) 来达到着色的目的(实际上还需要判断在pipe中消费stdout的下一个stdin到底是不是tty/conhost,如果不是有效terminal(tty/conhost同属terminal emulator)而是其他程序在接收stdin那反而应该跳过任何着色尝试,因为您不会希望pipe他们时看到第二个进程收到了一大堆ansi escape code)

@n0099
Copy link

n0099 commented Jan 9, 2023

配置log format这个需求在starry神这一波发威( #71 (comment) )结束从而落地实现他的可配置性之前您可以直接对着log文件sed/awk来转换他的结构,建议多写正则 https://regex101.com (贴吧管理器( https://github.com/dog194/TiebaManager )用户在贴吧辅助工具皇帝鸡血神的指导下人均正则带师)
backup count如果阁下指的是logrotate那也可以使用外部程序来完成,如 https://linux.die.net/man/8/logrotate https://github.com/logrotate/logrotate

@hikariyo
Copy link
Contributor Author

hikariyo commented Jan 9, 2023

我觉得我这个需求其实也挺简单的,或许我可以抽时间提一个 PR

@n0099
Copy link

n0099 commented Jan 9, 2023

经典pr is welcome

@lumina37
Copy link
Owner

lumina37 commented Jan 9, 2023

我当时第一想法是用官方的dict_config,不过这个明显很难debug,https://docs.python.org/zh-cn/3/library/logging.config.html#module-logging.config
我明天起床了再看看uvicorn和httpx的日志是怎么写的

@n0099
Copy link

n0099 commented Jan 9, 2023

建议直接引入库: https://github.com/Delgan/loguru
他也支持自动检测stdout对着什么以(不)进行正确的着色: https://loguru.readthedocs.io/en/stable/api/logger.html#:~:text=of%20a%20deadlock.-,The%20color%20markups,-To%20add%20colors

@lumina37 lumina37 added the enhancement New feature or request label Jan 10, 2023
@lumina37
Copy link
Owner

lumina37 commented Jan 11, 2023

看看最新的develop分支能否解决问题
52f2663
现在aiotieba不会在初始化阶段输出日志或创建日志文件夹,你可以在uvicorn和aiotieba都初始化完成后使用aiotieba.LOG.set_formatter把uvicorn的多彩formatter换上去。如果需要修改backup_count就自己创建一个LOG.TiebaLogger(backup_count=15)然后用LOG.set_logger把这个logger换上去
额外开销就是每次aiotieba打印内部日志都会多出一次null判断

@hikariyo
Copy link
Contributor Author

hikariyo commented Jan 11, 2023

已解决,并且我发现一个库 colorlog 可以提供一个带颜色的 Formatter

https://github.com/borntyping/python-colorlog

@hikariyo
Copy link
Contributor Author

呃,发现一个小问题
https://github.com/Starry-OvO/aiotieba/blob/20a286770b27b691b1a459934da5baa8c8b01e5c/aiotieba/_logging.py#L36

这行应该是 Defaults to 5

@n0099
Copy link

n0099 commented Jan 16, 2023

呃,发现一个小问题

https://github.com/Starry-OvO/aiotieba/blob/20a286770b27b691b1a459934da5baa8c8b01e5c/aiotieba/_logging.py#L36

这行应该是 Defaults to 5

fixed in c57ef59

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants