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编辑并重新运行spring引导单元测试,无需重新加载上下文即可加快测试速度   为什么我不能做演员?   java为什么是线程。join通常用于停止安卓中的线程   java从weblogic服务器调用JSON POST REST服务时收到400:错误请求   java在DeviceAdmin模式禁用时设置身份验证?   java SortedMap的keySet()能否始终安全地强制转换到SortedSet?   安卓 java。lang.NoSuchMethodException可包裹类   java JOGL库安装   javatomcat内存管理   java使用getString()中的变量   java将最小星号设置为评级栏   Java中字符串相等的println()方法。。。它到底是如何工作的?   java如何从文本中输出的数组中放入随机图像