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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Surviving the Incident
  • Detect Log4j Vulnerability Using ACS
  • How To Detect and Secure Your Java App From Log4j Vulnerabilities
  • How to Check if a Java Project Depends on A Vulnerable Version of Log4j

Trending

  • Spring Cloud LoadBalancer vs Netflix Ribbon
  • One Checkbox to Cloud: Migrating from Tosca DEX Agents to E2G
  • Event Storming Workshops: A Closer Look at Different Approaches
  • Reducing Hallucinations Using Prompt Engineering and RAG
  1. DZone
  2. Coding
  3. Java
  4. Log4j Tutorial – Writing different log levels in different log files

Log4j Tutorial – Writing different log levels in different log files

By 
Veera Sundar user avatar
Veera Sundar
·
May. 19, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
51.3K Views

Join the DZone community and get the full member experience.

Join For Free

Recently one of my blog reader Surisetty send me a question, asking me if it is possible to write log messages of different levels (info, debug, etc) into different log files? To answer his question, yes, it is possible. We can do this by extending the FileAppender class and writing our own logic.

Below is the proof of concept code written to demonstrate this. Before that, you can download the Eclipse project file to run this code in your environment.

Download the Source code

To write different log levels in different log files

  1. Create a custom Log4j appender extending FileAppender.
  2. In that, override the append() method and check for the log level before writing a log message. Based on the level, call the setFile() method to switch between corresponding log file.
  3. Also, use MDC to store the original log file name mentioned in the log4j.properties. This is needed because setFile() changes the log file name every time you call it. So, we need to keep a track of the original file name somehow. And, we can use Log4j MDC for this.

Custom Appender: LogLevelFilterFileAppender

 

package com.veerasundar.log4j;


import java.io.File;
import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.MDC;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;

/**
* This customized Log4j appender will seperate the log messages based on their
* LEVELS and will write them' into separate files. For example, all DEBUG
* messages will go to a file and all INFO messages will go to a different file.
*
* @author Veera Sundar | http://veerasundar.com
*
*/
public class LogLevelFilterFileAppender extends FileAppender {

private final static String DOT = ".";
private final static String HIPHEN = "-";
private static final String ORIG_LOG_FILE_NAME = "OrginalLogFileName";

public LogLevelFilterFileAppender() {

}

public LogLevelFilterFileAppender(Layout layout, String fileName,
boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
super(layout, fileName, append, bufferedIO, bufferSize);
}

public LogLevelFilterFileAppender(Layout layout, String fileName,
boolean append) throws IOException {
super(layout, fileName, append);
}

public LogLevelFilterFileAppender(Layout layout, String fileName)
throws IOException {
super(layout, fileName);
}

@Override
public void activateOptions() {
MDC.put(ORIG_LOG_FILE_NAME, fileName);
super.activateOptions();
}

@Override
public void append(LoggingEvent event) {
try {
setFile(appendLevelToFileName((String) MDC.get(ORIG_LOG_FILE_NAME),
event.getLevel().toString()), fileAppend, bufferedIO,
bufferSize);
} catch (IOException ie) {
errorHandler
.error(
"Error occured while setting file for the log level "
+ event.getLevel(), ie,
ErrorCode.FILE_OPEN_FAILURE);
}
super.append(event);
}

private String appendLevelToFileName(String oldLogFileName, String level) {
if (oldLogFileName != null) {
final File logFile = new File(oldLogFileName);
String newFileName = "";
final String fn = logFile.getName();
final int dotIndex = fn.indexOf(DOT);
if (dotIndex != -1) {
// the file name has an extension. so, insert the level
// between the file name and the extension
newFileName = fn.substring(0, dotIndex) + HIPHEN + level + DOT
+ fn.substring(dotIndex + 1);
} else {
// the file name has no extension. So, just append the level
// at the end.
newFileName = fn + HIPHEN + level;
}
return logFile.getParent() + File.separator + newFileName;
}
return null;
}
}

log4j.properties file

log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = com.veerasundar.log4j.LogLevelFilterFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c - %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = C:/vraa/temp/logs.log

Lets test our code

package com.veerasundar.log4j;

import org.apache.log4j.Logger;

public class Log4jDemo {

private static final Logger logger = Logger.getLogger(Log4jDemo.class);

public static void main(String args[]) {
logger.debug("This is a debug message");
logger.info("This is a information message");
logger.warn("This is a warning message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
logger.debug("This is another debug message");
logger.info("This is another information message");
logger.warn("This is another warning message");
logger.error("This is another error message");
logger.fatal("This is another fatal message");
}
}

 Download the Source code

From http://veerasundar.com/blog/2011/05/log4j-tutorial-writing-different-log-levels-in-different-log-files/

Log4j

Opinions expressed by DZone contributors are their own.

Related

  • Surviving the Incident
  • Detect Log4j Vulnerability Using ACS
  • How To Detect and Secure Your Java App From Log4j Vulnerabilities
  • How to Check if a Java Project Depends on A Vulnerable Version of Log4j

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: