Inheritance concept in Log4j


Target Audience:
Enterprise Java programmers, beginners in Apache Log4j tool.
Content:
This article illustrates the inheritance concept of the world’s most popular Java Logging Framework, Log4j. This blog posts explains one of the nice features of Log4j that caught my eyes.
Author: Sridhar Jammalamadaka

By the way, Log4j is an opensource tool by Apache software foundation (which means its free!). Log4j is a unanimous choice for a logging framework among developers. Log4j finds its use in stand-alone and web-based applications Java.

A practical example is given below to illustrate this concept. This examples uses files A.java, B.java, C.java, log4j.properties

Inheritance in logging using Log4j
Consider Class A, B, C. Class A is in package com, Class B, C are in package com/foo

/*
************************************************************
Index: com/A.java
************************************************************
*/

package com;
import org.apache.log4j.Logger;
public class A {

 // Give Classname.class as input to getLogger method
 private static Logger logger = Logger.getLogger(A.class);

 public A() {
  logger.info("Inside class A constructor");
  this.aMethod();
 }
 public void aMethod() {
  logger.debug("Inside aMethod()");
 }
}
/*
************************************************************
Index: com/foo/B.java
************************************************************
*/

package com.foo;
import org.apache.log4j.Logger;

public class B {
 private static Logger logger = Logger.getLogger(B.class);
 public B() {
  logger.info("Inside Class B constructor");
  this.bMethod();
 }
 public void bMethod() {
  logger.debug("Inside bMethod()");
 }
}
/*
************************************************************
Index: com/foo/C.java
************************************************************
*/

package com.foo;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.A;

public class C {
 private static Logger logger = Logger.getLogger(C.class);
 public C() {
  logger.info("Inside class C constructor");
  this.cMethod();
 }

 public void cMethod() {
  logger.debug("Inside cMethod()");
 }

 public static void main(String args[]) {
  PropertyConfigurator.configure("log4j.properties");
  A a = new A();
  B b = new B();
  C c = new C();
 }
}

Index: log4j.properties
=====================================================================
#This sets the rootLogger Level to DEBUG and adds ConsoleAppender
log4j.rootLogger=debug,CONSOLE
############ Settings for CONSOLE Appender #################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#Set the Layout for CONSOLE Appender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#Set the Conversion pattern for printing logs
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %c – %m%n

Above properties file would configure logging levels in such a way that only debug logs or above would be displayed. The logs are configured to be shown on the console or command prompt where this java program is run.

Output would be:

  18:19:01,854 INFO com.A - Inside class A constructor
  18:19:01,854 DEBUG com.A - Inside aMethod()
  18:19:01,870 INFO com.foo.B - Inside Class B constructor
  18:19:01,870 DEBUG com.foo.B - Inside bMethod()
  18:19:01,870 INFO com.foo.C - Inside class C constructor
  18:19:01,870 DEBUG com.foo.C - Inside cMethod()

If we add a line of code to our log4j.properties
log4j.logger.com.foo=off

Logging is switched off in all the classes contained in the package, com.foo, i.e., Class B, Class C . This simply means that all the log statements written in classes B, C would not appear in the output.

Output would be:

  18:23:37,478 INFO com.A - Inside class A constructor
  18:23:37,478 DEBUG com.A - Inside aMethod()

Let us say we added below code snippet instead of the previous one mentioned to our log4j.properties
log4j.logger.com=off

Let us see what the output is. Hey there is no log statement at all! We switched off logging of package com right?
Which means logging should be switched off only in the classes directly under com folder , i.e, Class A

But why did logging in Class B, Class C which fall under package com.foo also get turned off??

Now this is the inheritance concept in Log4j. Until explicitly specified not to allow inheritance, the logging properties set to a package folder would apply to all the sub packages under it. So, the logging properties applied to com package would be inherited to com.foo package too.

I hope this tutorial was of some use to you and it did not confuse you further, please comment below your feedback.

Next Steps: Download Log4j, Learn how to use Log4j


About the Author:  Sridhar Jammalamadaka is the Editor of Interview Mantra. He's a typically non-typical Software Engineer from Pune, India. He likes entrepreneurship, web technologies and Micro Controller programming. He enjoys playing cricket and piano (but rarely does these activities). Through this website, he wishes to gather a large community of aspiring engineers, entrepreneurs and professionals from all parts of the globe. You can connect with him on Facebook - http://www.facebook.com/sridhar.j



All comments of post - "Inheritance concept in Log4j":

:Haha! I'am the first! Yeh~

Thank you!

Add a Comment / Trackback url

Comment begin from here or jump up!