Python日志记录模块启动KeyErrors?

时间:2015-11-13 17:45:50

标签: python logging python-2.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'                                                                                                     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()

作为旁注,在我正在做的链中从另一个函数调用一个函数是不好的做法吗?我应该将此作为单独的流程吗?我打算创建一个处理它的类,但后来我认为没有真正的好处。

2 个答案:

答案 0 :(得分:1)

level不是标准LogRecord属性之一,因此当您以日志记录格式使用它时:

logging.basicConfig(level=logging.INFO, format='[%(level)s] - %(message)s')

格式化程序无法找到level并抛出异常。也许你的意思是levelnamelevelno

答案 1 :(得分:1)

您的格式字符串<div class="container"> <div class="row"> <div class="col-lg-3 col-md-6">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </div> <div class="col-lg-3 col-md-6">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> <div class="col-lg-3 col-md-6">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </div> <div class="col-lg-3 col-md-6">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </div> </div> 必须是:  format='[%(level)s] - %(message)s'

相关问题