为python应用程序记录对象。
stutter的Python项目详细描述
stutter是我自己对于简单日志库的想法。包句柄中的对象 时间戳和自动记录到文件记录到文件可以缓冲, 甚至是线程化的,根据应用程序的需要
注意,可以使用BufferedReader对象和twisted to 不需要穿线。我将来可能会创造这样一个物体。
简要示例
如果要记录消息,请使用此包中的对象我打算用这个 在一个机器人中,也可能是一个客户端,用于deviantART聊天网络测井罐 操作如下:
from stutter import logging logger = logging.BaseLogger() logger.message('Sup homie?') logger.message('Everything is saved to files in `./log` by default') logger.message('You can change this somehow... I'll explain later.') logger.debug('This message won't be printed on-screen, but will be saved.')
缓冲日志记录
文件IO有时很昂贵,因此可以使用bufferedlogger对象 以确定何时将邮件保存到文件当缓冲记录器 用于在屏幕上显示日志消息,消息立即显示, 但在使用.push()之前,它们不会保存到任何文件:
import time from stutter import logging logger = logging.BufferedLogger() logger.message('Some boring message...') time.sleep(5) # You can use the time you have to confirm that no logs have been written # yet, if you like. These are really trivial examples... logger.warning('Writing logs!') logger.push()
请注意,.push()一次最多只能保存5条消息。你可以改变 通过使用limit参数告诉push要保存多少条消息。 例如,要保存最多10条消息,您可以调用.push(limit=10)。到 将all存储的消息推送到日志文件中,调用.push(limit=0)
线程日志记录
如果您想自动调用push而不必认真考虑 太多,您可以使用ThreadedLogger对象一个简单而天真的例子:
from stutter import logging logger = logging.ThreadedLogger() # Start pushing in a thread. logger.start() logger.message('Your mother smells of elderberries.') # Stop the threading stuff. logger.stop() # Wait until the thread has actually stopped. logger.join() # Make sure all log messages have been saved. # This is not done by stopping the thread! logger.push(0)
文件
我会在被纵火时写文件。