wildflywildfly-10jboss-logging

How can i append the username (principal) to each log line in Wildfly?


I am working with Wildfly 10 and I am using its logging subsystem.

Now, I need to append the LoginName of the currently logged in User (principal) from the session to every log message that is processed by the JBoss Logging Subsystem.

Is there a generic way to do this, rather than appending the UserName to every Log Message in my Application?

For example i could do:

        HttpServletRequest request = (HttpServletRequest)externalContext.getRequest();
        UserModel user = (UserModel)request.getSession().getAttribute("user");
        String username = user.getName();

But how can i get the Logging Subsystem to do this for every Log Message?


Solution

  • As James R. Perkins suggested, this can be done be using MDC (Mapped Diagnostic Context).

    An example of how this works can be found here: https://veerasundar.com/blog/2009/11/log4j-mdc-mapped-diagnostic-context-example-code/