简体   繁体   English

Python 日志记录:为 TODO 添加自定义级别?

[英]Python Logging: Add custom level for TODO?

While developing an application using PyQT5 I'm still trying to find a workflow that works best for me.在使用 PyQT5 开发应用程序时,我仍在努力寻找最适合我的工作流程。 At the moment, I'm mostly working on the gui (to demo to client), leaving the back-end code for later, so I'm connecting the button signals to empty functions that I need to write later.目前,我主要在 gui 上工作(演示给客户端),将后端代码留给以后,所以我将按钮信号连接到稍后需要编写的空函数。 Up until now, I've added print('#TODO: xxx') in each function that is empty.到目前为止,我已经在每个为空的函数中添加了print('#TODO: xxx') This gives me some feedback in the terminal as to how often certain functions are called and which to prioritize.这给了我一些关于某些函数被调用的频率和优先级的反馈。 This sort-of works for me.这种对我有用。

At the same time I am using the logging module as it is intended to be used.同时,我正在使用预期使用的logging模块。 It seems like I could add a new logging level and use the logging module for this purpose as well, something like this:似乎我可以添加一个新的日志记录级别并为此目的使用日志记录模块,如下所示:

def print_badge(self, pers: Person):
    # TODO: print_badge
    # print('#TODO: print badge') #<- not needed anymore
    self.log.TODO('print badge')

The logging documentation seems to discourage creating own logging levels though.日志记录文档似乎不鼓励创建自己的日志记录级别。 Is there a reason why I shouldn't do this or a better option?我是否有理由不这样做更好的选择?

The reason why custom logging levels are discouraged in general is that people who configure logging have to take these levels into account - if multiple libraries did this with multiple levels, one might have to know all of the custom levels used by all of the libraries in use in order to configure logging a particular way.通常不鼓励自定义日志记录级别的原因是配置日志记录的人必须考虑这些级别 - 如果多个库使用多个级别执行此操作,则可能必须知道所有库使用的所有自定义级别用于以特定方式配置日志记录。 Of course the logging package allows you to do it for those cases where it might be really necessary - perhaps for an application and libraries which are self-contained and not public, so the question of configuring those libraries by someone else doesn't arise.当然, logging包允许您在可能真正需要的情况下执行此操作 - 也许对于自包含且不公开的应用程序和库,因此不会出现由其他人配置这些库的问题。

It seems that one could easily use the DEBUG level for your use case - just include TODO in the message itself, and one can grep logs for that just as easily as a custom level.似乎可以很容易地为您的用例使用DEBUG级别 - 只需在消息本身中包含TODO ,并且可以像自定义级别一样轻松地 grep 日志。 Or you can have a logger called TODO to which you log these messages, and handle those in a separate handler/destination (eg a file todo.log ).或者,您可以使用一个名为TODO的记录器来记录这些消息,并在单独的处理程序/目标(例如文件todo.log )中处理这些消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM