使用XML文件(log4j2.xml)配置Log4j 2 [英] Using a XML File (log4j2.xml) to configure Log4j 2

查看:225
本文介绍了使用XML文件(log4j2.xml)配置Log4j 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的Log4J 2 - Java日志记录框架。一切都工作正常,但是我尝试一小时后加载自定义配置文件来配置日志记录(如日志级别)。



这是我的log4j2.xml: p>

 <?xml version =1.0encoding =UTF-8?> 
< configuration status =OFF>
< appenders>
<控制台名称=控制台target =SYSTEM_OUT>
< PatternLayout pattern =%d {HH:mm:ss} [%t]%-5level%logger {36} - %msg%n/>
< / Console>
< / appenders>
< loggers>
< root level =error>
< appender-ref ref =控制台/>
< / root>
< / loggers>
< / configuration>

我尝试过以下操作,但无效:




  • 移动log4j2.xml文件,使其位于默认包中。

  • 将log4j2.xml文件移动到项目的任何位置

  • 将log4j2.xml文件命名为log4j.xml

  • 在项目中创建一个文件夹,将您的log4j2.xml文件放在那里,并将该文件夹添加到运行时classpath



由于官方网站无法帮助我,希望您能帮助我让Log4j 2使用我的配置文件。

解决方案

重要提示:确保配置文件的名称为 log4j2.xml (注意 2 在该期间之前)而不是 log4j.xml



我的猜测是没有发生因为没有使用错误级别记录。您可能想尝试添加另一个记录器:

 < logger name =com.foo.Barlevel =迹> 
< appender-ref ref =控制台/>
< / logger>

看起来像配置部分可能是一个很好的资源。



要进一步阐述,你指定一个具有级别的记录器设置为错误:

 < root level =error> 
< appender-ref ref =控制台/>
< / root>

这意味着只有使用Level.ERROR记录的消息才会显示在日志中。添加限制级别较小的记录器将允许在日志中显示更多消息。我建议您查看手册的架构部分(如果您向下滚动页面,您将看到说明记录级别的表)。或者,您可以将根记录器的级别更改为跟踪(而不是添加新的记录器)



给定您指定的配置,执行下面的代码会产生类似 13:27:50.244 [main] ERROR com.example.Log4j2Tester - 测试错误级别

 code> package com.example; 

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

public static void main(String [] args){
LOG.error(testing ERROR level);

//如果将根记录器的级别更改为trace
//那么您还将看到像
// 13:27:50.244 [main] TRACE com.example.Log4j2Tester - 退出应用程序
LOG.trace(退出应用程序);
}
}


I want to use the new Log4J 2 - Java Logging Framework. Everything work fine, but I tried since a hour to load a custom configuration file to configure the logging (like log level).

This is my log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
  <appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </appenders>
  <loggers>
    <root level="error">
      <appender-ref ref="Console"/>
    </root>
  </loggers>
</configuration>

I tried the following, but nothing works:

  • Move the log4j2.xml file so it's located in the default package.
  • Move the log4j2.xml file anywhere in the project
  • Name the log4j2.xml file as "log4j.xml"
  • Create a folder in your project, place your log4j2.xml file there and add that folder to your runtime classpath

Since the official website can't help me, I hope you can help me get Log4j 2 working with my configuration file.

解决方案

Important: make sure the name of the configuration file is log4j2.xml (note the 2 before the period) as opposed to log4j.xml

My guess is nothing's happening because nothing is logged using the error level. You may want to try adding another logger like so:

<logger name="com.foo.Bar" level="trace">
  <appender-ref ref="Console"/>
</logger>

Looks like the Configuration section might be a good resource.

To elaborate further, you're specifying a logger with the level set to "error":

<root level="error">
  <appender-ref ref="Console"/>
</root>

This means that only the messages logged using Level.ERROR will show up in the log. Adding a logger with a less restrictive level will allow for more messages to appear in the log. I recommend taking a look at the Architecture section of the manual (if you scroll down the page you'll see the table that explains logging levels). Alternatively, you could just change the level of the root logger to trace (instead of adding a new logger)

Given the configuration you specified, executing the code below yields something like 13:27:50.244 [main] ERROR com.example.Log4j2Tester - testing ERROR level

package com.example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

    public static void main(String[] args) {
        LOG.error("testing ERROR level");

//if you change the level of root logger to 'trace'
//then you'll also see something like
//    13:27:50.244 [main] TRACE com.example.Log4j2Tester - exiting application
        LOG.trace("exiting application");
    }
}

这篇关于使用XML文件(log4j2.xml)配置Log4j 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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