打印日志时出现Python数组问题

时间:2018-07-02 23:47:25

标签: python arrays

有人可以帮我吗? 同时将数组的值保存到变量中以将其存储在日志中。数组的值被保存为索引的次数,例如,如果索引为1,则仅捕获一次数组中的值并将其保存在日志中,但如果索引为2,则捕获数组的值2次并重复,并且相同的行为在增加,具体取决于索引中的值。

      'lass tmonCon():

def setup_logger(name, log_file, level=logging.DEBUG):
    formatter = logging.Formatter(' %(levelname)s %(message)s')
    handler = logging.FileHandler('C:\\config\\' + log_file)
    handler.setFormatter(formatter)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger

# conection to the database to get the Webtmon details
try:
    connect = psycopg2.connect("dbname='TMONNETWarehouse' user='postgres' host='localhost' password='John1710'")
except:
    print ("I am unable to connect to the database")
# Read the db and assign the details to a variable
cur = connect.cursor()
cur.execute("""SELECT "Type","Realtime", "LowerBoundary", "NAID", "RecordTime" FROM "Boundaries" """)
rows = cur.fetchall()
for i in range (len(rows)):
    logType = rows[i][0]
    logRealT = rows[i][1]
    logLowerB = rows[i][2]
    logNaid  = rows[i][3]
    logRecordT = rows[i][4]
    logSearch = setup_logger('tmonSearch', 'tmonSearch')
    logSearch.info(str(logType)+", " + str(logRealT) + ", " + str(logLowerB) +", "+str(logNaid)+", "+str(logRecordT))
#print (logType, logRealT, logLowerB, logNaid, logRecordT)
# logSearch = setup_logger('tmonSearch', 'tmonSearch')
#logSearch.info(rows)

# import variable from the main class and use variables for calculation
# ***************************************************************
# * Add the filter method to determine if the alert needs to be *
# * sent                                             *
# *                                                             *
# ***************************************************************
for i in range (len(rows)):
    if (rows[i][1] < rows[i][2] * tmonMain.DELTA_ADJUST) and (rows[i][1] < rows[i][2] - tmonMain.DELTA_ADJUST):
        RealT = rows[i][1]
        NETID = rows[i][3]
        RecTime = rows[i][4]
        logCount = setup_logger('tmonCount', 'tmonCount')
        logCount.warning(str(RealT)+"," + str(NETID) + "," + str(RecTime))
        reminder = 0'

我的日志是,您可以看到每个循环的第四个值都在增加  INFO T,0.0,0.0,MXO1Z,2018-05-30 16:59:00  INFO T,0.0,0.0,RO335,2018-05-30 16:58:00  INFO T,0.0,0.0,RO335,2018-05-30 16:58:00  INFO P,0.0,0.0,PKL55,2018-05-30 16:58:00  INFO P,0.0,0.0,PKL55,2018-05-30 16:58:00  INFO P,0.0,0.0,PKL55,2018-05-30 16:58:00  INFO P,0.0,0.0,GU587,2018-05-30 16:58:00  INFO P,0.0,0.0,GU587,2018-05-30 16:58:00  INFO P,0.0,0.0,GU587,2018-05-30 16:58:00  INFO P,0.0,0.0,GU587,2018-05-30 16:58:00  INFO T,0.0,0.0,PK999,2018-05-30 17:00:00  INFO T,0.0,0.0,PK999,2018-05-30 17:00:00  INFO T,0.0,0.0,PK999,2018-05-30 17:00:00  INFO T,0.0,0.0,PK999,2018-05-30 17:00:00  INFO T,0.0,0.0,PK999,2018-05-30 17:00:00

1 个答案:

答案 0 :(得分:0)

setup_logger在每次迭代中添加一个处理程序。结果是,在第N次迭代中,N个处理程序将打印相同的消息。

只需将其移出循环(我还清理了循环)

log_search = setup_logger('tmonSearch', 'tmonSearch')

for row in rows:
    log_search.info(', '.join((str(r) for r in row)))  # not sure if str conversion is needed