[英]How to send a logging attachment to Sentry in Python?
I have been trying to send a " .log
" file attachment to sentry from python every time an error occurs/exception occurs but so far without success.每次发生错误/异常时,我一直试图从 python 向哨兵发送“ .log
”文件附件,但到目前为止没有成功。 Sentry does not provide attachments documentation for python, so I have been reading the java attachments example ( https://docs.sentry.io/platforms/java/enriching-events/attachments/ ), which is Sentry 不提供 python 的附件文档,所以我一直在阅读 java 附件示例( https://docs.sentry.io/platforms/java/enriching-events/attachments/ ),这是
import io.sentry.Sentry;
import io.sentry.Attachment;
Attachment fileAttachment = new Attachment("your/path/file.log");
// Global Scope
Sentry.configureScope(scope -> {
scope.addAttachment(fileAttachment);
});
// Clear all attachments in the global Scope
Sentry.configureScope(scope -> {
scope.clearAttachments();
});
// Local Scope
Sentry.withScope(scope -> {
scope.addAttachment(fileAttachment);
Sentry.captureMessage("my message");
});
Trying to do a similar conversion in python using sentry_sdk ( https://github.com/getsentry/sentry-python/tree/master/sentry_sdk ), my code is:尝试使用 sentry_sdk ( https://github.com/getsentry/sentry-python/tree/master/sentry_sdk ) 在 python 中进行类似的转换,我的代码是:
from sentry_sdk.scope import Scope
from sentry_sdk import configure_scope, push_scope
scope=Scope()
configure_scope(lambda scope: scope.add_attachment(path="sentry.log"))
push_scope(lambda scope: scope.add_attachment(path="sentry.log"))
ps In python Attachment()
objects gets created inside scope.add_attachment()
, so no need for explicit assignment. ps 在 python 中, Attachment()
对象是在scope.add_attachment()
内创建的,所以不需要显式赋值。 I also tried push_scope()
but did not have much effect.我也试过push_scope()
但没有太大效果。
Any help on this issue is appreciated.对这个问题的任何帮助表示赞赏。
I was able to send an attachment by adding this line of code capture_exception(AttributeError())
where AttributeError()
can be a built-in exception or a custom one derived from the Exception
class.我能够通过添加这行代码来发送附件capture_exception(AttributeError())
其中AttributeError()
可以是内置异常或从Exception
类派生的自定义Exception
。 A minimal working code is the following.一个最小的工作代码如下。
from sentry_sdk.scope import Scope
from sentry_sdk import configure_scope, push_scope
from sentry_sdk.api import capture_exception
scope=Scope()
configure_scope(lambda scope: scope.add_attachment(path="sentry.log"))
capture_exception(AttributeError())
You may further explore by visiting https://github.com/getsentry/sentry-python/blob/master/tests/test_basics.py您可以通过访问https://github.com/getsentry/sentry-python/blob/master/tests/test_basics.py进一步探索
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.