Monday, 21 March 2016

How to maintain a separate logs for some functionality of your application ?



Setup the log4j.properties

# Set root logger level to WARN and append to stdout
log4j.rootLogger=ERROR, stdout, SYSLOG  
log4j.logger.NoModule=ERROR    
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n
# Print only messages of level ERROR or above in the package noModule.
log4j.logger.noModule=ERROR

# Entries for Login Logger
log4j.logger.com.shaan.logging.LoginLogger=INFO, login, SYSLOG
log4j.appender.login=org.apache.log4j.RollingFileAppender
log4j.appender.login.File=Login.log
log4j.appender.login.MaxFileSize=1000KB
log4j.appender.login.MaxBackupIndex=10
log4j.appender.login.layout=org.apache.log4j.PatternLayout
log4j.appender.login.layout.ConversionPattern=%d (%t) [%24F:%-3L:%-5p]%x %m%n

# Added for SYSLOG generation on audit requirment
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost=10.200.205.100
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern=%-5p -%m
log4j.appender.SYSLOG.Facility=LOCAL1


Create a customized logger class to write the logs
package com.shaan.logging;
public class LoginLogger {
  private static final String logModule = LoginLogger.class.getName();
  public static void log(Map logValues) {
    // Use logValues map for writing values
    Debug.logInfo("logs to write ...", logModule);
  }
}

Now, you can use this class and its method to write log in a separate log file.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.