CALL FOR:
 New package org.mmbase.util.logging.java.

Since MMBase 1.7 we are using java 1.4. In java 1.4 there is a logging
framework (java.util.logging).

I want to add support for this logging framework in a new package
org.mmbase.util.java.
    
I think there are two things which you may want:

Make MMBase log with java.util.logging.Logger's. This is possible with
the 'Impl' class of this package, which can be configured in log.xml,
and an own property file. So, this wraps java.util.logging.Logger in
org.mmbase.util.logging.Logger.

You have some class which asks for a java.util.logging.Logger where it will log 
to, and you
want it to log to the MMBase log. For this, the 'MMBaseLogger' class is meant. 
So, this
wraps org.mmbase.util.logging.Logger in java.util.logging.Logger.


So these are the classes I want to include in this package for now. I am
no experienced user in java.util.logging, so obviously bugfixes may
follow. The LoggerWrapper I actually needed for a projectq, the 'Impl' class I 
only
tested briefly with the ConsoleHandler of java.util.logging. The general
contract wants to be that org.mmbase.util.logging.java contains classes
related to java.util.logging.

This change is for MMBase 1.8.

Code attached.
 
START OF VOTING:   2005-01-24 13:30
END OF CALL:       2005-01-27 13:30

 [_] +1 (YES)
 [_] +0 (ABSTAIN )
 [_] -1 (NO), because :
 [_] VETO, because:

Michiel


-- 
Michiel Meeuwissen                  mihxil'
Mediacentrum 140 H'sum                [] ()
+31 (0)35 6772979         nl_NL eo_XX en_US



/*
This software is OSI Certified Open Source Software.
OSI Certified is a certification mark of the Open Source Initiative.

The license (Mozilla version 1.0) can be read at the MMBase site.
See http://www.MMBase.org/license

*/

package org.mmbase.util.logging.java;

import org.mmbase.util.logging.Logger;
import org.mmbase.util.logging.Level;
import org.mmbase.util.logging.Logging;


import java.io.*;

import org.mmbase.module.core.MMBaseContext;
import org.mmbase.util.ResourceWatcher;
import org.mmbase.util.ResourceLoader;

/**
 * Since java 1.4 there is a Logger implemented in java itself. This MMBase Logger implementation
 * delegates all logging to this java framework.
 *
 * @author Michiel Meeuwissen
 * @since MMBase-1.8
 */


public final class Impl implements Logger {

    private static Logger log = Logging.getLoggerInstance(Impl.class);
    private static ResourceWatcher configWatcher;

    private java.util.logging.Logger logger;


    /**
     * Constructor, like the constructor of [EMAIL PROTECTED] org.apache.log4j.Logger}.
     */

    protected Impl(String name) {
        logger = java.util.logging.Logger.getLogger(name);
    }



    /**
     * As getLogger, but casted to MMBase Logger already. And the possible
     * ClassCastException is caught.
     */
    public static Impl getLoggerInstance(String name) {
        return new Impl(name);
    }


    /**
     * Calls LogManager#readConfiguration, after setting the system property
     * 'java.util.logging.config.file'.
     **/

    public static void configure(String s) {
        if (s.equals("")) {
            System.out.println("Using default java logging configuration");
        } else {
            try {
                log.info("logging configurationfile : " + s);

                ResourceLoader rl = Logging.getResourceLoader();
        
                log.info("using " + rl + " for resolving " + s);
                configWatcher = new ResourceWatcher (rl) {
                        public void onChange(String s) {
                            try {
                                log.info("Reading configuration file : " + s);
                                java.util.logging.LogManager.getLogManager().readConfiguration(resourceLoader.getResourceAsStream(s));
                            } catch (IOException ioe) {
                                log.error(ioe);
                            }
                        }
                    };
                
                configWatcher.add(s);
                configWatcher.start();
            
                java.util.logging.LogManager.getLogManager().readConfiguration(rl.getResourceAsStream(s));
            } catch (IOException ioe) {
                log.error(ioe);
            }
        }
    }

    /**
     * @deprecated use setLevel
     **/

    public void setPriority(Level p) {
        setLevel(p);
    }
    public void setLevel(Level p) {
        switch (p.toInt()) {
        case Level.TRACE_INT:   logger.setLevel(java.util.logging.Level.FINER);   break;
        case Level.DEBUG_INT:   logger.setLevel(java.util.logging.Level.FINE);   break;
        case Level.SERVICE_INT:   logger.setLevel(java.util.logging.Level.CONFIG);   break;
        case Level.INFO_INT:   logger.setLevel(java.util.logging.Level.INFO);   break;
        case Level.WARN_INT:   logger.setLevel(java.util.logging.Level.WARNING);   break;
        case Level.ERROR_INT:   logger.setLevel(java.util.logging.Level.SEVERE);   break;
        case Level.FATAL_INT:   logger.setLevel(java.util.logging.Level.SEVERE);   break;
        }

    }


    public void trace (Object m) {
        logger.log(java.util.logging.Level.FINER, "" + m);
    }
    public void trace (Object m, Throwable t) {
        logger.log(java.util.logging.Level.FINER, "" + m, t);
    }
    public void debug (Object m) {
        logger.log(java.util.logging.Level.FINE, "" + m);
    }
    public void debug (Object m, Throwable t) {
        logger.log(java.util.logging.Level.FINE, "" + m, t);
    }

    public void service (Object m) {
        logger.log(java.util.logging.Level.CONFIG, "" + m);
    }
    public void service (Object m, Throwable t) {
        logger.log(java.util.logging.Level.CONFIG, "" + m, t);
    }
    public void info    (Object m) {
        logger.log(java.util.logging.Level.INFO, "" + m);
    }
    public void info    (Object m, Throwable t) {
        logger.log(java.util.logging.Level.INFO, "" + m, t);
    }
    public void warn    (Object m) {
        logger.log(java.util.logging.Level.WARNING, "" + m);
    }
    public void warn    (Object m, Throwable t) {
        logger.log(java.util.logging.Level.WARNING, "" + m, t);
    }
    public void error   (Object m) {
        logger.log(java.util.logging.Level.SEVERE, "" + m);
    }
    public void error   (Object m, Throwable t) {
        logger.log(java.util.logging.Level.SEVERE, "" + m, t);
    }
    public void fatal   (Object m) {
        logger.log(java.util.logging.Level.SEVERE, "" + m);
    }
    public void fatal   (Object m, Throwable t) {
        logger.log(java.util.logging.Level.SEVERE, "" + m, t);
    }

    private  final java.util.logging.Level getLevel() {
        java.util.logging.Level level = null;
        java.util.logging.Logger log = logger;
        while (level == null) {
            level = log.getLevel();
            log = log.getParent();
        }
        return level;
    }

    public boolean isDebugEnabled() {

        return (getLevel().intValue() <= java.util.logging.Level.FINE.intValue());
    }
    public boolean isServiceEnabled() {

        return (getLevel().intValue() <= java.util.logging.Level.CONFIG.intValue());
    }

    

}

/*
This software is OSI Certified Open Source Software.
OSI Certified is a certification mark of the Open Source Initiative.

The license (Mozilla version 1.0) can be read at the MMBase site.
See http://www.MMBase.org/license

*/

package org.mmbase.util.logging.java;

import org.mmbase.util.logging.*;
import java.util.logging.LogRecord;
import java.util.logging.Level;


/**
 * Since java 1.4 there is a Logger implemented in java itself. If you have code which requests a
 * java.util.logging.Logger object to which it will log to, and you want it to log the MMBase logger
 * then, you can offer it an instance of this class, which wraps an MMBase Logger object in a
 * java.util.logging.Logger object.
 *
 * @author Michiel Meeuwissen
 * @since MMBase-1.8
 */


public class MMBaseLogger extends java.util.logging.Logger {
    
    Logger log = null;
    public MMBaseLogger() {
        super(null, null);
    }

    public MMBaseLogger(Logger log) {
        super(null, null);
        this.log = log;
    }


    public void log(LogRecord record) {

        Logger l;
        if (log == null) {
            l = Logging.getLoggerInstance(record.getLoggerName());
        } else {
            l = log;
        }

        String message = record.getMessage();
        int level = record.getLevel().intValue();
        if (level >= Level.SEVERE.intValue()) {
            l.error(message);
        } else if (level >= Level.WARNING.intValue()) {
            l.warn(message);
        } else if (level >= Level.INFO.intValue()) {            
            l.info(message);
        } else if (level >= Level.CONFIG.intValue()) {
            l.service(message);
        } else if (level >= Level.FINE.intValue()) {
            l.debug(message);
        } else {
            l.trace(message);
        }            
    }

}

Title: org.mmbase.util.logging.java

Classes related to the java.util.logging package (new in java 1.4). There are two things which you may want:

  • Make MMBase log with java.util.logging.Logger's. This is possible with the 'Impl' class of this package, which can be configured in log.xml. So, this wraps java.util.logging.Logger in org.mmbase.util.logging.Logger.
  • You have some class which asks for a java.util.logging.Logger where it will log to, and you want it to log to the MMBase log. For this, the 'MMBaseLogger' class is meant. So, this wraps org.mmbase.util.logging.Logger in java.util.logging.Logger.
@since MMBase-1.8
_______________________________________________
Developers mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/developers

Reply via email to