我正在使用.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调用它: / p>
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
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
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
答案 0 :(得分:1)
请为两个命名记录器设置additivity=false
。
另外,添加一个root logger配置告诉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
答案 1 :(得分:0)
使用以下配置并在想要使用完全限定路径时调用addLogger()
public static void addLogger(String parentPath) throws SecurityException, IOException {
try {
InputStream in = ConfiguratorHelper.class.getResourceAsStream("/resources/mylogging.properties");
LogManager.getLogManager().readConfiguration(in);
} catch (SecurityException | IOException e1) {
e1.printStackTrace();
}
logger.setLevel(Level.FINE);
logger.addHandler(new ConsoleHandler());
logger.addHandler(new MyHandler());
try {
//FileHandler file name with max size and number of log files limit
Handler fileHandler = new FileHandler(path, 1000000, 10);
fileHandler.setFormatter(new MyFormatter());
fileHandler.setFilter(new MyFilter());
logger.addHandler(fileHandler);
logger.log(Level.CONFIG, "Config data");
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
}
public class MyFilter implements Filter {
@Override
public boolean isLoggable(LogRecord log) {
//don't log CONFIG logs in file
if(log.getLevel() == Level.CONFIG) return false;
return true;
}
}
public class MyFormatter extends Formatter {
@Override
public String format(LogRecord record) {
return record.getThreadID()+"::"+record.getSourceClassName()+"::"
+record.getSourceMethodName()+"::"
+new Date(record.getMillis())+"::"
+record.getMessage()+"\n";
}
}
public class MyHandler extends StreamHandler {
@Override
public void publish(LogRecord record) {
//add own logic to publish
super.publish(record);
}
@Override
public void flush() {
super.flush();
}
@Override
public void close() throws SecurityException {
super.close();
}
}