When I use the PIL.ImageTk library to load a png to my GUI and use logging to log some events, it creates some unwanted logs in DEBUG mode.
I have tried changing the level
of logging
to INFO
or WARNING
(or higher). But that does not help:
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)
For example, the following code will create a log file with some unwanted lines:
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()
Here is a sample image test.png
This will create a log file with some unwanted lines like this:
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...
It should have been only:
07/23/2019 01:34:23 PM INFO: This is a test log...
If you remove the image from the GUI, the problem goes away. Is there any workaround for this?
EDIT: I apologize for not going through the docs carefully. This was happening because the root module was created with DEBUG level when I first ran the script in Spyder with level=DEBUG
and it was never changed by basicConfig
subsequently when I changed the level to INFO. If I reload all the modules and libs (only by restarting the kernel in Spyder), the problem goes away, which means level=INFO
would work perfectly as I want.