Log4j2:动态创建多个日志的日志文件 [英] Log4j2: Dynamic creation of log files for multiple logs

查看:378
本文介绍了Log4j2:动态创建多个日志的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个可以拥有模块的系统(将它们视为插件),其中每个模块都有自己的专用日志。

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.

I我想使用log4j2项目进行日志记录,但我似乎对文件appender有些麻烦。

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).

通过阅读有关appender的文档,我发现了 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 。更长的例子是常见问题解答,这些stackoverflow问题可能有趣的是:
Log4j2的RoutingAppender的通配符模式
如何写使用log4j2(MDC in xml)在不同文件中记录不同的日志?

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 Log4j2 and 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.

但是,你真的需要它才能成为这种动态吗?如果你事先知道你有什么插件,你可以为每个插件声明一个记录器(使用插件的包名是执行此操作的常用方法),并将每个这样的记录器映射到单独的appender。

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:动态创建多个日志的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆