structlog与graylog-gelf的集成,由graypy提供
graystruct的Python项目详细描述
structlog与graylog-gelf的集成,由graypy提供。
基本原理
Structlog提供了生成结构化日志消息的丰富工具 从应用程序。Graypy提供了一个python接口来发出登录 由graylog接受的GELF格式。实际上,structlog 预处理pythonloggingmodule的输入,而graypy 处理输出(LogRecord实例),两者都不需要 其他人在场。
graystruct提供由两个主 与structlog和 graypy。这些组件最低限度地改变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')