Log4J Levels

This log4j post is a tutorial post describing different levels of logging in log4j. This is for log4j beginners only but if you wish to refresh, go ahead and enjoy!

Log4j logger contains three main components namely logger, appender and layout. Logger takes care of the logging mechanism and deals with level of logging. Log4j provides five standard levels of logging. There are two more special levels given by log4j. Above all these, log4j allows you to create custom levels.

Five standard log4j levels

DEBUG Level

This log4j level helps developer to debug application. Level of message logged will be focused on providing support to a application developer.

INFO Level

This log4j level gives the progress and chosen state information. This level will be generally useful for end user. This level is one level higher than DEBUG.

WARN Level

This log4j level gives a warning about an unexpected event to the user. The messages coming out of this level may not halt the progress of the system.

ERROR Level

This log4j level gives information about a serious error which needs to be addressed and may result in unstable state. This level is one level higher than WARN.

FATAL Level

This log4j level is straightforward and you don’t get it quite often. Once you get this level and it indicates application death.

Two special log4j levels

ALL Level

This log4j level is used to turn on all levels of logging. Once this is configured and the levels are not considered.

OFF Level

This log4j level is opposite to ALL level. It turns off all the logging.

New Trace Level added in Log4j

TRACE Level

This log4j level gives more detailed information than the DEBUG level and sits top of the hierarchy. This level is introduced from version 1.2.12 in log4j.

Custom log4j levels

Log4j’s levels are mostly sufficient for all common applications. Rarely you may need a new Level apart from the levels provided by log4j. In that case you can extend org.apache.log4j.Level class and have your own custom level implementation.

Log4j Logger Output

When a logger is created, generally you assign a level. The logger outputs all those messages equal to that level and also all greater levels than it. So the order for the standard log4j levels are:

Log4J Levels

TRACE Level DEBUG Level INFO Level WARN Level ERROR Level FATAL Level
TRACE Level Y Y Y Y Y Y
DEBUG Level N Y Y Y Y Y
INFO Level N N Y Y Y Y
WARN Level N N N Y Y Y
ERROR Level N N N N Y Y
FATAL Level N N N N N Y
ALL Level Y Y Y Y Y Y
OFF Level N N N N N N

Log4j Level Inheritance Rule

There can be instances when you don’t assign a level to the logger. In such cases log4j handles it seamlessly based on the following level inheritance rule:
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.

That means, if you have a package as com.foo.bar and you didn’t allocate a level, then it will inherit the level from com.foo package. Still in that package also if the level is not available, then it will inherit from the log4j root level. The log4j’s root logger is instantiated and available always. Log4j’s root logger by default has DEBUG level.

This Log4J tutorial was added on 16/01/2011.

«

»

Comments on "Log4J Levels" Tutorial:

  1. Pawan says:

    Thanks a lot Joseph.

    Could you give a sample example. How can we turn on or off the logger. Is there way to redirect the log message to standard output?

    • Pradip Bhatt says:

      private Logger log = Logger.getLogger(ClassName.class);

      public void MyMehodABC()
      {
      try
      {
      log.info(“we are in MyMethodABC”);
      // code
      }
      }

      For this you have to configure log4j.properties file for configuring where too display this file.

      Using log4j you can manage logging mechanism in File, Console etc etc…

      catch(Exception e)
      {
      log.error(e.getMessage(),e);
      }

  2. Nagender says:

    Thanks for sending blog material

    Thanks
    Nagender

  3. Sriram says:

    awesome tutorial.
    I like this theme also.
    Nice one dude

  4. hi says:

    good one…
    keep posting…..

  5. bharathi says:

    how to i separate the jvm info in console and application info into .log file

  6. kvs says:

    Gud material Dude………….

  7. muralidhar says:

    Good one ..i want some example code..can u post the code for logs..

  8. santosh says:

    This is quick informative article. Thanks -sant.

  9. shamsuddin says:

    quick summary.. thanks… shams

  10. raj says:

    hi joe, still i am not getting when to use a particular level like debug info warn error and fatal
    please help with a example

    • Pradip Bhatt says:

      Dear…. Its your application related logging mechanism that when you have to use which level of logging.

      Whenever you feel that you want to make Test cases for your program at that time its better to write debug level in every methods of program so if any error occured during the execution then you will find out that in which situation you have to felt down your contrl over the application.

      Generally fatal error is the highest priority based error.

  11. Garfield says:

    The table was very helpful, thx!

  12. Anonymous says:

    Thanks for simple and useful article

    -Santosh

  13. Anonymous says:

    If you want monitoring Log4j at runtime in your web application, and change the level of a logger, can use Log4j Web Tracker.

  14. [...] a professional logger like log4j provides different levels for logging. We can seggregate the log message accordingly. For example, X messages should be [...]

  15. arun says:

    what is purpose of log4j in java??
    still now i don’t know
    pls help me

  16. arun says:

    what is purpose of log4j in java?
    still now i don’t know
    pls help me

  17. Supriya says:

    Hi Sir…..,

    Very nice and thank you soo much.

    Regards,
    Supriya

  18. Anonymous says:

    really good way the them is thought.

    thanks,
    Manoj

  19. Kiran says:

    Very good collection of all topic s… Got inspiration from you to do something healthier .

    Thanks so much Joe and your family.

  20. TK says:

    Nice one !!!

  21. Swapna says:

    Thanks for the nice information………….

  22. jasmin says:

    HI

    i want to send only specific msg into logfile .our require ment is to maintain some particular details like uid chainId etc …into logfile

  23. madu says:

    i’m interested in redirecting log messages from a method “abc” in package “xyz” to go to a specific log file “pqr”. I dont want all the logger messages from package xyz to go to “pqr” but just from that one method (“abc”) in the package.

    How do i achieve this?

    thank u in advance

  24. srinivas battineni says:

    nice information

    and thank you for given this information..

  25. arti says:

    very gud article…..
    but how to impliment logging facility in our application plz send example

  26. [...] lets leave JCL and look at underlying logging framework. We want to use Log4j as the underlying logging component how do we do it. Assume we are using commons-logging then it is [...]

  27. sri says:

    very nice

  28. Amit Herlekar says:

    This is what is precisely needed! Very crisply explained!

  29. sha tk says:

    nicely written…

  30. Nathan says:

    Thala plz change your(Joe) photo .
    Eventhough u hv done many things, ur photo is not looking good

  31. Thanjavur @rvind $mart says:

    Thank you. very nice explanation

  32. Steve says:

    Thanks. The chart in the “Log4j Logger Output” section was very helpful.

  33. kishore says:

    I need some Log4j and ajax material which i can easily understand do needful

  34. Anonymous says:

    It’s very nice to read.could u please provide some Examples.

  35. bayu says:

    helo joe,
    how restart java daemon on unix ?
    svcs can’t help me, especially svcadm

  36. Thank you very much! Very useful information.

  37. rahul says:

    gud one

  38. Poornima says:

    Really very nice :)

  39. Anonymous says:

    how to generate a valid xml output by useing XmlLayout in log4j

  40. Abhishek says:

    Hi Joe,

    Could you please let us know if it is necessary to restart the application server every time the logging level are changed? I tried it without restarting the server, it doesn’t work. Just wanted to know if there is a way to change the logging level without restarting the server.

  41. Suvojeet Pal says:

    Can you please provide one sample example for Custom log4j levels

  42. Miguel Olave says:

    Thanks for your time. Always that I have a doubt respect log levels I come back to this article.

  43. Aakeef says:

    Hi Joe,

    Can you give java example for disable logging(like axis logs,etc… )??

    Thanks in Advance..

  44. naresh says:

    how to only one level log it means i want print the only warn statemens

  45. rajesh says:

    if i enable log level to fatal but log level enable as debug

  46. Raju says:

    How to create custom logger level and how to set message in that?

  47. govardhana reddy says:

    super

  48. govardhana reddy says:

    How to create custom logger level and how to set message in that?

Your Comment

↑ Go to top