[logback-user] per-session logging level and appending

Chris shef31 at yahoo.com
Tue Sep 28 04:38:48 CEST 2010


On 9/27/2010 6:10 PM, Gregory Gerard wrote:
> Here's what I'd like to do. I have an idea how I would do but would like feedback and to know if someone has already done this before I spend time.
>
> 1. when a user logs in, I would like to create a new appender just for their actions.
>
> 2. during the course of the user's interactions (new code is entered, an exception is encountered which is not fatal but puzzling), I would like to make the logging finer.
>
> 3. by some action (a link at the top of the page or a fatal/really bad exception is encountered), I would like to package up these collected logs and notify someone.
>
> 4. if someone with appropriate rights logs into a server, the admin can watch the logs for that user's session.
>
> basically, I want a per-session flight recorder.
>
> I plan to use MDC to stuff interesting things for the appender of course.
>
> Any problems with this or recommendations? I plan on either using Tomcat/Spring or JEE6.
>
> In those environments, is there a recommended way of doing reasonably reliable DB operations?
>
> thanks,
> greg

This is the same question I raised above in the post "Dynamic Loggers?". 
Logback doesn't support it very well. Essentially, it appears to be 
impossible to do it using the logback.xml configuration alone. The 
reason is that you can't tell the system to give each 
dynamically-created logger its own appender instance.

As a workaround, I had to implement it in code. I've pasted the current 
incarnation of the function below. This solution sucks because 
everything is hard-coded, and you can't change the appender type in the 
.xml file.

Please let me know if you find a better solution.



/**
  * Return a Logger that creates a file in the specified directory,
  * using the specified prefix as a subdirectory name. Creates the
  * logger if it doesn't exist.
  * @param dir directory to contain the log files
  * @param prefix prefix for the log file name
  * @param encodePattern the pattern to use when formatting each line in 
the log
  * @return a logger
  */
public static synchronized Logger getLogger(String dir, String prefix, 
String encoderPattern) {

   File file = new File(dir + "/" + prefix, prefix);
   String path = file.toString();

     Logger logger = LoggerFactory.getLogger("dynamic." + path);

     if (logger instanceof ch.qos.logback.classic.Logger) {
       ch.qos.logback.classic.Logger lb = 
(ch.qos.logback.classic.Logger) logger;

       if (lb.getAppender("dynamic") == null) {
         lb.setAdditive(false);
         LoggerContext context = lb.getLoggerContext();

         // configure the logger unless the overrideDynamic flag has 
been set
         if (!"true".equals(context.getProperty("overrideDynamic"))) {

           String filenamePattern = 
context.getProperty("DEFAULT_FILENAME_PATTERN");
           if (encoderPattern == null) {
             encoderPattern = 
context.getProperty("DEFAULT_ENCODER_PATTERN");
           }

           RollingFileAppender app = new RollingFileAppender();
           app.setContext(context);
           app.setName("dynamic");

           TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
           policy.setContext(context);
           policy.setFileNamePattern(path + filenamePattern);
           policy.setParent(app);

           PatternLayoutEncoder encoder = new PatternLayoutEncoder();
           encoder.setContext(context);
           encoder.setPattern(encoderPattern);

           app.setRollingPolicy(policy);
           app.setEncoder(encoder);

           policy.start();
           encoder.start();
           app.start();

           lb.addAppender(app);
         }
       }
     }

     return logger;
}





More information about the Logback-user mailing list