问题描述
我目前正在创建一个可以拥有模块(将它们视为插件)的系统,其中每个模块都可以拥有自己的专用日志.
I am currently creating a system that can have modules (think of them as plugins), where each one of them can have their own log, dedicated.
我想使用 log4j2 项目进行日志记录,但文件附加程序似乎有些问题.
I would like to use the log4j2 project for logging, but I seem to have some trouble with the file appenders.
主项目(模块加载器和整个事物的核心")应该有自己的日志文件,而模块应该有自己的日志文件(如mod_XXXXXXXX.log
).
The main project (the module loader and "core" of the whole thing) should have its own log file, while the modules should have their own (like mod_XXXXXXXX.log
).
通过阅读关于附加程序的文档,我发现了 FileAppender
类,我打算使用它.直到我发现我不能简单地将 appender 添加到由 LogManager.getLog()
创建的默认记录器中.
By reading the documentation about the appenders I discovered the FileAppender
class, and I was going to use that. Until I found out that I can't just simple add the appender to the default logger created by LogManager.getLog()
.
LogManager 返回的记录器与 Logger
接口不同.
The logger returned by the LogManager is a different logger than the Logger
interface.
即使搜索也没有给我任何接近的解决方案,我发现的只是 xml 配置中的预定义文件日志 - 这不是我想要的.
Even searching did not give me any near solution, all I found was predefined file logs in the xml configuration - which is not what I want.
感谢您的阅读;即使是最轻微的线索也欢迎:)
Thank you for reading; even the slightest clue is welcome :)
推荐答案
如果你真的需要动态确定日志文件,看看 Log4J2 RoutingAppender.一个更长的例子在 FAQ 中,这些 stackoverflow 问题可能感兴趣:Log4j2 的 RoutingAppender 的通配符模式和 如何写不同的日志在不同的文件中使用 log4j2(xml 中的 MDC)?
if you really need to determine the log file dynamically, take a look at the Log4J2 RoutingAppender. A longer example is in the FAQ and these stackoverflow questions may be of interest:Wildcard pattern for RoutingAppender of Log4j2and How to write different logs in different files with log4j2 (MDC in xml)?
请注意,您需要在 ThreadContext
映射中设置值,RoutingAppender 使用该映射来决定将日志事件路由到哪个 appender.这意味着每次您的代码进入不同的插件时,您都需要在 ThreadContext 映射中放入一些值.
Note that you need to set values in the ThreadContext
map that the RoutingAppender uses to decide which appender to route the log event to. This means that you would need to put some value in the ThreadContext map every time your code enters a different plugin.
但是,您真的需要它如此动态吗?如果你事先知道你有哪些插件,你可以为每个插件声明一个记录器(使用插件的包名是一种常见的方法),并将每个这样的记录器映射到一个单独的附加器.
However, do you really need it to be this dynamic? If you know in advance what plugins you have, you can just declare a logger for each plugin (using the package name of the plugin is a common way to do this), and map each such logger to a separate appender.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<File name="MyFile" fileName="logs/app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<File name="plugin1" fileName="logs/plugin1.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<File name="plugin2" fileName="logs/plugin2.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="com.mycomp.project.plugin1" level="debug">
<AppenderRef ref="plugin1" level="debug" />
</Logger>
<Logger name="com.mycomp.project.plugin2" level="debug">
<AppenderRef ref="plugin2" level="debug" />
</Logger>
<Root level="trace">
<AppenderRef ref="MyFile" level="trace" />
</Root>
</Loggers>
</Configuration>
这篇关于Log4j2:为多个日志动态创建日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!