将消息放在同一行

时间:2010-02-18 04:26:40

标签: python logging

import pythoncom, pyHook, logging, string

LOG_FILENAME = 'logfile.txt'

def OnKeyboardEvent(event):
    print 'MessageName:',event.MessageName
    print 'Time:',event.Time
    print 'WindowName:',event.WindowName
    print 'Ascii:', event.Ascii, chr(event.Ascii)
    print 'Key:', event.Key
    print '---'
    k = event.Key
    logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG, 
                        format='%(message)s')
    logging.debug(k)

    return True

    hm = pyHook.HookManager()
    hm.KeyDown = OnKeyboardEvent
    hm.HookKeyboard()
    pythoncom.PumpMessages()

在日志file.txt中,消息在不同的行上显示单独的字母,如何让它在同一行显示消息?

1 个答案:

答案 0 :(得分:0)

似乎没有办法让Logger.debug()将消息附加到同一行。由于您的日志文件格式如此简单,为什么不只使用普通文件对象?

logger = open(LOG_FILENAME, 'a')
logger.write(k)
logger.close()