How can I log the User Principal in my Log4J Log with Grails?
I want to see the name of the current login when an exception occurs, ie logging the current principal on each action would not work. I need the username stored so when an exception occurs the User Name can be in the Log Pattern.
I assume you should go with log4j’s Nested Diagnostic Contexts (NDC). See the respective chapter in http://logging.apache.org/log4j/1.2/manual.html.
The idea is to create a grails filter hat manages NDC data (push/pop) upon every request and use %x in your logger’s pattern, see http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html.
By doing so, you have separated the burden of logging from the controller/service login.