incubator-adffaces-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frank Felix Debatin" <...@gmx.net>
Subject RE: Logging system used by Trinidad
Date Tue, 04 Jul 2006 08:22:57 GMT
Hi Cosma, 

I worked around this by writing a simplistic log handler
that accepts JDK log messages and send them to commons
logging. 

Feel free to use, below.

Frank Felix 

_______________________

package whatever;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Writes JDK log messages to commons logging.
 */
public class JDKToCommonsLogging 
{
    static JDKLogHandler activeHandler;
    
    /**
     * Activates this feature. 
     */
    public static void activate() 
    {
        try
        {
            Logger rootLogger =
LogManager.getLogManager().getLogger("");
            // remove old handlers
            for (Handler handler: rootLogger.getHandlers())
            {
                rootLogger.removeHandler(handler);
            }
            // add our own
            activeHandler = new JDKLogHandler();
            rootLogger.addHandler(activeHandler);
            // done, let's check it right away!!!
 
Logger.getLogger(JDKToCommonsLogging.class.getName())
                .info("activated: sending JDK log messages
to Commons Logging");
        }
        catch (Exception exc)
        {
            LogFactory.getLog(JDKToCommonsLogging.class)
                .error("activation failed", exc);
        }
    }
    
    public static void deactivate()
    {
        Logger rootLogger =
LogManager.getLogManager().getLogger("");
        rootLogger.removeHandler(activeHandler);
 
Logger.getLogger(JDKToCommonsLogging.class.getName())
            .info("dactivated");
    }
    
    private static class JDKLogHandler extends Handler
    {
        private Map<String, Log> cachedLogs = new
ConcurrentHashMap<String, Log>();
        
        private Log getLog(String logName)
        {
            Log log = cachedLogs.get(logName);
            if (log==null)
            {
                log = LogFactory.getLog(logName);
                cachedLogs.put(logName, log);
            }
            return log;
        }
        
        @Override
        public void publish(LogRecord record)
        {
            Log log = getLog(record.getLoggerName());
            String message = record.getMessage();
            Throwable exception = record.getThrown();
            Level level = record.getLevel();
            if (level==Level.SEVERE){
                log.error(message, exception);
            } else if (level==Level.WARNING) {
                log.warn(message, exception);
            } else if (level==Level.INFO) {
                log.info(message, exception);
            } else if (level==Level.CONFIG) {
                log.debug(message, exception);
            } else {
                log.trace(message, exception);
            }
        }
    
        @Override
        public void flush()
        {
           // nothing to do
        }
    
        @Override
        public void close()
        {
            // nothing to do
        }
    }
}


Mime
View raw message