How to create multiple log files in log4j2 using property file?(如何使用属性文件在log4j2中创建多个日志文件?)
问题描述
我正在使用.Property文件在特定路径下创建日志文件,但我正在使用该文件创建单个文件。以下是我的物业文件代码: status = error
dest = err
name = PropertiesConfig
property.filepath= /ap
property.filename =${filepath}/ai.log
property.filename1 =${filepath}/file1.log
property.filename2 =${filepath}/file2.log
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.file.type = RollingFile
appender.file.name = RootFile1
appender.file.fileName = ${filename}
appender.file.filePattern = ${filepath}/%d{yyyyMMdd}/cpl.mw.%d{yyyyMMdd}.%i.log.gz
appender.file.layout.type = PatternLayout
#appender.file.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.file.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.file.policies.type = Policies
appender.file.policies.time.type = TimeBasedTriggeringPolicy
appender.file.policies.time.interval = 1
appender.file.policies.time.modulate = true
appender.file.policies.size.type = SizeBasedTriggeringPolicy
appender.file.policies.size.size=50MB
appender.file.strategy.type = DefaultRolloverStrategy
appender.file.strategy.max = 20
appender.rolling1.type = RollingFile
appender.rolling1.name = RollingFile1
appender.rolling1.fileName = ${filename1}
appender.rolling1.filePattern = ${filepath}/%d{yyyyMMdd}/cpl.mw.%d{yyyyMMdd}.%i.log.gz
appender.rolling1.layout.type = PatternLayout
#appender.rolling1.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling1.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling1.policies.type = Policies
appender.rolling1.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling1.policies.time.interval = 1
appender.rolling1.policies.time.modulate = true
appender.rolling1.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling1.policies.size.size=50MB
appender.rolling1.strategy.type = DefaultRolloverStrategy
appender.rolling1.strategy.max = 20
appender.rolling2.type = RollingFile
appender.rolling2.name = RollingFile2
appender.rolling2.fileName = ${filename2}
appender.rolling2.filePattern = ${filepath}/%d{yyyyMMdd}/cpl.mw.%d{yyyyMMdd}.%i.log.gz
appender.rolling2.layout.type = PatternLayout
appender.rolling2.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling2.policies.type = Policies
appender.rolling2.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling2.policies.time.interval = 1
appender.rolling2.policies.time.modulate = true
appender.rolling2.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling2.policies.size.size=50MB
appender.rolling2.strategy.type = DefaultRolloverStrategy
appender.rolling2.strategy.max = 20
loggers
logger.name=file1
logger.level = debug
logger.additivity = true
logger.appenderRefs = rolling1,stdout
logger.appenderRefs.level = debug
logger.appenderRef.rolling1.ref = RollingFile1
logger.appenderRef.stdout.ref = STDOUT
logger.name=file2
logger.level = debug
logger.additivity = true
logger.appenderRefs = rolling2
logger.appenderRefs.level = debug
logger.appenderRef.rolling2.ref = RollingFile2
上面的代码是我的xyz.properties文件,我想使用这个配置文件创建多个日志。我的属性配置文件创建多个文件,但不在日志文件中放入不同的不同日志。我使用以下代码在Java中调用:
private static final Logger logCommon = LogManager.getLogger("file1");
private static final Logger logAnalytics = LogManager.getLogger("file2");
public static void main(String[] args) throws Exception {
logCommon.info ("file1 Need it save into commons.log file");
logAnalytics.info ("file2 Only save into analytics.log file");
}
2018-01-22 12:52:23 INFO file2:301 - file2 Only save into analytics.log file
两个日志文件中都放入日志。
我参考以下链接:
Log4j2: Dynamic creation of log files for multiple logs
Wildcard pattern for RoutingAppender of Log4j2
How to write different logs in different files with log4j2 (MDC in xml)?
http://logging.apache.org/log4j/2.x/manual/extending.html
我引用了以下链接,但未获得任何解决方案:
How to implement multi file appender in log4j2
How to avoid multiple log files in slf4j logging?
log4j2 - generate application specific log files in weblogic
How to create multiple log files of different content with log4j
enter link description here
log4j2 KeyValuePair for .properties file
How to Create a Custom Appender in log4j2?
Configuring async loggers in log4j2.properties
How to create custom RewritePolicy in log4j2?
How to set the log level on a class in log4j2 properties
Increasing file indexing on OnStartupTriggeringPolicy in log4j2
how to configure log file path to current working target directory in log4j2 xml format
How to configure RestTemplate debug logging in log4j2 xml
Creating multiple log files in iPhone app
Log4j2 using multiple appender and logger
How to create multiple log file programatically in log4j2?
How to create multiple log file programatically in log4j2?
How to specify log file in log4j2
Log separate log levels to separate files in log4j2 properties file
Unable to create multiple log files based on the ThreadContext map values using routing appender in log4j2
How to create Custom Rolling File appender in log4j2 - customized file name
Different log files for multiple threads using log4j2
Log4j2 YAML generate multiple log files issue
How to create multiple log files using log4j
Log4j2 - Overriding log file programmatically
How to create a rolling file appender plugin in log4j2
推荐答案
请为这两个命名的记录器设置additivity=false
。
另外,添加一个根记录器配置,以告知Log4j2将日志记录调用发送到"file1"和"file2"记录器之外的其他位置:
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT
另外,为您的命名记录器提供唯一的前缀:
logger.aaa.name = file1
logger.aaa.additivity = false
logger.aaa.level = debug
logger.aaa.appenderRef.rrr.ref = RollingFile1
logger.bbb.name = file2
logger.bbb.additivity = false
logger.bbb.level = debug
logger.bbb.appenderRef.sss.ref = RollingFile2
这篇关于如何使用属性文件在log4j2中创建多个日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用属性文件在log4j2中创建多个日志文件?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01