我陷入了一个无法解决的令人惊讶的问题。
我们正在谈论 Python 3.6.4 和 tkinter 8.6 以及内置的 logging 库。
# file: logger.py
import logging.handlers
GENERIC_FORMAT = "%(asctime)s :: %(levelname)-8s :: '%(name)s' :: %(module)s.%(funcName)s :: proc=%(process)d :: %(message)s (%(filename)s:%(lineno)d)"
class MyLogger(logging.Logger):
""" Main logger """
def __init__(self, level=logging.DEBUG, filehandler=None, consolemode=True):
self.fmt = logging.Formatter(GENERIC_FORMAT)
logging.Logger.__init__(self, name='my_logger', level=level)
if filehandler is not None:
self.setup_filehandler(filehandler)
if consolemode:
console_handler = logging.StreamHandler()
console_handler.setFormatter(self.fmt)
self.addHandler(console_handler)
def setup_filehandler(self, filehandler):
file_handler = logging.handlers.RotatingFileHandler(
filename=filehandler, mode='a', maxBytes=1e+7, backupCount=10)
file_handler.setFormatter(self.fmt)
self.addHandler(file_handler)
Nota::实例化时,它将创建一个名为“ my_logger”的记录器。
# file: main_application.py
from tkinter import Tk
import logging
import os
from logger import MyLogger
from myframe import MyFrame
# Logger main instance
logger = MyLogger(level=logging.DEBUG)
def MyFrameRunner():
root = Tk()
root.geometry("450x450+400+150")
MyFrame(root)
root.mainloop()
def main():
# Set the filehandler for the main L6 logger
logfile = os.path.join(os.getcwd(), "MYTOOL.log")
if not os.path.exists(os.path.dirname(logfile)):
os.makedirs(os.path.dirname(logfile))
logger.setup_filehandler(filehandler=logfile)
# It logs successfully into sdterr and $SW_PATH/MYTOOL.log file !!
logger.info("Let's go !!")
MyFrameRunner()
if __name__ == '__main__':
main()
注意:,如上所述,main / main_application.py中的logger.info成功登录到sdterr(StreamHandler)和$ SW_PATH / MYTOOL.log文件(RotatingFileHandler)!
# file : myframe.py
from tkinter import *
import logging
# With the same name as defined in MyLogger class
logger = logging.getLogger('my_logger')
class MyFrame(Frame):
def __init__(self, master, **kw):
logger.info("Here is my frame !!")
Frame.__init__(self, master)
使用上面的代码,不可能用logging.getLogger('my_logger')将名为“ my_loger”的记录器捕获到myframe.py中。
没有错误 ,但是没有没有“日志” ,也没有 sdterr (StreamHandler)或 $ SW_PATH / MYTOOL .log 文件(RotatingFileHandler)。
问题:在Tk()主循环中,没有办法“通过记录器”(实际上是赶上记录器),这是“正常的”吗?
我获得的唯一“日志输出”是:
2018-10-29 17:03:32,871 :: INFO :: 'my_logger' :: main_application.main :: proc=21484 :: Let's go !! (main_application.py:31)
我除外:
2018-10-29 17:03:32,871 :: INFO :: 'my_logger' :: main_application.main :: proc=21484 :: Let's go !! (main_application.py:31)
2018-10-29 17:03:xx,xxx :: INFO :: 'my_logger' :: myframe.MyFrame :: proc=21484 :: Here is my frame !! (myframe.py:15)
非常感谢!
答案 0 :(得分:0)
可以通过将“ logger”作为MyFrame参数之一来实现:
在 main_application.py 中编辑:
# file: main_application.py
from tkinter import Tk
import logging
import os
from logger import MyLogger
from myframe import MyFrame
# Logger main instance
logger = MyLogger(level=logging.DEBUG)
def MyFrameRunner():
root = Tk()
root.geometry("450x450+400+150")
# Old: MyFrame(root)
MyFrame(root, logger)
root.mainloop()
def main():
# Set the filehandler for the main L6 logger
logfile = os.path.join(os.getcwd(), "MYTOOL.log")
if not os.path.exists(os.path.dirname(logfile)):
os.makedirs(os.path.dirname(logfile))
logger.setup_filehandler(filehandler=logfile)
# It logs successfully into sdterr and $SW_PATH/MYTOOL.log file !!
logger.info("Let's go !!")
MyFrameRunner()
在 myframe.py 中编辑:
# file : myframe.py
from tkinter import *
import logging
from logger import MyLogger
# With the same name as defined in MyLogger class
logger = logging.getLogger('my_logger')
class MyFrame(Frame):
# Old: def __init__(self, master, **kw):
def __init__(self, master, logger=MyLogger(), **kw):
logger.info("Here is my frame !!")
Frame.__init__(self, master)