Python日志记录模块启动keyrerrors?
我在不同的函数中使用日志模块,它启动了一个错误堆栈,如下所示:Python日志记录模块启动keyrerrors?,python,logging,python-2.x,Python,Logging,Python 2.x,我在不同的函数中使用日志模块,它启动了一个错误堆栈,如下所示: KeyError: 'level' x Traceback (most recent call last):
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level'
我在努力想原因。这是我的密码:
from csv import reader, writer
import logging
from sys import argv
from types import IntType, FloatType
def main():
logging.basicConfig(level=logging.INFO, format='[%(level)s] - %(message)s')
# logging.disable(logging.CRITICAL)
# Files
infile = argv[1]
header_file = argv[2]
transact_file = argv[3]
start_process(infile)
def start_process(infile):
"""Create a csv reader and parse it into two lists."""
with open(infile, 'r') as inf:
logging.info('Infile name: {0}'.format(inf))
csv_reader = reader(inf, quotechar='"')
parse_headers(csv_reader)
def parse_headers(reader_obj):
"""Separate header files ("H", "S") from transaction files."""
headers = []
transactions = []
for row in reader_obj:
row_type = row[0]
logging.info('Row type is: {0}'.format(row_type))
if row_type == 'H':
logging.info('Row added to header list.')
headers.append(row)
if row_type == 'S':
if row not in headers:
headers.append(row)
else:
logging.info('Row added to transaction list.')
transactions.append(row)
logging.info('Header list contains: {0}'.format('\n'.join([str(header) for header
in headers])))
logging.info('Transaction list contains: {0}'.format(
'\n'.join([str(trans) for trans in transactions])))
recon_totals(headers, transactions)
def recon_totals(header_list, transact_list):
"""Reconcile the check total amount and document count."""
# Client totals
client_doc_count = int(header_list[0][6])
client_check_tot = float(header_list[0][7])
# Double check variable typing for reconciliation totals.
logging.info('Document count is: {0}'.format(client_doc_count))
doc_var_type = type(client_doc_count)
assert doc_var_type is IntType, 'Doc Count is not an integer: {0}'.format(
doc_var_type)
logging.info('Check Total is: {0}'.format(client_check_tot))
check_var_type = type(client_check_tot)
assert check_var_type is FloatType, 'Check tot is not a float: {0}'.format(
check_var_type)
# RRD totals
rrd_doc_count = 0
rrd_check_tot = 0.0
for transact in transact_list:
row_type = transact[0]
logging.info('Transaction type is: {0}'.format(row_type))
if row_type == 'P':
rrd_doc_count += 1
trans_chk_amt = float(transact[12])
trans_chk_type = type(trans_chk_amt)
assert trans_chk_type is FloatType, 'Transaction Check Total is '\
'not a float: {0}'.format(
trans_chk_type)
rrd_check_tot += trans_chk_amt
# Reconcile totals
if (client_doc_count, client_check_tot) == (rrd_doc_count, rrd_check_tot):
write_files()
else:
raise ValueError('Recon totals do not match! Client: {0} {1} '
'RRD {2} {3}'.format(client_doc_count,
client_check_tot,
rrd_doc_count,
rrd_check_tot))
def write_files(header_file, transact_file, header_data, transact_data):
pass
if __name__ == '__main__':
main()
顺便提一下,像我这样在一个链中从一个函数调用另一个函数是不是一种不好的做法?我是否应该将其作为单独的过程来执行?我本来打算创建一个类来处理这一切,但后来我决定这并没有真正的好处。
level
不是标准的LogRecord属性之一,所以在日志格式中使用它时:
logging.basicConfig(level=logging.INFO, format='[%(level)s] - %(message)s')
格式化程序找不到
级别
,并引发异常。也许您的意思是levelname
或levelno
?您的格式字符串format='[%(level)s]-%(message)s'
需要:
format='%(levelname)s-%(message)s'
是的,对我来说总的derp时刻:/