Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 格式错误的日志消息;日志记录已写入日志_Python_Logging_Format - Fatal编程技术网

Python 格式错误的日志消息;日志记录已写入日志

Python 格式错误的日志消息;日志记录已写入日志,python,logging,format,Python,Logging,Format,我尝试在我的应用程序中使用日志模块;阅读文件后,我编写了完整的代码: class mainwin(): def start_logging(self , level): logger = logging.getLogger('Rockdome:') FORMAT = "***\n\n%s%(name)s [%(levelname)s]\nmodule: %(module)s\nMessage:%(message)s\n***" loggi

我尝试在我的应用程序中使用日志模块;阅读文件后,我编写了完整的代码:

class mainwin():
    def start_logging(self , level):

        logger = logging.getLogger('Rockdome:')
        FORMAT = "***\n\n%s%(name)s [%(levelname)s]\nmodule: %(module)s\nMessage:%(message)s\n***"
        logging.basicConfig(format=FORMAT, filename= "example.log" , filemode="w")
        logger.setLevel(level)
        self.logger = logger

   def __init__(self ):
       self.start_logging(logging.INFO)        
       self.pid = os.getpid()
       with open('%s/.aria2/pid'%self.home , 'w') as p:
           p.write(str(self.pid))
我得到以下信息:

***{'threadName':'MainThread','name':'Rockdome:','thread':-1221756160,'created':1412549164.939926,'process':7195,'processName':'mainproces','args':(),'module':'mainwin','filename':'mainwin.py','levelno':20,'exc_text':None,'pathname':'mainwin.py','lineno':184,'msg':'Application started PID=7195','exc_info':None,'message':'Application started PID=7195','funcName':'relativeCreated':178.4038543701172,'levelname':'info','msecs':939.9259090423584}Rockdome:[信息]
模块:mainwin
消息:应用程序已启动PID=7195
***
{}
之间的文本是logrecord对象本身

为什么logrecord对象已写入日志文件

问题是:如何获得这个输出

***
Rockdome:[信息]
模块:mainwin
消息:应用程序已启动PID=7195
***

在格式字符串的开头有一个额外的
\n%s
。删除它们:

FORMAT = "***\n%(name)s [%(levelname)s]\nmodule: %(module)s\nMessage:%(message)s\n***"
一旦你这样做了,你就会得到正确的格式。特别是额外的
%s
使得所有可能的
记录器属性打印出来,这就是为什么你的消息看起来与你想要的相去甚远。你可以通过一个小测试更好地了解到底发生了什么:

>>> d = {"test" : "abcdefg", "another" : "asdfasdf"}
>>> print "%(test)s" % d # What you wanted to do
abcdefg
>>> print "%s" % d # What you ended up doing
{'test': 'abcdefg', 'another': 'asdfasdf'}