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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:04:09+00:00 2026-05-27T11:04:09+00:00

We have a plain old java library that is instantiated from many different applications.

  • 0

We have a plain old java library that is instantiated from many different applications. In this case each application is a web application that all live in the same tomcat container.

Each application logs to its own log file, using its own logger. We want the logs generated by the library, that are pertinent to a specific application, to also go to that applications separate log file.

For this, one way is to allow the application to pass in its logger to the library:

library = new library(Logger applicationsVeryOwnLogger);

And then use that logger, to log all statements in the library. However, this means that the logger is now a class variable in the library, and every class in the library needs a reference to the library to use the right logger.

Are there better ways to do this?

  • 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-27T11:04:10+00:00Added an answer on May 27, 2026 at 11:04 am

    We had a similar need in one of our older applications. The solution we came up with was a ResourceManager that would retrieve resources(Logger, Config files etc) by (context)ClassLoader.

    Usually every application deployed as an EAR gets its own ClassLoader and the library can then just call ResourceManager.getLogger() to get the Logger associated with the current Thread/Application. That way you dont need to pass it with every method call in the library (it requires that you can change the library tho).

    import java.util.*;
    import java.util.logging.*;
    
    public class ResourceManager 
    {
        private static final Map<ClassLoader, Map<String, Object>> resources = 
            Collections.synchronizedMap(new WeakHashMap<ClassLoader, Map<String, Object>>());
        public static final String LOGGER = Logger.class.getName();
    
        static
        {
            // adjust for log4j or other frameworks
            final Logger logger = Logger.getLogger("logging.default");
            logger.setLevel(Level.ALL);
            logger.addHandler(new ConsoleHandler() 
            {
                {
                    setOutputStream(System.out);
                    setLevel(Level.ALL);
                }
            });
            registerResource(null, LOGGER, logger);
        }
    
        private static ClassLoader getApplicationScope()
        {
            return Thread.currentThread().getContextClassLoader();
        }
    
        public static void registerResource(final String name, final Object resource)
        {
            registerResource(getApplicationScope(), name, resource);
        }
    
        public static synchronized void registerResource(final ClassLoader scope, final String name, final Object resource)
        {
            Map<String, Object> hm = null;
            hm = resources.get(scope);
            if (hm == null)
            {
                hm = Collections.synchronizedMap(new HashMap<String, Object>());
                resources.put(scope, hm);
            }
            hm.put(name, resource);
        }
    
        public static Object getResource(final String name)
        {
            for(ClassLoader scope = getApplicationScope();;scope = scope.getParent())
            {
                final Map<String, Object> hm = resources.get(scope);
                if ((hm != null) && hm.containsKey(name)) 
                {
                    return hm.get(name);
                }
                if (scope == null) break;
            }
            return null;
        }
    
        public static void registerLogger(final Logger logger)
        {
            registerResource(LOGGER, logger);
        }
    
        public static Logger getLogger()
        {
            return (Logger)getResource(LOGGER);
        }       
    }
    

    Register Logger in the init phase of EJB/WebApp(needs to be registered before any call to getLogger):

    Logger logger = Logger.getLogger([Application Logger Name]);
    ResourceManager.registerLogger(logger);
    

    Retrieve Logger in library(utility method):

    private Logger getLogger()
        {
            return ResourceManager.getLogger();     
        }
    

    This will return the logger for the application(EAR) that is associated with the current thread.

    Not limited to Loggers it also works for other resources that you want to share.

    Limitations:

    • wont work if you package multiple applications/EJBs per deployed EAR

    • ResourceManager and Logging library need to be on the same or a higher ClassLoader than the library and application. If there is an option to bundle then Alexanders approach is cleaner. (we use java.util.logging which is by default on the server level so his approach doesnt work)

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

Sidebar

Related Questions

I have a Mono application with a plain-old Windows Forms form that has a
In JPA the Entities are nice annotated Plain Old Java Objects. But I have
I have an old personal project written in Java 1.4 that I am porting
I have a code library written in plain old C++ (no .NET/managed code) and
I have an old application that uses frames (not iframes) was was written back
I have a plain old Windows Forms application with a WiX installer, and it
Say, I have a pretty simple Java application that needs the way to store
I need to start 1-3 external programs in my Java application that have paths
I have a plain text file looking like this: some text containing line breaks
In an iPhone application, I have some plain C functions. Is it possible to

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.