1

我想将 log4j 的位置定义为 ${catalina.base}/logs/mylog/logdate.log

但我想在运行时做

这是我在运行时定义fileappender的代码,如果我离线运行,我在项目中获得一个新文件夹,所有日志都在那里,这很好,但是当我在服务器上运行项目时,我找不到日志.......

如何在本地主机上定义运行时日志的位置?

String targetLog = "${catalina.base}/logs/ElectronicArena/ea.log";

ConsoleAppender console = new ConsoleAppender(); // create appender
// configure the appender
String PATTERN = "%d %-5p [%c{1}] %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.INFO);
console.activateOptions();
// add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(console);
FileAppender appender = null;
try {
    appender = new FileAppender(new PatternLayout(
            "%d %-5p [%c{1}] %m%n"), targetLog, true);
    appender.setName(TAG);
    appender.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
    appender.setFile(targetLog);
    appender.setAppend(true);
    appender.setThreshold(Level.INFO);
    appender.activateOptions();
    Logger.getRootLogger().addAppender(appender);

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

出于某种原因,以声明方式编辑 不起作用:这是我的属性文件

log4j.rootLogger=INFO, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %5p [%t](%c:%L) - %m%n

log4j.rootLogger=DEBUG, Log

log4j.appender.Log=org.apache.log4j.FileAppender
log4j.appender.Log.layout=org.apache.log4j.PatternLayout

log4j.appender.Log = org.apache.log4j.DailyRollingFileAppender
log4j.additivity = false
log4j.appender.Log.File=${catalina.base}/logs/ElectronicArena/ea.log
log4j.appender.Log.MaxFileSize = 5MB
log4j.appender.Log.MaxBackupIndex = 20
# Pattern to output the caller's file name and line number.
log4j.appender.Log.layout.ConversionPattern=%d %5p [%t](%c:%L) - %m%n
4

1 回答 1

0

尝试替换此行:

String targetLog = "${catalina.base}/logs/ElectronicArena/ea.log";

通过这个:

String targetLog = System.getProperty("catalina.base")  + "/logs/ElectronicArena/ea.log";
于 2013-08-23T23:36:20.063 回答