One option would be to create a new ContextProvider interface and get your context string from it
public interface ContextProvider{ public List<String> getContextToLog(); } ... public class DefaultLoggingContext implements ContextProvider{ public List<String> getContextToLog(){ ... list.Add(CurrentUser.getName()); ... return list; } } ... public class Logger{ private static ContextProvider contextProvider; public static initiliseLogger(ContextProvider defaultProvider){ contextProvider = defaultProvider; } public static log(String text){ log(text, contextProvider); } public static log(String text, contextProvider){ List<String> toLog = contextProvider.getContextToLog(); toLog.add(text); } ... public class ...{ private ContextProvider loggingContext;
You can do this even further and use Formatter instead of contextProvider, the formatter will be responsible for entering the string "text" and formatting it completely, including adding information about the session, date, time, request, etc. You can look at log4 * for a complete understanding of this.
On the side of the note, I would suggest that moving the log method as an instance method, rather than a static method, would be a very good step, you can either support it for a while, or statically marked as deprecated, or you can find and replace the stick. One log4 * feature that I really like is the ability to change logging sensitivity based on a class or package.
source share