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 8961537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:55:01+00:00 2026-06-15T15:55:01+00:00

Originally titled: How can I get Apache LogFactory to always return the same log

  • 0

Originally titled: How can I get Apache LogFactory to always return the same log implementation?

I have a project that uses quite a few different loggers configured to go to different places. When I’m running a unit test, I want to override them all with a SimpleLogger that I instantiate myself. How can I set this into LogFactory so that it’s always returned for unit tests?

I’d really rather not deal with an external configuration file.

As a little bit of background, I find loggers notorously untrustworthy. It’s not really the logger exactly, it’s that there are so many ways to configure it and filter the output–it can be done from various config files or anywhere in code, and then it could go out to a variety of locations. The logger can even send some kind of output (errors) to different locations from others based on the configuration.

Although this can be quite awesome and powerful, it’s all undone when you spend 6 hours wondering why your code isn’t being called when it actually IS being called but your output is being redirected.

When running tests I want to be as sure as possible that I’m seeing all the output, so I want to redirect ALL logs to the console. (All, as in my current project can create a couple dozen different logfiles spread across 6 directories.)

  • 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-06-15T15:55:02+00:00Added an answer on June 15, 2026 at 3:55 pm

    I finally figured out a way to do this.

    You can extend LogFactory with your own factory–overriding both getInstance methods to return your logger (apparently all the other methods can be empty) . I used SimpleLogger to divirt all logging to the console:

    public Log getInstance(String name) throws LogConfigurationException {
        // If this weren't just for testing I'd keep a map of these loggers instead...
        SimpleLog consoleLogger=new SimpleLog(name);
        consoleLogger.setLevel(SimpleLog.LOG_LEVEL_ALL);
        return consoleLogger;
    }
    public Log getInstance(Class className) throws LogConfigurationException {
        return getInstance(arg0.getName());
    }
    

    then install it with a static method inside this class:

    public static void initialize() {
        System.setProperty(LogFactory.FACTORY_PROPERTY, MyLogFactory.class.getName());
    }
    

    this confines the entire log re-router to one class, if you call initialize() at the beginning of a test, all log output for the session is re-routed to the console. Don’t call it later or the code you are testing may have already called the “Normal” factory and cached the real logger.

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

Sidebar

Related Questions

I have been trying everything I can to get this script to work and
I have a IBAction such as: - (IBAction)showPicker:(id)sender; How can I get the name
Originally I had two tables in my DB, [Property] and [Employee]. Each employee can
Originally, I just want to verify that session_start locks on session. So, I create
I can't get javascript to work with Twitter's Bootstrap. I've got this <p> <a
I have been requested to get involved with a family member's site. To date,
I have two instances of NSScrollView both presenting a view on the same content.
I have a Sharepoint 2007 site that is displaying the old/original title in the
I need to have a custom view controller with the same functionality as the
I am trying to get the next highest PK in an SQLite database that

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.