用于以graylog扩展日志格式(gelf)发送消息的udp客户端
gelfclient的Python项目详细描述
凝胶客户机
用于以graylog扩展日志格式发送udp消息的python客户端 (盖尔夫)。
消息是zlib压缩的,并且支持gelf分块编码。
由于消息是用udp发送的,所以log方法应该很快返回 不会由于超时而引发异常。但也有例外 由于目标主机名的DNS名称解析失败而引发。
安装
pip安装gelfclient
用法
from gelfclient import UdpClient gelf_server = 'localhost' # Using mandatory arguments gelf = UdpClient(gelf_server) # Using all arguments gelf = UdpClient(gelf_server, port=12202, mtu=8000, source='macbook.local') # Bare minimum is to send a string, which will map to gelf['short_message'] gelf.log('server is DOWN') # 'source' and 'host' are the same. Defaults to socket.gethostname() but can be overridden gelf.log('server is DOWN', source='hostchecker') # Set extra fields gelf.log('status change', _state='DOWN', _server='macbook') # Set severity level import syslog gelf.log('unexpected error', level=syslog.LOG_CRIT) # You can also prepare all data into a dictionary and give that to .log data = {} data['short_message'] = 'warning from python' data['host'] = 'hostchecker' data['level'] = syslog.LOG_WARNING gelf.log(data)
其他字段及其含义见GELF规范: http://docs.graylog.org/en/latest/pages/gelf.html#gelf-format-specification