通过带有可选Sentry集成的预配置的structlogger记录而无需设置日志
structlog-sentry-logger的Python项目详细描述
Structlog哨兵记录器
多用途、预配置、性能优化的^{
为什么
- 使日志记录与使用print语句一样简单,但更漂亮,更不臭!在
- 非常固执己见!只有two (2) distinct configurations。在
- JSON格式的结构化日志意味着它们可以被许多 最喜欢的日志分析工具!在
你得到了什么
强大的自动上下文字段
预配置选项包括:
- 时间戳
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
- 日志级别
- 添加到JSON上下文以进行筛选和分类
- 记录器名称
- 自动分配给初始化的命名空间版本
python模块(
.py
文件),相对于项目目录。- e、 g,
docs_src/sentry_integration.py
中的记录器名为docs_src.sentry_integration
- e、 g,
- 自动分配给初始化的命名空间版本
python模块(
使用按键排序的字段,以便于快速分析。在
性能
structlog-sentry-logger
已完全调整并利用^{
如需进一步参考,请参阅:
- “^{
}: Serialize ”表示基准 - “^{
}: Performance ”表示显著的性能相关配置。在
[1] 资料来源: Choosing a faster JSON library for Python: Benchmarking
内置哨兵集成(可选)
自动添加更丰富的背景到您的哨兵报告。在
- 当
structlog-sentry-logger
时,整个日志上下文将作为哨兵事件发送 日志级别为error
或更高。- i、 例如,
logger.error("")
,logger.exception("")
- i、 例如,
- 有关详细信息,请参见^{
} 。在
目录
安装
pip install structlog-sentry-logger
使用
纯structlog
日志记录(无哨兵)
在Python模块的顶部,导入并实例化记录器:
^{pr2}$现在,任何时候你想打印任何东西,都不要。而是这样做:
LOG_MSG="Information that's useful for future me and others"LOGGER.info(LOG_MSG,extra_field="extra_value")
注意:支持所有的正则Python logging levels。
自动生成:
{
"event": "Information that's useful for future me and others",
"extra_field": "extra_value",
"level": "info",
"logger": "docs_src.pure_structlog_logging_without_sentry",
"sentry": "skipped",
"timestamp": "2020-10-18 15:30:05"
}
岗哨一体化
导出您的Sentry DSN 融入当地环境。在
- 一种简单的方法是将其放入本地
.env
文件中并使用 ^{} 要填充环境:
# On the command line:SENTRY_DSN=YOUR_SENTRY_DSN echo"SENTRY_DSN=${SENTRY_DSN}" >> .env
然后在实例化记录器之前,在Python代码中加载.env
文件,例如:
fromdotenvimportfind_dotenv,load_dotenvload_dotenv(find_dotenv())importstructlog_sentry_loggerLOGGER=structlog_sentry_logger.get_logger()
将自定义上下文直接记录到Sentry
使用structlog
,您甚至可以在异常处理中合并自定义消息,这些消息将自动报告给Sentry(多亏了structlog-sentry
模块):
importuuidimportstructlog_sentry_loggerLOGGER=structlog_sentry_logger.get_logger()curr_user_logger=LOGGER.bind(uuid=uuid.uuid4().hex)# LOGGER instance with bound UUIDtry:curr_user_logger.warn("A dummy error for testing purposes is about to be thrown!")x=1/0exceptZeroDivisionErroraserr:ERR_MSG=("I threw an error on purpose for this example!\n""Now throwing another that explicitly chains from that one!")curr_user_logger.exception(ERR_MSG)raiseRuntimeError(ERR_MSG)fromerr
{
"event": "A dummy error for testing purposes is about to be thrown!",
"level": "warning",
"logger": "docs_src.sentry_integration",
"sentry": "skipped",
"timestamp": "2020-10-18 15:29:55",
"uuid": "181e0e00b9034732af4fed2b8424fb11"
}
{
"event": "I threw an error on purpose for this example!\nNow throwing another that explicitly chains from that one!",
"exception": 'Traceback (most recent call last):\n File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 10, in <module>\n x = 1 / 0\nZeroDivisionError: division by zero',
"level": "error",
"logger": "docs_src.sentry_integration",
"sentry": "sent",
"sentry_id": null,
"timestamp": "2020-10-18 15:29:55",
"uuid": "181e0e00b9034732af4fed2b8424fb11"
}
Traceback (most recent call last):
File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 10, in <module>
x = 1 / 0
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 17, in <module>
raise RuntimeError(ERR_MSG) from err
RuntimeError: I threw an error on purpose for this example!
Now throwing another that explicitly chains from that one!
输出:格式化和存储
默认行为是将JSON日志直接流式传输到标准输出 流like a proper 12 Factor App。在
对于本地开发,它通常有助于美化日志记录到stdout和save
JSON会记录到项目目录根目录下的.logs
文件夹,以便以后使用
调试。要启用此行为,请设置以下环境变量
(例如通过^{
CI_ENVIRONMENT_SLUG=dev-local
在此过程中,通过前面的异常处理示例,我们将得到:
摘要
就这样。现在没有借口了。 走出去,带着不认识任何人的骄傲去计划 在制作中会嘲笑你!因为没有正确地记录日志。你自己一个人 为了其他的东西。在
进一步阅读
^{} :Python的结构化日志
^{} :监视并修复rea中的崩溃l时间。
^{} :为Sentry集成提供^{} SentryProcessor
。
- 项目
标签: