简体   繁体   English

如何配置Log4J2

[英]How to configure Log4J2

I have spent a couple hours now trying to figure this out and am at a complete loss. 我现在花了几个小时试图解决这个问题并完全失败。 I find the new configuration process unnecessarily complex. 我发现新的配置过程不必要地复杂。

I have a Servlet with a web.xml file with the following: 我有一个带有web.xml文件的Servlet,其中包含以下内容:

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>file:///etc/myApp/log4j2.xml</param-value>
</context-param>

It doesn't seem to have any effect. 它似乎没有任何影响。 I am using Tomcat 7.0.42, and I have removed all references to log4j*.jar in the catalina.properties file. 我使用的是Tomcat 7.0.42,我已经删除了catalina.properties文件中对log4j * .jar的所有引用。 The logs in my app are still being sent, but they are just being sent to System.out with none of the formatting I specified. 我的应用程序中的日志仍在发送,但它们只是被发送到System.out而没有我指定的格式。

So, I tried to just do it by hand on my own: 所以,我试着亲自手工完成:

InputStream in =
    new FileInputStream(new File("/etc/myApp/log4j2.xml"));
ConfigurationSource source = new ConfigurationSource(in);
ConfigurationFactory factory = new XMLConfigurationFactory();
Configuration configuration = factory.getConfiguration(source);
LoggerContext context = (LoggerContext) LogManager.getContext();
context.start(configuration);
context.updateLoggers();

Logger.getLogger("Test").log(Level.INFO, "This is a logging message.");

First, this seems entirely convoluted. 首先,这似乎完全令人费解。 Clearly there exists some code that will search for different files and assume file types based on their extensions via the "log4jConfiguration" property, so why isn't there a LogManger.reconfigure(String) that has the same effect? 显然,存在一些代码,它们将通过“log4jConfiguration”属性搜索不同的文件并根据它们的扩展来假设文件类型,那么为什么没有具有相同效果的LogManger.reconfigure(String)呢?

Second, this has no effect either. 其次,这也没有效果。 Again, the log is printed to System.out, and none of the requested formatting is being done. 同样,日志将打印到System.out,并且没有执行任何请求的格式。

Here is the contents of my log4j2.xml: 这是我的log4j2.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>

    <File name="LogFile" fileName="/var/log/myApp/myApp.log">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %p %c{1.} [%t] %m%n"/>
    </File>
  </Appenders>
  <Loggers>
    <Root level="DEBUG">
      <AppenderRef ref="Console" level="DEBUG"/>
      <AppenderRef ref="LogFile" level="DEBUG"/>
    </Root>
  </Loggers>
</Configuration>

The output in both cases comes out something like: 两种情况下的输出都是这样的:

Dec 03, 2013 6:06:45 PM test.Test main
INFO: This is a logging message.

Thanks in advance, 提前致谢,

John 约翰

EDIT: This is actually working. 编辑:这实际上是有效的。 It appears that I was missing, "log4j-jcl-2.0-beta9.jar". 看来我失踪了,“log4j-jcl-2.0-beta9.jar”。 Remko Popma's answer works as well, if the "context-param" above is not working. 如果上面的“上下文参数”不起作用,Remko Popma的答案也会起作用。

There are many possible scenarios with webapp logging, which makes configuration more complex than the ideal. webapp日志记录有许多可能的情况,这使得配置比理想情况更复杂。 Putting the log4j2 jars and the config file in your webapp's classpath should work. 将log4j2 jar和配置文件放在webapp的类路径中应该可以正常工作。 Did you see the manual page for log4j2 use in web apps? 您是否在Web应用程序中看到了log4j2的手册页? If the issue remains, please file a log4j2 jira ticket. 如果问题仍然存在,请提交log4j2 jira票。

The LoggerContext already has a method setConfigLocation that takes in a URI as parameter. LoggerContext已经有一个方法setConfigLocation ,它接受一个URI作为参数。 It baffles me why there isn't a similar method that takes an InputStream as parameter. 令我感到困惑的是,为什么没有类似的方法将InputStream作为参数。 Instead we have to deal with the most convoluted and messy way of loading our log4j2.xml that is not on the classpath. 相反,我们必须处理加载我们不在类路径上的log4j2.xml的最复杂和混乱的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM