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

  • Home
  • SEARCH
  • 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 808933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:38:43+00:00 2026-05-15T00:38:43+00:00

My application has a homebrew logging class that I’m migrating to using log4j under

  • 0

My application has a homebrew logging class that I’m migrating to using log4j under the covers. However, since I’m using the homebrew class to pass the rest of the application’s logging statements to log4j, the output statements are logged as coming from the wrapper class instead of the source class.

Is there a way to ensure that the “correct” source is being shown besides creating new org.apache.log4j.Logger instances for every log statement? I’ve also tried using the Logger.log(String callerFQCN, Priority level, Object message, Throwable t) method, but it doesnt seem to be working, for example:

public class Logger2 {

    public static org.apache.log4j.Logger log4JLogger = org.apache.log4j.Logger.getLogger(Logger2.class);

    public static void warning(Object source, String message) {

        log(source, message, Level.WARN, null)
    }

    private static void log(Object source, String message, Level level, Throwable t) {

        String className = source.getClass().getName();
        System.out.println("Logging class should be " + className);
        log4JLogger.log(className, loggingLevel, message, t);
    }
}

When called by:

public void testWarning() {
    Logger2.warning(new Integer(3), "This should warn");
}

Prints:

Logging class should be java.lang.Integer
2010-05-25 10:49:57,152 WARN                              test.Logger2 - This should warn
  • 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-15T00:38:44+00:00Added an answer on May 15, 2026 at 12:38 am

    My home brewed logging solution used log4j’s LocationInfo class to find the source code information.

    With this solution, the locationInfo object contains information from the object that calls my logger with the loggerName.

    Here’s a simplified version of my logger that logs with log4j:

    public void log(Level level, String message) {
        LocationInfo locationInfo = new LocationInfo(new Throwable(),
                loggerName);
    
        MDC.put(LINE_NUMBER, locationInfo.getLineNumber());
        MDC.put(FILE_NAME, locationInfo.getFileName());
        MDC.put(CLASS_NAME, locationInfo.getClassName());
        MDC.put(METHOD_NAME, locationInfo.getMethodName());
        MDC.put(FQMETHOD_NAME, locationInfo.getClassName() + "."
                + locationInfo.getMethodName());
    
        logger.log(level, message);
    
        MDC.remove(LINE_NUMBER);
        MDC.remove(FILE_NAME);
        MDC.remove(CLASS_NAME);
        MDC.remove(METHOD_NAME);
        MDC.remove(FQMETHOD_NAME);
    }
    

    Btw: The Level, MDC and Logger class are all log4j classes.

    Replies to comments:

    The MDC object is stored on the ThreadLocal object and is accessible for the log4j logger.

    From the MDC Java documentation:

    The MDC is managed on a per thread basis.

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

Sidebar

Related Questions

My application has a class full of functions that perform various operations. These operations
My application has a feature that parses text using a regular expression to extract
The application has been put in iCloud since the beginning, so I have the
Our application has many controls that are created dynamically. For example, a navigation pane
My application has to poll a server for a maximum of 10 minutes (using
My application has a view controller that extends UITableViewController . The initialization method looks
My application has many aggregate fields that need to be updated when any related
My application has only one service class (not activity) in which I keep value
My application has a requirement that we need to encrypt index fields. Right now
My application has the following classes: public class Widget { public virtual int Id

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.