Changing Log4j Logging Levels by Output Type

Let’s say you want only errors to display in your console, but warnings to be logged to file as well for later review. Here’s how to do it in your log4j.properties file.

Our properties file is below.

First we have to first consider Log4j Level Precedence, with OFF followed by FATAL, ERROR, WARN, INFO, DEBUG and finally ALL for the most detailed output. So in our example below we go for a noisy level in our rootLogger. We used ALL, but since we’re going to top out at WARN to our log file, INFO would have produced the same result.

We then set the Thresholds per output type with our desired logging action of ERRORS and FATALS to the Console, and the inclusion of WARNINGS logged in our physical log file.

Logged!