-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to log spring principal in request? #236
Comments
This should work: final class PrincipalHttpLogFormatter implements HttpLogFormatter {
private final JsonHttpLogFormatter delegate;
PrincipalHttpLogFormatter(final JsonHttpLogFormatter delegate) {
this.delegate = delegate;
}
@Override
public String format(final Precorrelation<HttpRequest> precorrelation) throws IOException {
final Map<String, Object> content = delegate.prepare(precorrelation);
content.put("principal", getPrincipal());
return delegate.format(content);
}
@Override
public String format(final Correlation<HttpRequest, HttpResponse> correlation) throws IOException {
final Map<String, Object> content = delegate.prepare(correlation);
content.put("principal", getPrincipal());
return delegate.format(content);
}
private String getPrincipal() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
@Nullable final String principal = authentication.getName();
return principal == null ? "anonymous" : principal;
}
} |
The only place to add this within logbook would be the spring-boot-starter, but I'm hesitant to do it, since it doesn't feel right to only support this for the json format. |
Thank you but i don't understand where can i configure my logbook?
In which class i can do that, to apply my PrincipalHttpLogFormatter? |
|
I understood that, but where should i put that configuration in which class? |
If you're using the spring boot starter, then you can just override that part of the auto configuration:
@Bean
public HttpLogFormatter httpLogFormatter(final ObjectMapper mapper) {
return new PrincipalHttpLogFormatter(new JsonHttpLogFormatter(mapper));
} |
Thank you very much :) |
@whiskeysierra how about in a webflux application ? |
Hello folks, I am banging my head from last today as I can't able to print the JSON log in the beautified format in the IntelliJ console rather it is printing the whole JSON in a single line. Can anyone please help me with this?
mail: rameshrathnat@gmail.com |
Is there any way to implement this?
The text was updated successfully, but these errors were encountered: