1

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?

Thomas
  • 620
  • 7
  • 19
  • 1
    You'd either have to append the user or use something like `MDC` (depending on your logging framework). Logging itself doesn't have any knowledge of the user. – James R. Perkins Nov 29 '16 at 18:25
  • Thanx a lot for your answer. My best idea is now to create a Wildfly module which implements a custom log handler that references the current session. Would that be possible? I am having a hard time to figure out how to build and deploy a cusom wildfly module. I would appreciate if someone could point me in the right direction. – Thomas Dec 02 '16 at 12:44
  • 1
    It wouldn't work as logging doesn't know what a session is. You may be able to use a servlet listener which would insert the user in MDC, then remove it when the request is complete. – James R. Perkins Dec 02 '16 at 18:53

2 Answers2

0

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/

Thomas
  • 620
  • 7
  • 19
0

I encountered the same situation as yours and found that there is no Wildfly in built support for the same. Hence the two options that you can have are-

  1. Append username to the logs manually as what you mentioned already in your question. Personally I prefer this to keep the logging minimum and relevant.
  2. Use MDC and along with that implement 'filter' attribute of logging in Wildfly, so that you can keep only relevant log messages in your log file.
Caffeine Coder
  • 948
  • 14
  • 17