You can't 销毁/清理 Appender 实例。但是您可以使用 Log4j2 工具箱改进您的解决方案。
在 Log4j 中将事物路由到动态文件是可能的,但大多数情况下,文件中的 Marker 就足够了,或者至少是更复杂路由的起点。
请阅读其documentation。特别是最后一段对您的情况很重要:
在使用标记时必须考虑一些关于标记的重要规则。
- 标记必须是唯一的。它们按名称永久注册,因此应注意确保您的应用程序中使用的标记与应用程序依赖项中的标记不同,除非这是需要的。
- 可以动态添加或删除父标记。但是,这样做相当昂贵。相反,建议在第一次获取标记时识别父母,如上例所示。具体来说,set 方法在单个操作中替换所有标记,而 add 和 remove 一次仅作用于单个标记。
- 评估具有多个祖先的标记比没有父母的标记要昂贵得多。例如,在一组测试中,评估标记是否与其祖父母匹配的时间是评估标记本身的 3 倍。尽管如此,与解析调用者类名或行号相比,评估 Markers 成本较低。
然后,您可以在配置中预期模式的地方使用标记,如下所示:$${marker:}。虽然我没有在文件名中使用它并且怀疑它是否有效,但您可以基于the Marker 使用create routing。
我使用了这个简单的测试脚本,请注意使用为扫描仪的每一行创建的Marker。在您的情况下,它将由 config 或 Servlet 输入或类似创建。
package toTest;
import java.util.Scanner;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
public class TestMe {
private final Logger myOneAndOnlyLogger = LogManager.getLogger("MyCentralName");
public static void main(String[] args) {
new TestMe().doMyThing();
}
private void doMyThing() {
Scanner input = new Scanner(System.in);
String line = "";
while(!line.equals("QUIT")) {
System.out.println("Line: ");
line = input.nextLine();
Marker forThisRound = MarkerManager.getMarker(line);
myOneAndOnlyLogger.log(Level.ERROR, forThisRound, "1");
myOneAndOnlyLogger.log(Level.ERROR, forThisRound, "2");
System.out.println("Line done.");
}
}
}
还有这个log4j2.properties(我手头的滚动文件示例,模式中有标记):
status = error
name = MarkerExample
#Make sure to change log file path as per your need
property.filename = /tmp/java/marker.log
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = /tmp/java/debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} $${marker:} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MBONE
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package structure as per your application
logger.rolling.name = MyCentralName
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile