python-3.x - How to increment integer variable within logging statement?
问题描述
I am using python 3.6 logging module and trying to print a counter which should increment with every logging statement. I have in working now as shown in code below.
Code
1 #!/something/bin/python
2
3 import logging
4
5 logger = logging.getLogger(__name__)
6 l_level = 'INFO'
7 l_level = eval("logging." + l_level.upper())
8 logger.setLevel(l_level)
9
10 handler = logging.StreamHandler()
11 handler.setLevel(l_level)
12
13 formatter = logging.Formatter('%(message)s')
14 handler.setFormatter(formatter)
15 logger.addHandler(handler)
16
17 i = 0
18 i += 1; logger.info("{} Started".format(i))
19 #logger.info("{} Started".format(i += 1)) # What I am trying to do
20
So it works as I want if I increment i
every time before I use logging statement. (line 18)
Question:
Is there any way I can increment i
within logging statement itself?
Tried:
I tried using i += 1
and i = i + 1
(line 19) but it doesn't work that way. Also checked documentation for logging module but couldn't find anything that I can use here.
Error I get when I tried using i += 1
and i = i + 1
:
File "./test_2.py", line 19
i += 1; logger.info("{} Started".format(i += 1))
^
File "./test_2.py", line 19, in <module>
i += 1; logger.info("{} Started".format(i = i + 1))
So now trying to find if there is any other way (specific to logging module or any other hack) to do this. Any help or suggestions are welcome. Thank you in for your time reading this.
解决方案
推荐阅读
- c - 套接字之间基于事件的通信
- django - Django 查询集和联合
- javascript - 使用 Ag-Grid-Angular 表在 Array 中显示工具提示值
- html - Webdrivers::BrowserNotFound: 找不到 Chrome 二进制文件
- r - 将汇总字段添加到数据框 R
- python - 添加对 Python 程序的可访问性,尤其是屏幕阅读器功能
- javascript - VueJS 在更新前显示旧数据(图像)
- reactjs - slik 滑块自定义按钮功能在 react.js 中不可编辑
- kotlin - 我应该如何在 Kotlin 多平台移动设备 (KMM) 中验证电子邮件地址?
- python - Python如何用空格替换\ xa0