Leaderboard1

Leaderboard2

Thursday, December 20, 2012

Tomcat: How to send the log messages to a File

Sometimes it's needed to send logs to separate file without mixing  them in server log files.. Thats makes it easy to understand & prevent disk overflow situation. We can use log4g.jar for this.

Log4j File Appenders – example for Logging messages to a file:
In most cases, either Rolling File Appender or Daily Rolling File Appender classes are used in production scenarios for logging messages into a file. A Rolling File Appender will send all the log messages to the configured file, until the file reaches a certain size. Once the log file reaches a maximum size, the current file will be backed up and a new log file will be created (thus the name Rolling File Appender). The Daily Rolling File Appender is also similar to Rolling File Appender except that the rolling happens at given frequency. Below is an example for using RollingFileAppender
  1. Add Log4j logging support to your project.
  2. In your log4j.properties file, add below lines:
    log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
    log4j.appender.rollingFile.File=D:/myapp/mylog.log
    log4j.appender.rollingFile.MaxFileSize=2MB
    log4j.appender.rollingFile.MaxBackupIndex=2
    log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
    log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
  3. As you can see from the above log4j  configuration, we are creating a new log4j appender in the name ofrollingFile and setting it’s options. I guess most of the options are self explanatory. The MaxBackupIndex tells the log4j to keep a maximum of 2 backup files for the log mylog.log. If the log file is exceeding theMaximumFileSize, then the contents will be copied to a backup file and the logging will be added to a new empty mylog.log file. From the above configuration, there can be a maximum of 2 backup files can be created.
  4. Next we need to direct our logs to go into this log4j appender.
    log4j.rootLogger = INFO, rollingFile
  5. That’s it. From now on, when you log something from your Java class, the log message will go into the mylog.log file.
 Copy this log4j.properties file in webcontent. Give its path in servlet class. See below one.

public void init(ServletConfig config) throws ServletException {
//1.
logger.info("XML_FetcherInitServlet is initializing log4j");
String log4jLocation = config.getInitParameter("log4j-properties-location");

ServletContext sc = config.getServletContext();

if (log4jLocation == null) {
System.err.println("*** No log4j-properties-location init param, so initializing log4j with BasicConfigurator");
BasicConfigurator.configure();
} else {
String webAppPath = sc.getRealPath("/");
String log4jProp = webAppPath + log4jLocation;
File log4jPropsFile = new File(log4jProp);
if (log4jPropsFile.exists()) {
System.out.println("Initializing log4j with: " + log4jProp);
PropertyConfigurator.configure(log4jProp);
} else {
System.err.println("*** " + log4jProp + " file not found, so initializing log4j with BasicConfigurator");
BasicConfigurator.configure();
}
}
logger.debug("RealPath" + sc.getRealPath("/"));
//3.
super.init(config);
}

If log4j.properties not found then default will be used by server..

No comments:

Post a Comment