Separate registrar for each library instance

We have a simple old java library created from different applications. In this case, each application is a web application that all runs in one tomcat container.

Each application is logged in its own log file using its own log. We want the logs generated by the library for a specific application to be sent to this separate log file as well.

To do this, one way is to allow the application to go through its journal in the library:

library = new library(Logger applicationsVeryOwnLogger);

And then use this registrar to register all the operators in the library. However, this means that the registrar is now a class variable in the library, and each library class needs a reference to the library in order to use the correct registrar.

Are there any better ways to do this?

+5
source share
2 answers

We had a similar need for one of our old applications. The solution we came across is a ResourceManager that will retrieve resources (Logger, Config files, etc.) Using the (context) ClassLoader.

, EAR, ClassLoader, ResourceManager.getLogger(), , Thread/Application. (, 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);
    }       
}

EJB/WebApp ( getLogger):

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

( ):

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

(EAR), .

Loggers, , .

:

  • , /EJB EAR

  • ResourceManager Logging ClassLoader, . , Alexanders . ( java.util.logging, , )

+2

log4j, , .

, .

, .

log4j.category.my.lib.package = INFO, libFileAppender
log4j.rootLogger = INFO, rootFileAppender

libFileAppender, rootFileAppender.

, rootFileAppender, , :

log4j.category.my.lib.package = INFO, libFileAppender
log4j.additivity.my.lib.package = false
log4j.rootLogger = INFO, rootFileAppender

libFileAppender

+3

All Articles