Skip to content

Instantly share code, notes, and snippets.

@abyx
Created August 27, 2010 16:58
Show Gist options
  • Save abyx/553724 to your computer and use it in GitHub Desktop.
Save abyx/553724 to your computer and use it in GitHub Desktop.
Logback converters for username and sessions
public class SessionConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
if (attrs != null) {
return attrs.getSessionId();
}
return "NO_SESSION";
}
}
public class UserConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
return auth.getName();
}
return "NO_USER";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment