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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Log4j File Appender

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

Join the DZone community and get the full member experience.

Join For Free

a typical requirement in the project is to log different modules in different log files. for instance in this example we have two modules one for the admin and other for reports.

to do this we create two seperate fileappenders and associate them with each package.

package com.vaannila.admin;

import org.apache.log4j.logger;
import org.apache.log4j.propertyconfigurator;

import com.vaannila.report.samplereport;

public class sampleadmin {
	
	static logger logger = logger.getlogger(sampleadmin.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");
		samplereport obj = new samplereport();
		obj.generatereport();
	}

}

here instead of associating the appenders to the rootlogger we associate it with different packages. the adminfileappender is linked with the admin package and the reportfileappender is linked with the report package. this means all the log entries in the admin module will be logged in the admin.log file and all the log entries in the report module will be logged in the report.log file. you can also set different logger levels for each package, here for the admin package it is set to warn and for the report package it is set to debug . the rootlogger will be by default set to the logger level debug , so this statement " log4j.rootlogger=debug " is there only for your understanding purpose and it is not necessary.

in this example we have two classes sampleadmin class in the admin package and samplereport class in the report package.

package com.vaannila.admin;

import org.apache.log4j.logger;
import org.apache.log4j.propertyconfigurator;

import com.vaannila.report.samplereport;

public class sampleadmin {
	
	static logger logger = logger.getlogger(sampleadmin.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");
		samplereport obj = new samplereport();
		obj.generatereport();
	}

}
package com.vaannila.report;

import org.apache.log4j.logger;

public class samplereport {

	static logger logger = logger.getlogger(samplereport.class);
	
	public void generatereport()
	{
		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");
	}
}

after executing the program, the contents of admin.log file.

0    [main] warn  com.vaannila.admin.sampleadmin  - sample warn message
0    [main] error com.vaannila.admin.sampleadmin  - sample error message
0    [main] fatal com.vaannila.admin.sampleadmin  - sample fatal message

the contents of report.log file.

0    [main] debug com.vaannila.report.samplereport  - sample debug message
0    [main] info  com.vaannila.report.samplereport  - sample info message
0    [main] warn  com.vaannila.report.samplereport  - sample warn message
0    [main] error com.vaannila.report.samplereport  - sample error message
0    [main] fatal com.vaannila.report.samplereport  - sample fatal message 

it's not necessary to apply the logger level to each package, by default the rootlogger level will be inherited.

log4j.logger.com.vaannila.admin=,adminfileappender 
log4j.logger.com.vaannila.report=,reportfileappender 

insted of applying a logger level, the field is left blank so the rootlogger level (" debug ") will be inheritted.

if you associate a consoleappender to the rootlogger then all the log entries in both the admin and report packages will be log on the console.

log4j.rootlogger=debug, ca

# adminfileappender - used to log messages in the admin.log file.
log4j.appender.adminfileappender=org.apache.log4j.fileappender
log4j.appender.adminfileappender.file=admin.log
log4j.appender.adminfileappender.layout=org.apache.log4j.patternlayout
log4j.appender.adminfileappender.layout.conversionpattern= %-4r [%t] %-5p %c %x - %m%n

# reportfileappender - used to log messages in the report.log file.
log4j.appender.reportfileappender=org.apache.log4j.fileappender
log4j.appender.reportfileappender.file=report.log
log4j.appender.reportfileappender.layout=org.apache.log4j.patternlayout
log4j.appender.reportfileappender.layout.conversionpattern= %-4r [%t] %-5p %c %x - %m%n

# consoleappender
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

log4j.logger.com.vaannila.admin=warn,adminfileappender 
log4j.logger.com.vaannila.report=debug,reportfileappender 

on executing the example you will see the following output on the console.

0    [main] warn  com.vaannila.admin.sampleadmin  - sample warn message
0    [main] error com.vaannila.admin.sampleadmin  - sample error message
0    [main] fatal com.vaannila.admin.sampleadmin  - sample fatal message
0    [main] debug com.vaannila.report.samplereport  - sample debug message
0    [main] info  com.vaannila.report.samplereport  - sample info message
0    [main] warn  com.vaannila.report.samplereport  - sample warn message
0    [main] error com.vaannila.report.samplereport  - sample error message
16   [main] fatal com.vaannila.report.samplereport  - sample fatal message

if you want the log messages from all the packages to be logged then add the corresponding appender to the rootlogger , if you want the appender to be associated with a specific package then apply only to that package. if you do it in both the places then the log entries will be repeated.

the directory structure of the example is shown below.

you can download the log4j configuration file here.

source: download

source + lib: download

Log4j

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Edge Compute? It’s Kind of Like Knitting Dog Hats
  • MACH Architecture Explained
  • SQL CTE: How to Master It in One Sitting With Easy Examples
  • Machine Learning in Cybersecurity

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo