Log4j 2.3简易教程 - log4j2.xml

本文档详细介绍了如何在Log4j 2.3版本中配置log4j2.xml文件。当缺少配置文件时,系统默认仅输出错误级别及以上信息到控制台。配置文件主要包含Appender如ConsoleAppender和FileAppender的设定,如layout、filter、fileName、append等属性。遵循官方示例,配置log4j2.xml并将其置于src目录下,可以实现自定义的日志记录和输出。使用Log4j涉及三个步骤:配置xml文件、创建Log对象及调用相应方法。
摘要由CSDN通过智能技术生成

Log4j版本:2.3
操作系统:Ubuntu 13.04
JDK版本:1.7.0_75

小例子:
在Main类中,使用log4j日志功能。

package main;

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

public class Main  {
    public static void main(String args[]){
        Logger Log = LogManager.getLogger(Main.class.getName());
        Log.info("this is the info");
        Log.warn("this is the warn info");
        Log.error("this is the error info");
        Log.fatal("this is the fatal info");
        Log.trace("enter Main.test()");
        //new Main().test();
        Log.trace("exit Main.test()");
    }
}

如果我直接这样运行的话,输出结果为:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
22:22:01.774 [main] ERROR main.Main - this is the error info
22:22:01.775 [main] FATAL main.Main - this is the fatal info
最上面是一条错误信息,说没有log4j2配置文件,然后使用了默认的配置,日志仅仅把error及以上级别的信息输出到控制台。

然后在官网上发现,log4j2配置有四种方式

http://logging.apache.org/log4j/2.x/manual/configuration.html

  • Through a configuration file written in XML, JSON, or YAML.
  • Programmatically, by creating a ConfigurationFactory and Configuration implementation.
  • Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.
  • Programmatically, by calling methods on the internal Logger class.

我使用的是第一种方法,即配置log4j2.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"> <!-- 这个status是控制系统信息的输出级别 -->
  <Appenders>
    <Console name="Console" target="SYSTEM_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值