我创建了单独的简单日志类:log.java) 例如,
import org.apache.log4j.LogManager;
public class Logs {
private static final org.apache.log4j.Logger logger= LogManager.getLogger(Logs.class);
public static org.apache.log4j.Logger Log() {
return logger;
}
}
并使用以下log4j2.xml文件
<Configuration status="INFO">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="${log-path}/Log${date:yyyy-MM-dd-HH-mm-ss}.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile" />
</Root>
</Loggers>
</Configuration>
问题:我想将日志文件名称testbest.log传递给我的日志类,以便在创建日志时,日志文件名称为testbest.log而不是Log $ {date:yyyy-MM-dd-HH-毫米-β}的.log
我怎样才能做到这一点?
答案 0 :(得分:1)
的log4j.xml
<Configuration status="INFO">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="${log-path}/${sys:log-file-name}" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile" />
</Root>
</Loggers>
</Configuration>
启动时可以将文件名指定为VM参数
java -Dlog-file-name="testbest.log"
或者您可以通过编程方式将其定义为系统属性
System.setProperty("log-file-name", "testbest.log");