What does threshold mean in Log4J?

108,268

Solution 1

You have two things here : a logger, and an appender. Unfortunately, you chose the same name for both, which doesn't make it very clear.

The logger's minimum level is set to warn, which means everything you log with this logger which doesn't have at least the warn level will be ignored.

Once a message is accepted by the logger, it's sent to one or several appenders (to a file, to the console, to a mail server, etc.). Each of these appenders may define a threshold. You could for example limit the messages in the console to errors, but accept warn messages in the log file.

Solution 2

Threshold is second filter for messages to be logged

e.g.:

 log4j.logger.TextProcessor=Debug,TextProcessor , InfoLogger
 .
 .
 .
 log4j.appender.TextProcessor.Threshold=Error

if Logger is set at level DEBUG and appender Threshold is set at Error then with the appender TextProcessor only Error and higher severity messages would be logged.

Use of Threshold is ,you can define different appender with different threshold levels ,for e.g in above mentioned example you can also have InfoLogger with Info level messages logging enabled

 log4j.logger.TextProcessor=Debug,TextProcessor , InfoLogger
 .
 .
 .
 log4j.appender.InfoLogger.Threshold=INFO

To understand levels , There are below levels of logging in log4j:

FATAL: shows messages at a FATAL level only  
ERROR: Shows messages classified as ERROR and FATAL  
WARNING: Shows messages classified as WARNING, ERROR, and FATAL  
INFO: Shows messages classified as INFO, WARNING, ERROR, and FATAL  
DEBUG: Shows messages classified as DEBUG, INFO, WARNING, ERROR, and FATAL  
TRACE : Shows messages classified as TRACE,DEBUG, INFO, WARNING, ERROR, and FATAL
ALL : Shows messages classified as TRACE,DEBUG, INFO, WARNING, ERROR, and FATAL 
OFF : No log messages display

go to URL for more details

Solution 3

The levels of logging are TRACE, DEBUG, INFO, WARN, ERROR and FATAL. You will be able to choose what to log at what level in the code depending on the severity. For example you will have the ability to log entry and exit of methods but can choose to log at the DEBUG level. This will help you to debug the code as by default it will print out on the console (default console appender is on). While going to production you can increase the threshold to ERROR and prevent the application from printing out not so useful details on the console or log files.

Solution 4

Give you simple mapping from properties config file to flow of log messages. (I hid some lines of config to minimize)

log4j.rootLogger=ALL, stdout
log4j.logger.com.xyz=INFO, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG
...

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.Threshold=WARN
...

schema of logging from settings above

To understand what it is, you should know that:

  • The levels of logging increase when retrieving to the leftmost: TRACE, DEBUG, INFO, WARN, ERROR and FATAL
  • Minimum level logging which logger accepts from application.
  • Minimum level logging on appender which decides what will be written

** There are some thing more complex about inheritance and additivity, but you should start at basic and simple things first.

Share:
108,268

Related videos on Youtube

javanerd
Author by

javanerd

Updated on July 14, 2020

Comments

  • javanerd
    javanerd almost 4 years

    I have a log4j properties something like below. Everything that is logged in TextProcessor.log is something above WARN level. I don't understand the threshold that is set here to debug. Can someone explain what the threshold does?

    log4j.logger.TextProcessor=warn,TextProcessor 
    
    log4j.appender.TextProcessor=org.apache.log4j.RollingFileAppender
    log4j.appender.TextProcessor.File=C:/project/logs/TextProcessor.log
    log4j.appender.TextProcessor.MaxFileSize=10MB
    log4j.appender.TextProcessor.MaxBackupIndex=10
    log4j.appender.TextProcessor.Threshold=debug
    log4j.appender.TextProcessor.layout=org.apache.log4j.PatternLayout
    log4j.appender.TextProcessor.layout.ConversionPattern=[%d] [%5p] (%F:%L) - %m%n
    
  • Dirk Schumacher
    Dirk Schumacher over 7 years
    for me 'threshold' does not really fit. I started log4j by using the xml configuration and in the appenders it is called 'level' with the meaning of threshold. I think the same concept (limiting log severity) with two different labels (label,threshold) makes it more difficult to understand (If I really did understand it right so far). But I am not a native english speaker anyway.
  • Samuel Åslund
    Samuel Åslund almost 3 years
    Log4J use "WARN" in configuration, not "WARNING" !!!!
  • Samuel Åslund
    Samuel Åslund almost 3 years
    For completness it should be said that the OP's problem is that the logger have a more restictive policy (WARN) than the Appender (DEBUG) and it's always the more restrictive policy that wins.
  • Alan Feldstein
    Alan Feldstein about 2 years
    Where is Threshold documented for Log4j 1.x?
  • Alan Feldstein
    Alan Feldstein about 2 years