logd python库
pylogd的Python项目详细描述
Pylogd
pylogd是一个到logd的python接口。它推送日志消息或统计信息 通过udp套接字登录。
用法
pylogd提供各种实用程序来处理logd、pythonlogging处理程序, 以及一个Stats对象,这使得记录统计数据变得很简单。
记录
要使用python日志模块登录到logd,请使用 logd服务器的主机和端口,然后将其设置为默认日志处理程序:
from pylogd.handlers import PylogdHandler handler = PylogdHandler('mylogpath.log', '127.0.0.1', 8126) logger = logging.getLogger() logger.setHandler(handler)
现在,对logger.(error|warn|etc)的后续调用将记录到您的日志 服务器。如果在根记录器(getLogger('base'))上执行此操作,它将 适用于所有随后创建的记录器。
要删除日志,请对日志的主机和端口使用pylogd.delete_log:
from pylogd import delete_log delete_log('mylogpath.log', host='127.0.0.1', 8126)
统计
要使用统计信息,请创建统计信息句柄:
from pylogd.stats import Logd stats = Logd('127.0.0.1', 8126)
您还可以提供一个可选前缀,该前缀将在 stats,以便多个应用程序可以使用同一个logd/graphite服务器 不必为每次统计调用重复每个应用程序的密钥。
一旦有了logd对象,就可以增加和减少计数器(使用 可选采样率:
stats.increment('my.counter') stats.change_by('my.counter', 10) stats.decrement('my.counter', 0.05) # only update 5% of the time
您还可以设置仪表值:
stats.set('my.meter', 30) stats.set('my.meter', 30, 0.25) # only set 25% of the time
有一个基本的时间界面和一个方便的计时器界面:
stats.time('my.timer', 11.43) # time manually # automatically start & stop a timer stats.timer.start('my.timer') do_some_timed_operation() stats.timer.stop('my.timer') # time this function with a 10% sample rate @stats.timed('my.long_operation', 0.1) def long_operation(): pass # accumulate time done doing various similar tasks stats.timer.start_accumulator('timers.mysql') do_some_mysql_stuff() stats.timer.stop_accumulator('timers.mysql') non_mysql_things() stats.timer.start_accumulator('timers.mysql') do_some_more_mysql_stuff() stats.timer.stop_accumulator('timers.mysql') # send this timing information to logd stats.timer.flush_accumulator('timers.mysql')
扭曲支架
对于twisted用户,使用pylogd.twisted(包括)而不是pylogd,并且 注意,日志消息和统计信息在反应堆被 起动。