zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    python爬虫第二天
    sqlite3 数据库创建表
    python 中的nonlocal
    python中 random.seed()函数
    每日一题6/5
    竞赛191
    二进制操作, ~按位取反, | 或, & 与, ^异或, >倍数
    竞赛190
    css BFC
    css动画 Vs js动画
  • 原文地址:https://www.cnblogs.com/jiuchongxiao/p/9088530.html
Copyright © 2011-2022 走看看