配置LOG4J(log4j-1.2.17)

weixin_30639719發表於2020-04-05

1
配置log4j
配置log4j的CLASSPATH環境變數一定要直接指定,
vim /etc/profile
export CLASSPATH=$JAVA_HOME/lib/log4j-1.2.17.jar:$JAVA_HOME/lib:$JAVA_HOME/jre/lib。

編譯 javac Hello.java ,生成Hello.class,並且生成的Hello.class要放到CLASSPATH裡,才能使用java Hello來執行。
執行 java Hello

2  http://bbs.csdn.net/topics/310256270

 Internal Error: File /WEB-INF/web.xml not found  



3  log4j.properties配置示例

  log4j.rootLogger=debug, stdout, R
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

  # Pattern to output the caller's file name and line number.
  log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

  log4j.appender.R=org.apache.log4j.RollingFileAppender
  log4j.appender.R.File=example.log
  log4j.appender.R.MaxFileSize= 100KB

  # Keep one backup file
  log4j.appender.R.MaxBackupIndex=1

  log4j.appender.R.layout=org.apache.log4j.PatternLayout
  log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n        

使用log4j的小程式:
  import com.foo.Bar;
  import org.apache.log4j.Logger;
  import org.apache.log4j.PropertyConfigurator;
  public class MyApp {
    static Logger logger = Logger.getLogger(MyApp.class.getName());
    public static void main(String[] args) {
      // BasicConfigurator replaced with PropertyConfigurator.
      PropertyConfigurator.configure(args[0]);
      logger.info("Entering application.");
      Bar bar = new Bar();
      bar.doIt();
      logger.info("Exiting application.");
    }
  }

轉載於:https://www.cnblogs.com/slips/p/3411585.html

相關文章