我有一个使用模块“logging”的Python应用程序,我用pysintaller将其打包。
我正在使用以下格式字符串:
%(asctime)s %(levelname)s [%(threadName)s:%(module)s.%(funcName)s()] %(message)s
如果我将应用程序作为 python 脚本运行,它工作正常,'%(module)s.%(funcName)s' 被调用模块和函数的实际值替换:
2014-06-19 18:46:10,373 DEBUG [MainThread:subcommands.exec_cmd()] Executing `service iptables stop` on server centos6root
2014-06-19 18:46:10,373 DEBUG [MainThread:ssh._connect()] Trying to connect to server centos6root
2014-06-19 18:46:10,945 DEBUG [MainThread:ssh._connect()] Established connection with root@192.168.122.57:22
2014-06-19 18:46:11,533 DEBUG [MainThread:subcommands.exec_cmd()] exitstatus = 0
2014-06-19 18:46:11,648 DEBUG [MainThread:ssh.cleanup()] Closed connection to server centos6root
2014-06-19 18:46:11,649 DEBUG [MainThread:hwswa2.main()] Application finished
但是,如果我使用 pyinstaller 打包应用程序,它现在会用“logging.debug”替换“%(module)s.%(funcName)s”:
2014-06-19 19:04:05,577 DEBUG [MainThread:logging.debug()] Executing `echo hello` on server centos6root
2014-06-19 19:04:05,577 DEBUG [MainThread:logging.debug()] Trying to connect to server centos6root
2014-06-19 19:04:06,293 DEBUG [MainThread:logging.debug()] Established connection with root@192.168.122.57:22
2014-06-19 19:04:06,705 DEBUG [MainThread:logging.debug()] exitstatus = 0
2014-06-19 19:04:06,707 DEBUG [MainThread:logging.debug()] Closed connection to server centos6root
2014-06-19 19:04:06,707 DEBUG [MainThread:logging.debug()] Application finished
原因是什么以及如何解决此问题?
最佳答案
我使用直接调用 logging.info()
、logging.debug()
等。
通过为每个模块使用单独的记录器解决了该问题
import logging
logger = logging.getLogger(__name__)
并调用logger.info()
、logger.debug()
等。
关于python - pyinstaller 应用程序的日志记录工作错误 : calling module and function are set to 'logging' and 'debug' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24306246/