structlog与graylog-gelf的集成,由graypy提供

graystruct的Python项目详细描述


structlog与graylog-gelf的集成,由graypy提供。

基本原理

Structlog提供了生成结构化日志消息的丰富工具 从应用程序。Graypy提供了一个python接口来发出登录 由graylog接受的GELF格式。实际上,structlog 预处理pythonloggingmodule的输入,而graypy 处理输出(LogRecord实例),两者都不需要 其他人在场。

graystruct提供由两个主 与structloggraypy。这些组件最低限度地改变structlog的行为 以及graypy在它们的接口点,以便它们能够合作 在生产结构化日志时。

示例

>>>importlogging>>>importstructlog>>>fromgraystruct.encoderimportGELFEncoder>>>fromgraystruct.handlerimportGELFHandler>>>fromgraystruct.utilsimportadd_app_context>>>structlog.configure(...logger_factory=structlog.stdlib.LoggerFactory(),...processors=[...# Prevent exception formatting if logging is not configured...structlog.stdlib.filter_by_level,...# Add file, line, function information of where log occurred...add_app_context,...# Format positional args to log as in stdlib...structlog.stdlib.PositionalArgumentsFormatter(),...# Add a timestamp to log message...structlog.processors.TimeStamper(fmt='iso',utc=True),...# Dump stack if ``stack_info=True`` passed to log...structlog.processors.StackInfoRenderer(),...# Format exception info is ``exc_info`` passed to log...structlog.processors.format_exc_info,...# Encode the message in GELF format (this must be the final processor)...structlog.processors.GELFEncoder(),...],...)>>>std_logger=logging.getLogger()>>>std_logger.setLevel(logging.WARNING)>>>gelf_handler=GELFHandler('localhost',12201)>>>std_logger.addHandler(gelf_handler)>>>logger=structlog.get_logger('some.package')# Will transmit a GELF-encoded message>>>logger.error('user.login',username='sjagoe')

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
关于如何将spring mvc重构为普通servlet或jetty处理程序的java建议   java循环和处理数据输入错误   使用java获取更改内容   java试图向一个数组中添加5张不同的“卡”,但它正在添加5张相同的卡   Java函数在Matlab类中变得未定义   多线程Java使用对象作为监视器,那个对象不是太重了吗?   将MySQL日期转换为Java日期:Android   java ResourceBundle在哪里。getBundle(“ResourceFile”,新语言环境(“us”,“us”))查找该文件?   java第一人称摄影机卷   如何在java中交替使用两个字符   addActionListener上下文中的java“this”   几天后,java Grails和Jasig CAS重定向循环   JavaAnt如何在javac中使用exclude、excludesfile?   java如何设置单行RecyclerView上的文本?