Log4j Default Appender

Active4 months ago

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

What does threshold mean in Log4J? This will help you to debug the code as by default it will print out on the console (default console appender is on).

Thanks in advance

Mridang Agarwalla
18.1k55 gold badges180 silver badges333 bronze badges
javanerdjavanerd
1,2324 gold badges18 silver badges30 bronze badges

4 Answers

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.

Log4j file appender properties

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.

JB NizetJB Nizet
571k69 gold badges956 silver badges1062 bronze badges

Threshold is second filter for messages to be logged

e.g.:

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

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

go to URL for more details

Shirishkumar BariShirishkumar Bari

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.

cweiskeLog4j.appender.default.maxbackupindex
24.1k11 gold badges97 silver badges162 bronze badges
Vinod RVinod R

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

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.

Ken BlockKen Block
2,9251 gold badge16 silver badges21 bronze badges

Not the answer you're looking for? Browse other questions tagged javalogginglog4j or ask your own question.

Active4 months ago

Is there any way to tell to log4j to write its log to the file and to the console?thanksthere are my properties:

fatnjazzyfatnjazzy

Log4j Consoleappender Xml

4,4718 gold badges50 silver badges72 bronze badges

4 Answers

Your root logger definition is a bit confused.See the log4j documentation.

This is a standard Java properties file, which means that lines are treated as key=value pairs. Your second log4j.rootLogger line is overwriting the first, which explains why you aren't seeing anything on the console appender.

You need to merge your two rootLogger definitions into one. It looks like you're trying to have DEBUG messages go to the console and INFO messages to the file. The root logger can only have one level, so you need to change your configuration so that the appenders have appropriate levels.

While I haven't verified that this is correct, I'd guess it'll look something like this:

Note that you also have an error in casing - you have console lowercase in one place and in CAPS in another.

LimitSteven SchlanskerSteven Schlansker
29.2k11 gold badges72 silver badges97 bronze badges

Your log4j File should look something like below read comments.

user295691
6,2091 gold badge18 silver badges30 bronze badges

Log4j Consoleappender

ramit girdharramit girdhar
MC Emperor

Log4j File Appender Date In Filename

10.5k12 gold badges56 silver badges92 bronze badges
Brijendra VermaBrijendra Verma

Write the root logger as below for logging on both console and FILE

log4j.rootLogger=ERROR,console,FILE

And write the respective definitions like Target, Layout, and ConversionPattern (MaxFileSize for file etc).

Pratap Kumar PandaPratap Kumar Panda

Log4j File Appender Properties

Not the answer you're looking for? Browse other questions tagged javaapachelogginglog4j or ask your own question.