Log4J

Usefull links

1.2 JavaDoc: http://logging.apache.org/log4j/1.2/apidocs/index.html

1.2 Maual Short Introduction: http://logging.apache.org/log4j/1.2/manual.html

Quick Start

Example - Uses the root logger to process logs

Main process MyApp.java

package log4J;
 
import log4J.com.foo.Bar;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

 public class MyApp {

   // Define a static logger variable so that it references the
   // Logger instance named "MyApp".
   static Logger logger = Logger.getLogger(MyApp.class);

   public static void main(String[] args) {

     // Set up a simple configuration that logs on the console.
     BasicConfigurator.configure();

     logger.info("Entering application.");
     Bar bar = new Bar();
     bar.doIt();
     logger.info("Exiting application.");
   }
 }

Source code of BasicConfigurator.configure();

public class BasicConfigurator {
  ............................
  static  public  void configure() {
    Logger root = Logger.getRootLogger();
    root.addAppender(new ConsoleAppender(
           new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
  }
  ............................

}

BasicConfigurator.configure() configures the root logger to process the logs manually

Functional process Bar.java

package log4J.com.foo;

import org.apache.log4j.Logger;

public class Bar {
  static Logger logger = Logger.getLogger(Bar.class);

  public void doIt() {
    logger.debug("Did it again!");
  }
}

The logs printed back

0 [main] INFO log4J.MyApp  - Entering application.
16 [main] DEBUG log4J.com.foo.Bar  - Did it again!
16 [main] INFO log4J.MyApp  - Exiting application.

Deeper Log4J  

Concept

        Log4j has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

 Logger

    1. Logger Hierarchy

        Named Hierarchy

                A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.

         Example belows,     

                 - "com.foo" is the parent of the logger named "com.foo.bar"

                 -  "java" is the parent of "java.util" and is the ancestor of "java.util.List"

         Root Logger

                - Root logger at the top of the Logger Hierarchy

                - it always exists,

                - it cannot retrieved by name. Invoking the class static Logger.getRootLogger method retrieves it.

                All the other loggers are instantiated and retrieved with the class static Logger.getLogger method,  This method takes the name(always the class name) of the desired logger as a parameter.  

                Source code of Logger

 

package org.apache.log4j;

  public class Logger {

    // Creation & retrieval methods:
    public static Logger getRootLogger();
    public static Logger getLogger(String name);

    // printing methods:
    public void trace(Object message);
    public void debug(Object message);
    public void info(Object message);
    public void warn(Object message);
    public void error(Object message);
    public void fatal(Object message);

    // generic printing method:
    public void log(Level l, Object message);
}

    2. Assign Level

    3. Assign Level Inheritance

    4. Selection Rule

Appender

  ... in progress

Layout 

  ... in progress

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值