问题
We do a lot of trace logging in our WebSphere application and would like to separate some timing information in a separate log file.
Usually we import:
import java.util.logging.Level;
import java.util.logging.Logger;
And then declare:
private static final Logger logger = Logger.getLogger(Myclass.class.getName());
And then log:
logger.info("now logging...");
What is the simplest way of doing that?
回答1:
You can use Apache logging services to create logs outside Websphere Application Server. Download log4j jar from here. This is a stable version of jar we have used. Add this jar to your application build path/class path/libraries. Add log4j.properties file under your src folder (empty package). log4j.properties will contain following lines:
log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = com.logging.NewLogForEachRunFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{dd-MM-yyyy} %d{ABSOLUTE} %5p %c:%L - %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = E:/Logs/MyApplication/logs.log
You can change the log level as per environment instead of DEBUG(Production should use INFO or ERROR). Then add a class NewLogForEachRunFileAppender in com.logging package with following code.
package com.logging;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.ErrorCode;
/**
* This is a customized log4j appender, which will create a new line for every
* run of the application.
*/
public class NewLogForEachRunFileAppender extends FileAppender {
public NewLogForEachRunFileAppender() {
}
public NewLogForEachRunFileAppender(Layout layout, String filename,
boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
super(layout, filename, append, bufferedIO, bufferSize);
}
public NewLogForEachRunFileAppender(Layout layout, String filename,
boolean append) throws IOException {
super(layout, filename, append);
}
public NewLogForEachRunFileAppender(Layout layout, String filename)
throws IOException {
super(layout, filename);
}
public void activateOptions() {
if (fileName != null) {
try {
fileName = getNewLogFileName();
setFile(fileName, fileAppend, bufferedIO, bufferSize);
} catch (Exception e) {
errorHandler.error("Error while activating log options", e,
ErrorCode.FILE_OPEN_FAILURE);
}
}
}
public String getNewLogFileName() {
if (fileName != null) {
final String DOT = ".";
final String HIPHEN = "-";
final File logFile = new File(fileName);
final String fileName = logFile.getName();
String newFileName = "";
final int dotIndex = fileName.indexOf(DOT);
if (dotIndex != -1) {
// the file name has an extension. so, insert the time stamp
// between the file name and the extension
newFileName = fileName.substring(0, dotIndex) + HIPHEN
+ getDate(System.currentTimeMillis(), "yyyyMMdd") + DOT
+ fileName.substring(dotIndex + 1);
} else {
// the file name has no extension. So, just append the timestamp
// at the end.
newFileName = fileName + HIPHEN
+ getDate(System.currentTimeMillis(), "yyyyMMdd");
}
return logFile.getParent() + File.separator + newFileName;
}
return null;
}
public static String getDate(long milliSeconds, String dateFormat) {
// Create a DateFormatter object for displaying date in specified
// format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in
// milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
Then your class (e.g., MyClass) in which you want to do logging should contain code like this:
package com.hpcl.cel;
import org.apache.log4j.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class);
public void showLogging() {
logger.info("Operation started");
logger.info("Operation finished");
}
}
Now build and run your application. Once you will call the showLogging() method of MyClass, it will create a log file with name
logs-20160107.txt
at path
E:/Logs/MyApplication
Sample logs will be:
07-01-2016 08:58:53,189 DEBUG Operation Started
07-01-2016 08:58:53,190 DEBUG Operation Finished
来源:https://stackoverflow.com/questions/34658634/websphere-how-to-log-timing-traces-to-separate-log