Python tkinter ImageTk在日志文件中创建不需要的日志
当我使用PIL.ImageTk库将png加载到GUI并使用日志记录一些事件时,它会在调试模式下创建一些不需要的日志 我已尝试将Python tkinter ImageTk在日志文件中创建不需要的日志,python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,当我使用PIL.ImageTk库将png加载到GUI并使用日志记录一些事件时,它会在调试模式下创建一些不需要的日志 我已尝试将日志记录的级别更改为信息或警告(或更高级别)。但这无助于: logging.basicConfig(filename='mylog.log', filemode='a', format='%(asctime)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
日志记录的级别更改为信息
或警告
(或更高级别)。但这无助于:
logging.basicConfig(filename='mylog.log', filemode='a', format='%(asctime)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
例如,以下代码将创建包含一些不需要的行的日志文件:
from PIL import ImageTk, Image
import logging
try:
import tkinter as tk # Python 3.x
except ImportError:
import Tkinter as tk # Python 2.x
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
for i in range(2):
self.grid_rowconfigure(i, weight=1)
self.grid_columnconfigure(0, weight=1)
self.img = ImageTk.PhotoImage(Image.open('test.png'))
logo = tk.Label(self, image = self.img)
logo.grid(row=0, column=0, columnspan=2, sticky="nw", pady=5, padx=10)
testLabel = tk.Label(self, width=8, text="This is a test")
testLabel.grid(row=1, column=0, sticky='ew', padx=5, pady=5)
logging.info("This is a test log...")
if __name__ == "__main__":
logging.basicConfig(filename='mylog.log', filemode='a', format='%(asctime)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
下面是一个示例图像test.png
这将创建一个日志文件,其中包含一些不需要的行,如下所示:
07/23/2019 01:34:23 PM DEBUG: STREAM b'IHDR' 16 13
07/23/2019 01:34:23 PM DEBUG: STREAM b'IDAT' 41 6744
07/23/2019 01:34:23 PM INFO: This is a test log...
它应该只是:
07/23/2019 01:34:23 PM INFO: This is a test log...
如果从GUI中删除图像,问题就会消失。有什么解决办法吗
编辑:我很抱歉没有仔细阅读。发生这种情况的原因是,当我第一次在Spyder中使用level=DEBUG
运行脚本时,根模块是使用DEBUG level创建的,当我随后将level更改为INFO时,它从未被basicConfig
更改。如果我重新加载所有模块和lib(仅通过在Spyder中重新启动内核),问题就会消失,这意味着level=INFO
将按照我的要求完美工作。问题是因为模块已经创建了根记录器,现在basicConfig
使用此记录器,但它无法更改现有记录器的级别
文件:
如果根记录器已经为其配置了处理程序,则此函数不执行任何操作
您必须创建自己的记录器(您可以使用\uuuu name\uuuu
使其唯一),然后您可以为文件和控制台处理程序设置根级别和级别。在自己的记录器中,您不会看到来自其他记录器的警告
if __name__ == "__main__":
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # root level
# console
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) # level only for console (if not collide with root level)
logger.addHandler(ch)
# file
fh = logging.FileHandler('mylog.log')
fh.setLevel(logging.DEBUG) # level only for file (if not collide with root level)
logger.addHandler(fh)
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
文档:,而不是使用:
...
level=logging.DEBUG
...
使用:
您的文件将是:
DD/MM/YYYY HH:MM:SS PM信息:这是一个测试日志
加:
(来源:)你的问题有点不清楚。您是说即使将日志级别设置为INFO,日志也包含调试消息吗?如果我没有弄错,那么当您使用basicConfig
时,它不会更改level
。可能是因为模块已经设置了级别,现在无法更改它:basicConfig
。也许如果你创建了一个新的日志,那么你只会在日志中看到你的消息。我为没有仔细阅读文档而道歉@BryanOakley:是的,即使我将日志级别设置为INFO,也会收到调试消息。原因是当我第一次运行脚本时,根模块是用调试级别创建的,当我将级别更改为INFO时,basicConfig
从未更改过根模块。如果我重新加载所有模块和库(仅通过在Spyder中重新启动内核),问题就会消失@谢谢,你救了我一天!
...
level=logging.INFO
...
pil_logger = logging.getLogger('PIL')
pil_logger.setLevel(logging.INFO)