您可以使用Apache日誌記錄服務在Websphere Application Server之外創建日誌。從here下載log4j jar。這是我們使用的一個穩定版本的jar。將此jar添加到您的應用程序構建路徑/類路徑/庫。添加log4j.properties文件在您的src文件夾(空包)。 log4j.properties將包含以下行:
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
可以更改日誌級別爲每環境,而不是DEBUG(生產中應使用INFO或ERROR)。 然後添加一個類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());
}
}
然後你的類(如MyClass的),要在其中做記錄應該包含這樣的代碼:
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");
}
}
現在建立並運行應用程序。一旦你將調用的MyClass showLogging()方法,它會創建一個日誌文件名稱爲
日誌-20160107.txt
在路徑
E:/日誌/所有MyApplication
抽樣日誌將是:
07-01-2016 08:58:53,189 DEBUG Operation Started
07-01-2016 08:58:53,190 DEBUG Operation Finished