简体   繁体   English

Tomcat中的Log4j2

[英]Log4j2 in Tomcat

I am using Tomcat 6 and trying to switch JUL to Log4j2 (2.0 beta 3). 我正在使用Tomcat 6并尝试将JUL切换到Log4j2(2.0 beta 3)。 However, as it is quite a new project, the documentation is quite sparse and there are not many resources online yet. 但是,由于它是一个相当新的项目,文档非常稀少,并且在线资源不多。

What I want 我想要的是

  1. All events above debug level should go to a file on disk. 调试级别以上的所有事件都应该转到磁盘上的文件。
  2. Errors should go to syserr . 错误应该发送到syserr
  3. Everything from info to warn should go to sysout . 从info到warn的所有内容都应该转到sysout
  4. Marked audit logs should skip all this and go to a different file on disk. 标记的审核日志应该跳过所有这些并转到磁盘上的其他文件。

What I tried 我尝试了什么

This is my first attempt log4j2.xml file: 这是我第一次尝试log4j2.xml文件:

What should I write in configuration status ? 我应该在configuration status写什么? Where is some documentation regarding it? 关于它的一些文件在哪里?

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="info">
<appenders>
    <Console name="Console-err" target="SYSTEM_ERR">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <Console name="Console-out" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <File name="MyFile" fileName="app.log">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</appenders>
<loggers>
    <logger name="com.ecm" level="error" additivity="false">
        <appender-ref ref="Console-err" />
        <appender-ref ref="MyFile" />
    </logger>
    <root level="info">
        <appender-ref ref="Console-out" />
        <appender-ref ref="MyFile" />
    </root>
</loggers>
</configuration>

How does the logger name relate to Java classes and their loggers? 记录器名称与Java类及其记录器的关系如何?

Example code: 示例代码:

package com.ecm.backend;

public final class Foo {
   private static final Logger LOG = LogManager.getLogger(Foo.class);

   public static void bar() {
     LOG.error("this is error");
     LOG.warn("this is warning");
   }
}

When I run this, I get this in console and the log file: 当我运行它时,我在控制台和日志文件中得到它:

10:22:49.385 [http-8080-3] ERROR com.ecm.backend.Foo - this is error

Why is the warning ignored? 为什么忽略警告?

If I remove the additivity="false" , I get: 如果我删除additivity="false" ,我得到:

10:26:51.388 [http-8080-1] ERROR com.ecm.backend.Foo - this is error
10:26:51.388 [http-8080-1] ERROR com.ecm.backend.Foo - this is error

The warning is ignored again and the error is repeated (as expected). 再次忽略该警告并重复该错误(如预期的那样)。

What am I doing wrong? 我究竟做错了什么?


Under the fold: rest of the Tomcat can use JULI, I just want Log4j2 for my application. 在折叠下:Tomcat的其余部分可以使用JULI,我只想将Log4j2用于我的应用程序。

If you set configuration status="debug" you will see a lot of log4j2-internal log statements about what happens during the configuration process. 如果设置配置状态=“debug”,您将看到很多关于配置过程中发生的事情的log4j2内部日志语句。 This may be useful to trouble-shoot configuration issues. 这可能有助于解决配置问题。

I usually set configuration status="warn" so I only get notifications if there is a problem. 我通常设置配置状态=“警告”,所以我只有在出现问题时才会收到通知。

About the relation between logger names, loggers and their java class, maybe this helps: http://logging.apache.org/log4j/2.x/manual/architecture.html 关于记录器名称,记录器和它们的java类之间的关系,这可能有所帮助: http//logging.apache.org/log4j/2.x/manual/architecture.html

About the question why the warning is ignored, there is this logger config: 关于为什么忽略警告的问题,有这个记录器配置:

<logger name="com.ecm" level="error" additivity="false">

There also is a logger like this: 还有一个这样的记录器:

Logger LOG = LogManager.getLogger(com.ecm.backend.Foo.class);

The logger config "com.ecm" will process all calls from the LOG logger, but only the ones at "error" level or higher (fatal) will actually be logged. 记录器配置“com.ecm”将处理来自LOG记录器的所有调用,但实际上只记录“错误”级别或更高级别(致命)的调用。 Log events at other levels (warn, info, debug and trace) are filtered out because their level is too low. 其他级别的日志事件(警告,信息,调试和跟踪)将被过滤掉,因为它们的级别太低。

The way that additivity works is that first the named logger config ("com.ecm" in this case) decides whether the event should be filtered out or not. 可加性的工作方式是首先命名的log​​ger配置(在这种情况下为“com.ecm”)决定是否应该过滤掉事件。 Then, if additivity="true" (the default), the event is passed on to the parent logger (in this case the root logger). 然后,如果additivity =“true”(默认值),则将事件传递给父记录器(在本例中为根记录器)。 Because the "warn"-level event is already filtered out by the "com.ecm" logger config it never reaches the root logger. 因为“警告”-level事件已被“com.ecm”记录器配置过滤掉,所以它永远不会到达根记录器。

Not sure what you are trying to achieve, but if you want to send different event levels to different appenders, one way to do it may be to set levels on the appender-refs, not on the loggers: 不确定你想要实现什么,但如果你想向不同的appender发送不同的事件级别,一种方法可能是在appender-refs上设置级别,而不是在logger上:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="info">
<appenders>
    <Console name="Console-err" target="SYSTEM_ERR">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <Console name="Console-out" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <File name="MyFile" fileName="app.log">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</appenders>
<loggers>
    <logger name="com.ecm" additivity="false">
        <appender-ref ref="Console-err" level="error" />
        <appender-ref ref="MyFile" level="debug" />
    </logger>
    <root>
        <appender-ref ref="Console-out" level="info" />
        <appender-ref ref="MyFile" level="debug" />
    </root>
</loggers>
</configuration>

This will send the output of the LOG logger to MyFile and (if level is error or fatal) also to Console-err. 这会将LOG记录器的输出发送到MyFile,并且(如果级别错误或致命)也发送到Console-err。

Output from loggers not in the "com.ecm" package will be sent to MyFile and (if level is info, warn, error or fatal) also to Console-out. 不在“com.ecm”包中的记录器的输出将发送到MyFile,并且(如果级别是信息,警告,错误或致命)也将发送到Console-out。

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

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