logd python库
项目描述
pylogd
pylogd是logd的python接口。它通过UDP套接字将日志消息或统计信息推送到logd。
用法
pylogd提供各种工具来处理logd、python logging处理程序,以及一个Stats对象,这使得记录统计信息变得非常简单。
logging
要使用python logging模块将日志记录到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)的调用将记录到您的logd服务器。如果您在根日志记录器(getLogger('base'))上这样做,它将适用于随后创建的所有日志记录器。
要删除日志,请使用带有logd地址和端口的pylogd.delete_log
from pylogd import delete_log
delete_log('mylogpath.log', host='127.0.0.1', 8126)
stats
要使用stats,创建一个stats处理程序
from pylogd.stats import Logd
stats = Logd('127.0.0.1', 8126)
您还可以提供可选的前缀,它将被附加到所有统计信息上,这样多个应用程序就可以使用同一个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支持
对于twisted用户,请使用(包含)pylogd.twisted而不是pylogd,并请注意,直到reactor启动,日志消息和统计信息都不会发送到logd。
项目详情
关闭
pylogd-0.2.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 6b9ad8828833b6f7953b91b30654b3c13ac81c9905761ce60a2edbacd175bc60 |
|
| MD5 | cc9922c19575403f31969d643fdc391c |
|
| BLAKE2b-256 | a09276e78a5785da00fccc9bbfea30c1fad6287c4068a8e9cf6c69d507c54e7d |