我们在WebSphere应用程序中执行了大量跟踪日志记录,并希望在单独的日志文件中分离一些时序信息。
通常我们导入:
import java.util.logging.Level;
import java.util.logging.Logger;
然后声明:
private static final Logger logger = Logger.getLogger(Myclass.class.getName());
然后记录:
logger.info("now logging...");
最简单的方法是什么?
答案 0 :(得分:-1)
您可以使用Apache日志记录服务在Websphere Application Server外部创建日志。从here下载log4j jar。这是我们使用的jar的稳定版本。将此jar添加到应用程序构建路径/类路径/库中。在 src 文件夹(空包)下添加 log4j.properties 文件。 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)。 然后使用以下代码在 com.logging 包中添加 NewLogForEachRunFileAppender 类。
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