Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7408639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:57:03+00:00 2026-05-29T05:57:03+00:00

My class code is as below. public class LogLevelFilterFileAppender extends FileAppender implements Cloneable{ private

  • 0

My class code is as below.

public class LogLevelFilterFileAppender extends FileAppender implements Cloneable{

    private final static String DOT = ".";
    private final static String DASH = "-";
    private static final String ORIGINAL_FILE_NAME = "OrginalLogFileName";

    public LogLevelFilterFileAppender() {}

    public LogLevelFilterFileAppender(Layout layout, String fileName,
                                      boolean append, boolean bufferedIO, int bufferSize)
    throws IOException {
        new FileAppender(layout, fileName, append, bufferedIO, 10);
        new RollingFileAppender(layout, fileName,append);
    }

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

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

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

    @Override
    public void append(LoggingEvent event) {
        try {
            setFile(appendLevelToFileName((String) MDC.get(ORIGINAL_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) {
                newFileName = fn.substring(0, dotIndex) + DASH + level + DOT
                + fn.substring(dotIndex + 1);
            }
            else {
                newFileName = fn + DASH + level;
            }
            return logFile.getParent() + File.separator + newFileName;
        }
        return null;
    }
}

And my configuration of log4j.properties are as follows.

log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = com.elitecore.mediation.util.log.LogLevelFilterFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{MMMMM dd,yyyy HH:mm:ss} %-5p [ %C{1} - %M() - %L ] - %m%n
# log4j.appender.fileout.layout.ConversionPattern = %d{MMMMM dd,yyyy HH:mm:ss} %-5p %c %M():%L - %m%n
# %r [%t] %-5p %c %x - %m%n
# %d{MMMMM dd HH:mm:ss} %-5p %c %M():%L - %m%n
#%d{MMMMM dd HH:mm:ss} %6p %c %M() : %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = /usr/local/mediation/logs/mediation-logs.log

This is creating a log for different types, for example, DEBUG, ERROR, and INFO in different log files. But what limitation is it? It is creating larger and larger log files. I want to make log files for, say, of 5 MB, and previous logs should be removed. How can I do that? When I try with RollingFile Appender, I get the below log files only.

[root@manage logs]# ls -1
mediation-logs-DEBUG.log
mediation-logs-ERROR.log
mediation-logs-INFO.log
mediation-logs-INFO.log.1
mediation-logs-INFO.log.2
mediation-logs.log

Rolling of log files ERROR,DEBUG is not done, but INFO is done.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:57:04+00:00Added an answer on May 29, 2026 at 5:57 am

    I suggest you derive from RollingFileAppender instead of FileAppender. This will give you the possibility to define how large the log-files will grow, and how many “old” ones you want to keep. Check out the manual on how to use it later in your log4j.propertiers.

    If I understand correctly, you want one file per log-level, is that correct? If so, I suggest you follow this FAQ-Entry rather than “rolling” your own solution 🙂

    Matching exact log-levels

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below: public class PatientAgent extends Agent { private final String
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I'm struggling to convert the below code to C#. Class Class1 Implements IMyInterface Public
Check out the simple code below : Public Class Form1 Private _items As List(Of
Examine the code below public abstract class ClassA<ClassBType extends ClassB<ClassCType>,ClassCType extends ClassC> { public
I have the code below : public class Anything { public int Data {
I'm getting this error message with the code below: class Money { public: Money(float
The below code in Java throws Null pointer exception. public class New{ int i;
I have the Global.asax like the code below: public class MvcApplication : System.Web.HttpApplication {
Assuming the code below: public class DynamicAspxHandler : IHttpHandler { bool IHttpHandler.IsReusable { get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.