java 如何将 log4j2 与不同的日志文件一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35573883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use log4j2 with different log files?
提问by Trong Tran
I have a log4j2.xml file that I configured as following:
我有一个配置如下的 log4j2.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">${sys:catalina.base}/logs</Property>
</Properties>
<Appenders>
<!-- console appender -->
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss} [%t] %c{1} - %msg%n"/>
</Console>
<!-- file appender -->
<RollingFile name="commons-log" fileName="${log-path}/commons.log"
filePattern="${log-path}/commons-%d{yyyy-MM-dd}.log">
<!-- log pattern -->
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
</RollingFile>
<RollingFile name="analytics-log" fileName="${log-path}/analytics.log"
filePattern="${log-path}/analytics-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.test.app" level="debug" additivity="false">
<appender-ref ref="commons-log" level="debug"/>
<appender-ref ref="analytics-log" level="warn"/>
<appender-ref ref="console-log" level="debug"/>
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
Now, I want to custom log data into commons.log and analytics.log files.
现在,我想将日志数据自定义到 commons.log 和 analytics.log 文件中。
Example in java:
java中的例子:
private static final Logger logCommon = LogManager.getLogger("commons-log");
private static final Logger logAnalytics = LogManager.getLogger("analytics-log");
And I want to custom write log to each file when I need, example:
我想在需要时自定义写入每个文件的日志,例如:
logCommons.info ("Need it save into commons.log file");
logAnalytics.info ("Only save into analytics.log file");
Question:How can I do that? Please help me fix all my issues from log4j2.xml file and in Java code examples.
问题:我该怎么做?请帮助我解决 log4j2.xml 文件和 Java 代码示例中的所有问题。
Thanks!
谢谢!
回答by awsome
Try it as below, You are writing debug logs to two files in the same logger
尝试如下,您正在将调试日志写入同一记录器中的两个文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">${sys:catalina.base}/logs</Property>
</Properties>
<Appenders>
<!-- console appender -->
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss} [%t] %c{1} - %msg%n"/>
</Console>
<!-- file appender -->
<RollingFile name="commons-log" fileName="${log-path}/commons.log"
filePattern="${log-path}/commons-%d{yyyy-MM-dd}.log">
<!-- log pattern -->
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
</RollingFile>
<RollingFile name="analytics-log" fileName="${log-path}/analytics.log"
filePattern="${log-path}/analytics-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.test.app.commons" level="debug" additivity="false">
<appender-ref ref="commons-log" level="debug"/>
<appender-ref ref="analytics-log" level="warn"/>
</Logger>
<Logger name="com.test.app.console" level="debug" additivity="false">
<appender-ref ref="console-log" level="debug"/>
<appender-ref ref="analytics-log" level="warn"/>
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
回答by Kevyn Meganck
I think the problem comes from the fact that you have all your appenders ref in one logger.
我认为问题来自这样一个事实,即您将所有 appender ref 都放在一个记录器中。
Also, the logger name filters the log entries by the name you gave to the Logger in your java code.
此外,记录器名称根据您在 Java 代码中为记录器提供的名称过滤日志条目。
You used in your example:
您在示例中使用了:
private static final Logger logCommon = LogManager.getLogger("commons-log");
private static final Logger logAnalytics = LogManager.getLogger("analytics-log");
So, your loggers will need to have the same names. What you can do is:
因此,您的记录器需要具有相同的名称。你可以做的是:
<Logger name="com.test.app" level="debug" additivity="false">
<appender-ref ref="console-log" level="debug"/>
<!-- you can put other appender references here if you want to log you
app logs in a specific file -->
</Logger>
<Logger name="commons-log" level="debug" additivity="false">
<appender-ref ref="commons-log" level="debug"/>>
</Logger>
<Logger name="analytics-log" level="debug" additivity="false">
<appender-ref ref="analytics-log" level="warn"/>
</Logger>
This way, it'll split your logs into different files based on the loggers names.
这样,它会根据记录器名称将您的日志拆分为不同的文件。
Hope it helps!
希望能帮助到你!
回答by randombee
You can configure a logger like this:
您可以像这样配置记录器:
String log4jfile = topDir + "log4j2.xml";
PropertyConfigurator.configure(log4jfile);
where log4jfile is the path to the configuration file you want to use. Without doing this, it will just have the default behaviour.
其中 log4jfile 是您要使用的配置文件的路径。如果不这样做,它将只具有默认行为。