如何使用sentry raven-java客户端设置用户上下文?
我尝试添加一个user_email
标签,并将user_email
添加到MDC中。它们都按预期工作,标签进入标签,而MDC进入附加数据,但是两者都无法设置sentry用户上下文。
我还使用raven-js的javascript与sentry,这很不错:
Raven.setUserContext({
email: '',
id: ''
});
有没有 Java 的等价物?
似乎不能直接通过logback发送用户信息。 您可以查看raven-java的实现:
protected Event buildEvent(ILoggingEvent iLoggingEvent) {
EventBuilder eventBuilder = new EventBuilder()
.withTimestamp(new Date(iLoggingEvent.getTimeStamp()))
.withMessage(iLoggingEvent.getFormattedMessage())
.withLogger(iLoggingEvent.getLoggerName())
.withLevel(formatLevel(iLoggingEvent.getLevel()))
.withExtra(THREAD_NAME, iLoggingEvent.getThreadName());
......
}
来自https://github.com/getsentry/raven-java/tree/master/raven-log4j#mapped-tags:
在appender中设置映射标签:
log4j.appender.SentryAppender.mappedTags=User,OS
并在运行时设置MDC:
void logWithExtras() {
// MDC extras
MDC.put("User", "test user");
MDC.put("OS", "Linux");
// This adds a message with extras and MDC keys declared in mappedTags as tags to Sentry
logger.info("This is a test");
}
Raven.getStoredInstance().addBuilderHelper(eventBuilder -> {
UserInterface userInterface = new UserInterface(MDC.get("userid"), null, MDC.get("ip"), null);
eventBuilder.withSentryInterface(userInterface);
});
new UserInterface(id, username, ipAddress, email)
。 - Alden