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第三方库


热门话题
如何使用外部java程序在minecraft中移动minecraft角色的相机   java输出文本文件中的变量   java LazyLoadingException在我尝试从多通关系获取对象时出现   java json rest API的错误:ClassCastException:org。json。无法将JSONObject强制转换为组织。json。杰索纳雷   java BigInteger。C中的intValue()等价物#   java大写所有字符,但不包括带引号字符串中的字符   java获取特殊字符   javascript为什么Selenium中的所有getX()调用都需要这么长时间?   rabbitmq rabbitmq java客户端并行消费   如何使用selenium Java在popover窗口中提取文本   对象在java中构造一类对象   java Room数据库未实现   json JSONObject可以使用java保存大的格式化双值吗?   有时限的旅行推销员   java HttpsURLConnection openConnection查询   java无法使用Spring@Entity注释创建MySQL表   lambda Java 8仅映射到值类型集合   java提供OSGi服务而不实现接口   java单个对象重写对象数组,不确定原因