gpt4 book ai didi

java - 独立登录多个文件夹中的多个文件-log4j JAVA

转载 作者:行者123 更新时间:2023-12-02 10:59:50 27 4
gpt4 key购买 nike

我正在开发一个多线程应用程序,我想在其中使用 log4j 在不同位置进行日志记录。

场景:

Folder1 ---> log1.log
                    log2.log
                    log3.log

Folder2 ---> log1.log
                    log2.log
                    log3.log

我希望我的记录器能够独立登录文件。

到目前为止,我已经创建了 3 个 RollingFileAppenders ,对应于文件夹 1 的 3 个记录器,但问题是“我将拥有大约 100 个文件夹,我不想为它们编写 Appender 和记录器”在我的 log4j.xml 文件中”。

<appender name="logfile1" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="Folder1/log1.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>

<appender name="logfile2" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="Folder1/log2.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>

<appender name="logfile3" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="Folder1/log3.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>

<logger name="Logger1" additivity="false">
<level value="INFO" />
<appender-ref ref="logfile1" />
</logger>

<logger name="Logger2" additivity="false">
<level value="INFO" />
<appender-ref ref="logfile2" />
</logger>

<logger name="Logger3" additivity="false">
<level value="INFO" />
<appender-ref ref="logfile3" />
</logger>

文件夹将在运行时创建

我的方法:
我想根据我的文件夹名称实例化我的记录器并将它们保存在 HashMap 中,例如

HashMap<FolderName, HashMap<LoggerType, LoggerObject>>

FolderName: String
LoggerType: it will be an Enum of log1, log2 and log3
LoggerObject: It will a log4j Logger object

因此,我将提供一种方法来获取 LoggerObject,例如

public Logger getLoggerObject(String folderName, LoggerType loggerType){
\\logic
return loggerObject;
}

Logging.getLoggerObject("Folder1", LoggerType.log1).info("I am writing to log1.log file in Folder1");


问题:
1. 我无法在运行时配置 LoggerObjects。帮我做那件事!
2. 如何为多个文件夹位置和多个日志文件配置记录器?

最佳答案

既然您说您愿意切换到 log4j2,我建议您这样做,然后使用 RoutingAppender类似于 log4j2 FAQ regarding dynamically writing to separate log files 中的示例.

下面是一些示例代码,说明如何使用 ThreadContext 动态更改文件夹和日志文件:

首先我们创建 2 个实现 Runnable 的类:

package runners;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

public class Runner1 implements Runnable{
private static final Logger log = LogManager.getLogger();

public void run() {
//Set up the context before getting logger
ThreadContext.put("logFolder", "Folder1");
ThreadContext.put("logFileName", "log1");

//Generate some logs
log.info("here's the first thread");

//Wait a while so that threads interleave
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

//Generate more logs
log.debug("some debug in first thread");
log.info("finishing first thread");

}
}

package runners;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

public class Runner2 implements Runnable{
private static final Logger log = LogManager.getLogger();

public void run() {
//Set up the context before getting logger
ThreadContext.put("logFolder", "Folder2");
ThreadContext.put("logFileName", "log2");

//Generate some logs
log.info("here's the second thread");
log.debug("some debug in second thread");

}
}

现在是一个用于创建和启动 2 个线程的基本 Controller 类:

package runners;

public class Controller {

public static void main(String[] args) {
Thread t1 = new Thread(new Runner1());
Thread t2 = new Thread(new Runner2());
t1.start();
t2.start();
}

}

最后我们需要配置log4j2:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Routing name="MyRoutingAppender">
<Routes pattern="$${ctx:logFolder}-$${ctx:logFileName}">
<Route>
<File fileName="logs/${ctx:logFolder}/${ctx:logFileName}.log"
name="appender-${ctx:logFolder}-${ctx:logFileName}">
<PatternLayout>
<Pattern>[%date{ISO8601}][%-5level][%t] %m%n</Pattern>
</PatternLayout>
</File>
</Route>
</Routes>
</Routing>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%date{ISO8601}][%-5level][%t] %m%n" />
</Console>

</Appenders>
<Loggers>
<Logger name="runners" level="TRACE" additivity="false">
<AppenderRef ref="STDOUT" />
<AppenderRef ref="MyRoutingAppender" />
</Logger>
<Root level="WARN">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>

运行Controller后,您将看到生成了两个文件夹,每个文件夹中都有一个文件:

enter image description here

第一个线程的日志位于folder1 > log1中,第二个线程的日志位于folder2 > log2中

请注意,我使用了基本文件附加程序,但您可以在路由附加程序中交换不同的附加程序以满足您的需求。

希望这有助于您入门。

关于java - 独立登录多个文件夹中的多个文件-log4j JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51419053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com