DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Log4j Multiple Appender Example

Log4j Multiple Appender Example

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (1)
Save
Tweet
Share
160.31K Views

Join the DZone community and get the full member experience.

Join For Free

In this example you will see how to create a console and file appender and add it to the rootLogger.

The log4j.properties file is shown below.

log4j.rootLogger=DEBUG, CA, FA

#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=sample.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Set the logger level of File Appender to WARN
log4j.appender.FA.Threshold = WARN

Here we create two appenders ConsoleAppender and FileAppender. You need to set the File attribute of the FileAppender to the log file name, here it is sample.log. Add the FileAppender (FA) and the ConsoleAppender (CA) to the rootLogger.

You can also set the logger level for each appender seperately. Here the FileAppender logger level is set to WARN. Only WARN, ERROR and FATAL level log messages will be logged in the sample.log file. Since we don't set any log level explicitly for the ConsoleAppender it will inherit the rootLogger level, that is DEBUG.

Here is our example code.

package com.vaannila.helloworld;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class HelloWorld {

	static final Logger logger = Logger.getLogger(HelloWorld.class);
	
	public static void main(String[] args) {
		PropertyConfigurator.configure("log4j.properties");
		logger.debug("Sample debug message");
		logger.info("Sample info message");
		logger.warn("Sample warn message");
		logger.error("Sample error message");
		logger.fatal("Sample fatal message");
	}
}

The output on the console.

0    [main] DEBUG com.vaannila.helloworld.HelloWorld  - Sample debug message
0    [main] INFO  com.vaannila.helloworld.HelloWorld  - Sample info message
0    [main] WARN  com.vaannila.helloworld.HelloWorld  - Sample warn message
0    [main] ERROR com.vaannila.helloworld.HelloWorld  - Sample error message
0    [main] FATAL com.vaannila.helloworld.HelloWorld  - Sample fatal message

The contents of the sample.log file.

0    [main] WARN  com.vaannila.helloworld.HelloWorld  - Sample warn message
0    [main] ERROR com.vaannila.helloworld.HelloWorld  - Sample error message
0    [main] FATAL com.vaannila.helloworld.HelloWorld  - Sample fatal message

 You can download the log4j configuration file here.

Source: Download

Source + Lib: Download

 

Log4j File attribute

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Use AWS Controllers for Kubernetes To Deploy a Serverless Data Processing Solution With SQS, Lambda, and DynamoDB
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Introduction To OpenSSH
  • A First Look at Neon

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: