Python中文
首页
教程
问答
标签
搜索
登录
注册
Python中嵌套前缀accross loggers
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我目前正在一个项目中使用一个单一的根记录程序。我从阅读日志中了解到这是一件坏事,但我正在努力寻找一个好的解决方案来实现它给我们带来的好处。在</p> <p>我们所做的事情(部分是为了避免使用不同的记录器,但在一定程度上给了我们一个很好的特性)就是使用一个<code>log_prefix</code>装饰器。在</p> <p>例如</p> <pre><code>@log_prefix("Download") def download_file(): logging.info("Downloading file..") connection = get_connection("127.0.0.1") //Do other stuff return file @log_prefix("GetConnection") def get_connection(url): logging.info("Making connection") //Do other stuff logging.info("Finished making connection") return connection </code></pre> <p>这为我们提供了一些很好的格式化日志,如下所示:</p> ^{pr2}$ <p>这也意味着如果我们</p> <pre><code>@log_prefix("StartTelnetSession") logging.info("Starting telnet session..") connection = get_connection("127.0.0.1") </code></pre> <p>我们在末尾得到相同的前缀:</p> <pre><code>StartTelnetSession:Starting telnet session.. StartTelnetSession:GetConnection:Making Connection StartTelnetSession:GetConnection:Other stuff StartTelnetSession:GetConnection:Finished making connection </code></pre> <p>事实证明,这对开发和支持非常有用。在</p> <p>我可以看到很多情况下,实际上只使用一个单独的记录器来执行操作可以解决我们的问题,但我也可以看到一些情况,即放弃我们的嵌套将使事情变得更糟。在</p> <p>有没有什么模式或常见的用途来嵌套伐木工人?i、 e</p> <pre><code>logging.getLogger("Download").getLogger("MakingConnection") </code></pre> <p>还是我在这里遗漏了什么?在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>日志名称是分层的。在</p> <pre><code>logger = logging.getLogger("Download.MakingConnection") </code></pre> <p>此记录器将从<code>logging.getLogger("Download")</code>继承任何配置</p> <p>Python2.7还添加了一个方便的函数,用于访问任意记录器的后代。在</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
文本导入时标题行中的特殊字符
10 回答
文本小部件:在没有输入时更新并在循环后保持空闲
6 回答
文本小部件tkin
7 回答
文本小部件tkinter中的标签更改或文本外观更改是否有撤消功能?
2 回答
文本小部件tkinter复制图像选项
1 回答
文本小部件上的Python Tkinter ttk滚动条未缩放
3 回答
文本小部件上的滚动条可能需要根据制表符ord显示前进行滚动
2 回答
文本小部件不显示lis中的内容
6 回答
文本小部件不显示Unicode字符
9 回答
文本小部件中写入的行间距
2 回答
文本小部件中的文本作为变量
9 回答
文本小部件中的滚动条仅显示在底部
9 回答
文本小部件中的选项卡键空间计数
10 回答
文本小部件作为Lis
5 回答
文本小部件在主框架中扩展列宽
8 回答
文本小部件未使用删除功能清除
4 回答
文本小部件滚动动画(Tkinter、Python)
9 回答
文本居中。格式正确吗?
7 回答
文本差分算法
9 回答
文本已知时音频文件中的单词索引
9 回答