log4j日志文件输出路径二个例子

发布时间:2019-09-28编辑:脚本学堂
有关log4j日志文件输出路径的二个测试例子,学习下log4j日志文件如何配置输出文件路径的,一起来了解下。

通过实例log4j日志文件输出路径的配置。

例1:
 

复制代码 代码示例:

public class Log4jManagerImpl implements LogManager {
private static final String FILE_APPENDER_ID = "D";
private static FileAppender getFileAppender() {
Appender appender = Logger.getRootLogger()
.getAppender(FILE_APPENDER_ID);
return (appender instanceof FileAppender ? (FileAppender) appender
: null);
}

public String getLogFile() {
return getFileAppender() == null ? null : getFileAppender().getFile();
}
}

例2:
 

复制代码 代码示例:
import java.io.File;
import org.apache.log4j.Appender;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
public class DcsLog {
// protected transient Log logger = LogFactory.getLog(getClass());
protected transient Logger logger = Logger.getLogger(getClass());
private static DcsLog instance;
private static final String FILE_APPENDER_ID = "R";
private DcsLog() {
}
public synchronized static DcsLog getInstance() {
if (instance == null) {
instance = new DcsLog();
}
return instance;
}
public Appender getFileAppender() {
DailyRollingFileAppender appender = (DailyRollingFileAppender) Logger
.getRootLogger().getAppender(FILE_APPENDER_ID);
return appender;
}
public File getFile() {
return new File(((DailyRollingFileAppender) getFileAppender())
.getFile());
}
public static void main(String[] args) {
DcsLog o = DcsLog.getInstance();
System.out.println(o.getFile().getName());
System.out.println(o.getFile().getAbsolutePath());
}
}