Show Users In Logs – Alfresco NDC

I was trying to debug a user’s issue on one of our alfresco servers.  I looked through the alfresco log file but of course there are entries from many users there so it was tough to pinpoint the relevant ones.

One of our other alfresco servers adds the user name to the log statements and that would be very helpful in this case.  I couldn’t figure out why our 3.2 server included user names but our 3.4 server did not.

Here’s what I found out:Alfresco’s AuthenticationUtil class (unchanged from 3.2 to 3.4) has a logNDC() method that adds the user name.

NDC (Nested Diagnostic Contexts) is part of Log4j.  Look for “Nested Diagnostic Contexts” way down this page for an explanation: http://logging.apache.org/log4j/1.2/manual.html

Instead of directly using Apache’s NDC, Alfresco uses a delegate to avoid any dependancy. AuthenticationUtil uses org.alfresco.util.log.NDC instead of org.apache.log4j.NDC.

The first thing Alfresco’s NDC class does is to instantiate its delegate (org.alfresco.util.log.log4j.Log4JNDC) which then uses org.apache.log4j.NDC.

HOWEVER

It only does this if org.alfresco.util.log.NDC’s logger has debug enabled.  If not then the delegate is null and any NDC calls do nothing at all.

How to Fix It

The solution is simple, set Alfresco’s NDC logger to debug in your log4j properties file like so:

log4j.logger.org.alfresco.util.log.NDC=debug

That class doesn’t actually log any messages so this won’t add anything to your log files except the user name.

Now just make sure the File appender includes the NDC information by adding %x to the conversion pattern like so:

log4j.appender.File.layout.ConversionPattern=%d{ABSOLUTE} %x %-5p [%c] %m%n

You just get this:

12:25:00,018 User:System DEBUG [org.alfresco.repo.jscript.ScriptLogger] start clean user home script

instead of this:

12:25:00,018 DEBUG [org.alfresco.repo.jscript.ScriptLogger] start clean user home script

NOTE

Usually you set your log4j overrides in a file in tomcat/shared/classes/alfresco/extension called custom-log4j.properties or dev-log4j.properties or something like that.  You can go ahead and put the NDC=debug line in this file.  However if you put your new conversion pattern in this file in 3.4 it will be ignored.  (See https://issues.alfresco.com/jira/browse/ALF-13742). Instead you must change the conversion pattern in tomcat/webapps/alfresco/WEB-INF/classes/log4j.properties.

About Alfresco Versions

This has been annoying me for a long time.  I looked at Alfresco’s NDC class in 3.2 and it does NOT check if debug is enabled – it just finds org.apache.log4j.NDC and uses it.  I’m not sure when this changed (I haven’t looked at 3.3) but I do know that you need to set NDC to debug in 3.4 and 4.0 as org.alfresco.util.log.NDC is the same in both of those.

 

This entry was posted in Alfresco, ECM by tim.frith. Bookmark the permalink.
tim.frith

About tim.frith

Alfresco Certified Engineer

Senior ECM Consultant
Tim holds a diploma in Computer Systems Technology from Camosun College as well as a Bachelor of Commerce Degree with a Finance specialization from the University of Alberta. He has over 10 years of experience developing web applications with Java as well as extensive database development experience with Oracle, IBM DB2, MS SQL Server, and MySQL.

In recent years he has focused on Enterprise Content Management, with specific training and experience in Alfresco's content management suite.

And finally, Tim is a strong believer that the Edmonton Oilers will soon reclaim their rightful place at the top of the hockey world. Yes, Stanley will once again come to town.

One thought on “Show Users In Logs – Alfresco NDC

  1. Hi Tim,

    Just stumbled across your post now – after finding it all out the hard way – digging through the code.

    So far goes the repo. Seems the situation is a bit different (and not so easy) with Alfresco Share. The NDC class is already used in SOAuthenticationFilter, but it does not quite push what I would expect and one needs a replacement to get it to log the username.

    regards
    Andreas

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>