[log4j] log4j configuration file

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


When Log4j starts it will locate all the ConfigurationFactory plugins and arrange then in weighted order from highest to lowest. As delivered, Log4j contains two ConfigurationFactory implementations, one for JSON and one for XML.

  1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using theConfigurationFactory that matches the file extension.
  2. If no system property is set the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  3. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  4. If a test file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  5. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  6. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

An example application named MyApp that uses log4j can be used to illustrate how this is done.

    import com.foo.Bar;
     
    // Import log4j classes.
    import org.apache.logging.log4j.Logger;
     
    public class MyApp {
     
    // Define a static logger variable so that it references the
    // Logger instance named "MyApp".
    Logger logger = LogManager.getLogger(MyApp.class.getName());
     
    public static void main(String[] args) {
     
    // Set up a simple configuration that logs on the console.
     
    logger.trace("Entering application.");
    Bar bar = new Bar();
    if (!bar.doIt() {
    logger.error("Didn't do it.");
    }
    logger.trace("Exiting application."); }
    }

MyApp begins by importing log4j related classes. It then defines a static logger variable with the nameMyApp which happens to be the fully qualified name of the class.

MyApp uses the Bar class defined in the packagecom.foo.


  
  
  1. package com.foo;
  2. import org.apache.logging.log4j.Logger;
  3.  
  4. public class Bar {
  5. static Logger logger = LogManager.getLogger(Bar.class.getName());
  6.  
  7. public boolean doIt() {
  8. logger.entry();
  9. logger.error("Did it again!");
  10. return logger.exit(false);
  11. }
  12. }

运行时:

$ java  -Dlog4j.configurationFile=src/main/java/resources/log4j2.xml com.foo.Bar.MyApp

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - [example] %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="trace">     <!--> error, info, trace, default is error-->
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值