问题描述
有什么方法可以为不同的日志级别创建单独的日志文件?
我想要的是将error记录到一个文件中,info将记录到另一个文件.
我找不到任何解决方案来在log4j2.properties中进行此操作.这是我得到的log4j2.xml,它可以正常工作.谁能帮助我在属性文件中写同样的内容?
此XML文件使用 from log4j2 faq and int
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="log-path">logs</Property> </Properties> <Appenders> <Console name="console-log" target="SYSTEM_OUT"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/> </Console> <RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log" filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log"> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log" filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log"> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </Appenders> <Loggers> <Logger name="com.mycuteblog.log4j2" level="debug" additivity="false"> <appender-ref ref="trace-log" level="debug"/> <appender-ref ref="error-log" level="error"/> <appender-ref ref="console-log" level="debug"/> </Logger> <Root level="info" additivity="false"> <AppenderRef ref="console-log"/> </Root> </Loggers> </Configuration>
P.S. - 我不想为此更改任何代码.我正在寻找专门的log4j2.properties.
预先感谢.
推荐答案
尝试阈值
ThresholdFilter ThresholdFilter 根据日志级别的消息.要获取不同的日志文件,每个appender应该具有适当的阈值过滤器.它应该是这样的东西(使用XML):
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="log-path">logs</Property> <Property name="log-pattern">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}- %msg%n"/</Property> </Properties> <Appenders> <Console name="console-log" target="SYSTEM_OUT"> <PatternLayout> <pattern>${log-pattern}</pattern> </PatternLayout> </Console> <RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log" filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log"> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/> <PatternLayout> <pattern>${log-pattern}</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log" filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log"> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>${log-pattern}</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </Appenders> ...
请注意,日志模式定义为属性,因为所有三个附录都使用相同的模式.
我不能用作属性文件的配置,我从未使用过.
您可以找到有关过滤器的更多信息此处在 thresholdfilter文档 /p>
其他推荐答案
尝试LevelLangeFilter
尝试 LevelRangeFilter :在您的appender配置
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
上线将与其他日志的错误日志分开
如果以Minlevel和Maxlevel作为信息提及,则仅考虑信息日志,而不是上述级别日志.
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
以下log4j.xml文件对我有用:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="log-path">logs</Property> </Properties> <Appenders> <Console name="console-log" target="SYSTEM_OUT"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/> </Console> <RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log" filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log"> <LevelRangeFilter minLevel="TRACE" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="info-log" fileName="${log-path}/mycuteblog-info.log" filePattern="${log-path}/mycuteblog-info-%d{yyyy-MM-dd}.log"> <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log" filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log"> <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </Appenders> <Loggers> <Root level="info" additivity="false"> <AppenderRef ref="console-log"/> <AppenderRef ref="trace-log"/> <AppenderRef ref="error-log"/> <AppenderRef ref="info-log"/> </Root> </Loggers> </Configuration>
有关更多参考,请点击此链接: https://howtodoinjava.com/log4j2/multiple-appenders/multiple-appenders/
其他推荐答案
假定log42j配置与log4j w/apache log4j Extras相同.在您的属性文件中尝试一下:
log4j.appender.mycuteblog.filter.1=org.apache.log4j.varia.LevelRangeFilter log4j.appender.mycuteblog.filter.1.levelMin=INFO log4j.appender.mycuteblog.filter.1.levelMax=INFO
问题描述
Is there any way we can create separate log files for different log levels?
All I want is to log error logs to one file and info logs to another file.
I did not find any solution to do this in log4j2.properties. Here is the log4j2.xml which I got and it works fine. Can anyone help me writing the same in properties file?
This XML file uses the method from the Log4j2 FAQ and sets level on the AppenderRef(s):
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="log-path">logs</Property> </Properties> <Appenders> <Console name="console-log" target="SYSTEM_OUT"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/> </Console> <RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log" filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log"> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log" filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log"> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </Appenders> <Loggers> <Logger name="com.mycuteblog.log4j2" level="debug" additivity="false"> <appender-ref ref="trace-log" level="debug"/> <appender-ref ref="error-log" level="error"/> <appender-ref ref="console-log" level="debug"/> </Logger> <Root level="info" additivity="false"> <AppenderRef ref="console-log"/> </Root> </Loggers> </Configuration>
P.S. - I do not want to make any code change for this. I am looking for specifically log4j2.properties.
Thanks in advance.
推荐答案
Try ThresholdFilter
The ThresholdFilter serves for filtering messages according to the log level. To get the different log files each appender should have the appropriate threshold filter. It should be something like this (with xml):
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="log-path">logs</Property> <Property name="log-pattern">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}- %msg%n"/</Property> </Properties> <Appenders> <Console name="console-log" target="SYSTEM_OUT"> <PatternLayout> <pattern>${log-pattern}</pattern> </PatternLayout> </Console> <RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log" filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log"> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/> <PatternLayout> <pattern>${log-pattern}</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log" filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log"> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>${log-pattern}</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </Appenders> ...
Pay attention, that the log pattern is defined as a property, since the same pattern is used for all three appenders.
I cannot help with configuration as a properties file, I never used it.
You can find more about filters here and on the ThresholdFilter documentation
其他推荐答案
Try LevelRangeFilter
Try LevelRangeFilter: If you are using log4j2 for logging, include this line in your Appender configuration
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
the above line seperates the error logs from other logs
If the same is mentioned with minLevel and maxLevel as INFO it considers only INFO logs but not the above level logs.
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
The below log4j.xml file worked for me:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="log-path">logs</Property> </Properties> <Appenders> <Console name="console-log" target="SYSTEM_OUT"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/> </Console> <RollingFile name="trace-log" fileName="${log-path}/mycuteblog-trace.log" filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log"> <LevelRangeFilter minLevel="TRACE" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="info-log" fileName="${log-path}/mycuteblog-info.log" filePattern="${log-path}/mycuteblog-info-%d{yyyy-MM-dd}.log"> <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="error-log" fileName="${log-path}/mycuteblog-error.log" filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log"> <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </Appenders> <Loggers> <Root level="info" additivity="false"> <AppenderRef ref="console-log"/> <AppenderRef ref="trace-log"/> <AppenderRef ref="error-log"/> <AppenderRef ref="info-log"/> </Root> </Loggers> </Configuration>
for more reference follow this link: https://howtodoinjava.com/log4j2/multiple-appenders/
其他推荐答案
Presuming the log42j config is the same as log4j w/apache log4j extras. Try this in your properties file:
log4j.appender.mycuteblog.filter.1=org.apache.log4j.varia.LevelRangeFilter log4j.appender.mycuteblog.filter.1.levelMin=INFO log4j.appender.mycuteblog.filter.1.levelMax=INFO