在运行时定义log4j文件位置

时间:2013-08-23 19:13:28

标签: tomcat logging log4j localhost

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

但我想在运行时

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

如何在localhost上定义运行时日志的位置?

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

1 个答案:

答案 0 :(得分:0)

尝试替换此行:

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

通过这个:

String targetLog = System.getProperty("catalina.base")  + "/logs/ElectronicArena/ea.log";