HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j / Log4j ConsoleAppender Configuration Example

Log4j ConsoleAppender Configuration Example

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:

PropertyDescription
immediateFlushTo set if console stream being flushed with each logging output request.
encodingOver-ride the default character-encoding scheme.
thresholdAny logging request with a level below the threshold will be ignored.
targetEither 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 !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. monika tiwari

    February 11, 2020

    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

  2. HARSHA

    October 9, 2018

    HOW TO ADD PREFIX TO EVERY LINE THAT IS WRITTEN USING LOG4J

  3. Remko Popma

    June 16, 2016

    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!

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces