fastnlp / fastNLP

fastNLP: A Modularized and Extensible NLP Framework. Currently still in incubation.
https://gitee.com/fastnlp/fastNLP
Apache License 2.0
3.05k stars 451 forks source link

允许 logger 使用 stderr, 使得 stdout 不被 fastNLP 框架污染 #337

Closed LouChao98 closed 3 years ago

LouChao98 commented 3 years ago

Is your feature request related to a problem? Please describe. 问题是什么 A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

fastNLP 的 logger 在 stdout='plain' 时默认使用 stdout 作为 stream, 希望增加一个选项将 stderr 作为 stream, 或者直接将 stderr 作为默认的 stream。这样在 redirect stdout 到文件的时候,不会有 fastNLP 的 logger 输出的信息。

案例: tqdm 缺省状态下使用 stderr, github logging 模块缺省状态下使用 stderr

Describe the solution you'd like 解决方案是什么 A clear and concise description of what you want to happen.

如果设置stderr为默认, 将line 59 line 111,直接改为 sys.stderr。

如果增加一个选项,因为文档没有提及,我不知道是否有一个方法允许用户配置logger。

Describe alternatives you've considered 其他解决方案 A clear and concise description of any alternative solutions or features you've considered.

目前解决方案

fastNLP.core._logger.logger.handlers = []
fastNLP.core._logger.logger.addHandler(logging.StreamHandler(sys.stderr))

存在的问题:我不清楚fastNLP内是否会添加其他 Handler,可能会误删 FileHandler,

Additional context 备注 Add any other context or screenshots about the feature request here.

choosewhatulike commented 3 years ago

感谢你的问题,目前你可以先使用这个方案解决问题:

目前解决方案

fastNLP.core._logger.logger.handlers = [] fastNLP.core._logger.logger.addHandler(logging.StreamHandler(sys.stderr))

fastNLP不会在logger初始化后添加别的Handler,你可以自定义想要的Handler 后续我们会尝试更新一个给用户配置logger的选项。

LouChao98 commented 3 years ago

Thanks