python添加fluent日志记录

istio默认会进行日志的记录,但是仅仅记录到服务、以及服务之间调用的信息,不记录业务日志。

如:

所以需要添加业务日志记录。

1.python引入package

fluent
msgpack

2.代码中引入相关类,并连接fluent服务

1)Event-Based Interface 发送fluent日志 是对FluentSender的包装

from fluent import sender, event

logger = sender.FluentSender('fluent-python', host='192.168.181.99', port=30224)

对应方法中添加日志记录

 

2)Python logging.Handler interface 定义日志格式 发送fluent日志

 

import msgpack
from io import BytesIO
import logging
from fluent import handler


def overflow_handler(pendings):
unpacker = msgpack.Unpacker(BytesIO(pendings))
for unpacked in unpacker:
print(unpacked)


custom_format = {
'host': '%(hostname)s',
'where': '%(module)s.%(funcName)s',
'type': '%(levelname)s',
'stack_trace': '%(exc_text)s'
}

logging.basicConfig(level=logging.INFO)
l = logging.getLogger('fluent.test')

#remote fluent服务 

# h = handler.FluentHandler('fluent-python.log', host='fluentd-es.logging', port=24224, buffer_overflow_handler=overflow_handler)

# 本地调用fluent服务
h = handler.FluentHandler('fluent-python.log', host='192.168.181.99', port=30224, buffer_overflow_handler=overflow_handler)
formatter = handler.FluentRecordFormatter(custom_format)
h.setFormatter(formatter)
l.addHandler(h)

对应方法中添加日志记录

3.fluent ui 展现情况

1)event记录日志

2)自定义日志格式

4.事例项目

istio事例项目(jeager-fluent分支)

https://github.com/jiuchongxiao/istio-python-hello-jaeger.git

没加istio事例项目

https://github.com/jiuchongxiao/python-fluent-example.git

猜你喜欢

转载自www.cnblogs.com/jiuchongxiao/p/9088530.html