Any logging application intended to print logging information to a console should use this org.apache.log4j.ConsoleAppender. ConsoleAppender
is a very simple class designed to write logging information to either System.out
or System.err
. The destination of the log messages can be configured via a property named target
.
Properties of ConsoleAppender
The configurable properties of ConsoleAppender
are described below:
Property | Description |
---|---|
immediateFlush | To set if console stream being flushed with each logging output request. |
encoding | Over-ride the default character-encoding scheme. |
threshold | Any logging request with a level below the threshold will be ignored. |
target | Either System.out or System.err . The default is System.out . |
ConsoleAppender Configuration
ConsoleAppender configuration in properties file
log4j.rootCategory=debug,console log4j.logger.com.demo.package=debug,console log4j.additivity.com.demo.package=false log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.out log4j.appender.console.immediateFlush=true log4j.appender.console.encoding=UTF-8 #log4j.appender.console.threshold=warn log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.conversionPattern=%d [%t] %-5p %c - %m%n
ConsoleAppender configuration in XML file
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="target" value="System.err"/> <param name="immediateFlush" value="false"/> <param name="encoding" value="UTF-8"/> <param name="threshold" value="warn"/> <layout class="org.apache.log4j.PatternLayout"> <param name="conversionPattern" value="%d [%t] %-5p %c - %m%n"/> </layout> </appender> <logger name="com.demo.package"> <level value="debug"/> <appender-ref ref="console"/> </logger> <root> <priority value ="debug" /> <appender-ref ref="console"/> </root> </log4j:configuration>
Test ConsoleAppender Configuration
Let’s write a quick java program and write the logs in console using above configuration.
import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class Demo { static Logger logger = Logger.getLogger(Demo.class); public static void main(String[] args) { // PropertiesConfigurator is used to configure logger from properties file PropertyConfigurator.configure("log4j.properties"); // Log in console logger.debug("Log4j console appender configuration is successful !!"); } }
Now put the log4j.properties
file in project root folder or resources folder as per your need – and run the application. You will get below log message in console.
2016-06-14 18:03:13,175 [main] DEBUG Demo - Log4j console appender configuration is successful !!
Drop me your questions in comments section.
Happy Learning !!
monika tiwari
i want my logger info/debug to be printed in my log file as well as apache console. it only goes to my log file , want to print them on console as well. How to do that
HARSHA
HOW TO ADD PREFIX TO EVERY LINE THAT IS WRITTEN USING LOG4J
Remko Popma
It would be great if you and other bloggers would write about Log4j 2. As you may know, Log4j-1.2 became End Of Life in 2015 and is no longer supported. That’s a nice way to say it is dead.
Log4j 2 on the other hand is actively being developed and is very much alive. Log4j 2 has some cool features like support for Java 8 lambdas, it is garbage-free(!) and performance-wise it runs circles around the competition (log4j-1.2, Logback and JUL).
There is a lot of interesting stuff to write about, take a look at its Async Loggers.
For us as an industry it is important to keep learning and switch to modern technology when something becomes outdated.
I think it’s great that bloggers like yourself help to make knowledge of modern Java technology available to many people!