fluentd的python客户端库
pyfluent的Python项目详细描述
简介
pyfluent是用于Fluentd的python客户端库。 它简单、快速、可靠。
主要目的是提供pythonic方法将json消息传输到fluentd。
为了获得更好的性能,pyfluent连接到fluentd的in_forward插件并传输由MessagePack序列化的消息。 当连接失败时,消息将自动排队等待后一次重新传输。 重新建立连接时,将自动重新传输所有排队的消息。 所以,你不用担心丢失信息。
pyfluent在apache许可2.0版下可用。
注意:由于长时间不活动,我决定将开发分支重置为0.2.0版本。旧开发分支中的所有提交现在都存在于服务器分支中。
安装
pyfluent需要python 2.5或更高版本(包括python 3)。
您可以使用pip安装:
$ pip install pyfluent
客户机
发送消息非常简单。就像这样:
from pyfluent.client import FluentSender fluent = FluentSender() fluent.send('Hello pyfluent!')
默认情况下,fluentsender连接localhost:24224并使用空字符串作为标记。 你可以通过传递参数来改变这一点。
fluent = FluentSender('fluent.example.com', 10000, 'pyfluent') fluent.send('Hello pyfluent!')
上面的示例中,我们将字符串作为fluentsender.send的参数传递。 为了方便起见,fluentsender.send在发送之前自动生成dict。
另一方面,你可以把dict当作参数传递。
fluent.send({'hello': 'fluent'})
另外,如果需要,可以更改每条消息的标记和时间戳。
import time fluent.send({'hello': 'fluent'}, 'pyfluent.info', time.time() - 60)
日志记录
由于pyfluent日志库的实现类似于python标准日志库, 很容易将混浊引入到现有的程序中。
以下是基本用法:
import logging from pyfluent.logging import SafeFluentHandler handler = SafeFluentHandler('localhost', 24224, 'pyfluent') handler.setLevel(logging.INFO) logger = logging.getLogger() logger.setLevel(logging.INFO) logger.addHandler(handler) logger.info('hello pyfluent!')
您可以通过fluentd获得如下json消息。
2012-03-02 11:50:46 +0900 pyfluent.info: {"message":"hello pyfluent!"}
如您所见,fluenthandler会自动将日志级别附加到标记。
为了方便起见,pyfluent提供fluentformatter来发送更多信息。
from pyfluent.logging import FluentFormatter formatter = FluentFormatter('%(asctime)s %(levelname)s %(message)s') handler.setFormatter(formatter) logger.info('get more information')
您可以获得如下json消息。
2012-03-02 11:52:18 +0900 pyfluent.info: {"threadName":"MainThread","name":"r oot","process":88908,"hostname":"host.example.com","module":"<ipython-input-2 3-ad36b045792f>","filename":"<ipython-input-23-ad36b045792f>","processName":" MainProcess","pathname":"<ipython-input-23-ad36b045792f>","lineno":1,"exc_tex t":null,"message":"2012-03-02 12:35:18,321 INFO get more information","funcNa me":"<module>","levelname":"info"}
默认行为是发出日志记录中除args,asctime,created,exc_info,levelno,msecs,msg,relativeCreated,thread和message之外的所有项目。例如,如果需要,它可以自定义,除了默认值之外,threadName、module、filename、processName、pathname、lineno和funcName。
formatter.exclude += ['threadName', 'module', 'filename', 'process', 'processName', 'pathname', 'lineno', 'funcName'] logger.info('suppress unnecessary information')
因此,您可以获得如下信息。
2012-03-02 11:54:21 +0900 pyfluent.info: {"exc_text":null,"message":"2012-03- 02 14:23:21,504 INFO suppress unnecessary information","hostname":"host.examp le.com","name":"root","levelname":"info"}
请注意:
- ^{tt18}$ is added automatically by FluentFormatter, so you cannot remove ^{tt18}$ from output information.
- ^{tt3}$ is converted to the fluentd’s time.
历史记录
- 0.2.1(2019-01-10)
- 提高可靠性
- 0.2.0(2012-04-21)
- 添加fluentsender。
- 重写safefluenthandler以在内部使用fluentsender。
- 将许可证从mit许可证更改为apache许可证,版本2.0。
- 0.1.2(2012-03-02)
- 支持Python2.5、2.6、3.0、3.1、3.2。
- 0.1.1(2012-02-26)
- 修复从pypi安装时出现的问题。
- 0.1.0(2012-02-26)
- 第一次释放。