Author: costin Date: Thu Oct 27 19:56:34 2005 New Revision: 329054 URL: http://svn.apache.org/viewcvs?rev=329054&view=rev Log: Handler and config for java.util.logging.
Added: tomcat/sandbox/java/org/apache/tomcat/ tomcat/sandbox/java/org/apache/tomcat/util/ tomcat/sandbox/java/org/apache/tomcat/util/log/ tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerConfig.java tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerFormatter.java Added: tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerConfig.java URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerConfig.java?rev=329054&view=auto ============================================================================== --- tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerConfig.java (added) +++ tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerConfig.java Thu Oct 27 19:56:34 2005 @@ -0,0 +1,108 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation. + * Copyright 2004 Costin Manolache + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tomcat.util.log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.logging.LogManager; + +/** + */ +public class JdkLoggerConfig { + + public JdkLoggerConfig() { + InputStream is=getConfig( "logging" ); + if( is!=null ) { + try { + LogManager.getLogManager().readConfiguration(is); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** Locate a config stream: + * + * Uses: + * - logging.configuration system property + * - ./logging.properties file + * - conf/logging.properties + * - logging.properties in classpath + * + * @return + */ + public static InputStream getConfig(String base) { + //String base="logging"; // "log4j" + // Initialize: + // 1. Find config file name + String confF=System.getProperty(base + ".configuration"); + if( confF!=null ) { + // Check if it is a path + } else { + confF=base + ".properties"; + } + + // URL + try { + URL url=new URL( confF ); + InputStream is=url.openStream(); + if( is!=null ) return is; + } catch( Throwable t ) { + + } + + // 2. Try to get the config from a file ( or conf/ ) + File f=new File( confF ); + if( ! f.exists() ) { + f=new File( "conf/" + confF ); + } + + if( f.exists() ) { + try { + return new FileInputStream( f ); + } catch (FileNotFoundException e) { + // ignore + } + } + + // 3. Load it from CLASSPATH + InputStream is=JdkLoggerConfig.class.getResourceAsStream( confF ); + + //No thread class loader + if( is!= null ) return is; + + f=new File( System.getProperty("java.home")); + f=new File( f, "lib/logging.properties"); + if( f.exists() ) { + try { + return new FileInputStream(f); + } catch (FileNotFoundException e) { + //e.printStackTrace(); + } + } + System.err.println("default logging doesn't exists" + f); + + return null; + } +} Added: tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerFormatter.java URL: http://svn.apache.org/viewcvs/tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerFormatter.java?rev=329054&view=auto ============================================================================== --- tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerFormatter.java (added) +++ tomcat/sandbox/java/org/apache/tomcat/util/log/JdkLoggerFormatter.java Thu Oct 27 19:56:34 2005 @@ -0,0 +1,109 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation. + * Copyright 2004 Costin Manolache + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tomcat.util.log; + +import java.util.logging.Formatter; +import java.util.logging.LogRecord; + + + +/** + * A more compact formatter. + * + * Equivalent log4j config: + * <pre> + * log4j.rootCategory=WARN, A1 + * log4j.appender.A1=org.apache.log4j.ConsoleAppender + * log4j.appender.A1.layout=org.apache.log4j.PatternLayout + * log4j.appender.A1.Target=System.err + * log4j.appender.A1.layout.ConversionPattern=%r %-15.15c{2} %-1.1p %m %n + * </pre> + * + * Example: + * 1130122891846 Http11BaseProtocol I Initializing Coyote HTTP/1.1 on http-8800 + * + * + * @author Costin Manolache + */ +public class JdkLoggerFormatter extends Formatter { + // values from JDK Level + public static final int LOG_LEVEL_TRACE = 400; + public static final int LOG_LEVEL_DEBUG = 500; + public static final int LOG_LEVEL_INFO = 800; + public static final int LOG_LEVEL_WARN = 900; + public static final int LOG_LEVEL_ERROR = 1000; + public static final int LOG_LEVEL_FATAL = 1000; + + public String format(LogRecord record) { + Throwable t=record.getThrown(); + int level=record.getLevel().intValue(); + String name=record.getLoggerName(); + long time=record.getMillis(); + String message=formatMessage(record); + + + if( name.indexOf(".") >= 0 ) + name = name.substring(name.lastIndexOf(".") + 1); + + // Use a string buffer for better performance + StringBuffer buf = new StringBuffer(); + + buf.append(time); + buf.append(" "); + + // pad to 8 to make it more readable + for( int i=0; i<8-buf.length(); i++ ) { buf.append(" "); } + + // Append the name of the log instance if so configured + buf.append(name); + + // pad to 20 chars + for( int i=0; i<8-buf.length(); i++ ) { buf.append(" "); } + + // Append a readable representation of the log level. + switch(level) { + case LOG_LEVEL_TRACE: buf.append(" T "); break; + case LOG_LEVEL_DEBUG: buf.append(" D "); break; + case LOG_LEVEL_INFO: buf.append(" I "); break; + case LOG_LEVEL_WARN: buf.append(" W "); break; + case LOG_LEVEL_ERROR: buf.append(" E "); break; + //case : buf.append(" F "); break; + default: buf.append(" "); + } + + + // Append the message + buf.append(message); + + // Append stack trace if not null + if(t != null) { + buf.append(" \n"); + + java.io.StringWriter sw= new java.io.StringWriter(1024); + java.io.PrintWriter pw= new java.io.PrintWriter(sw); + t.printStackTrace(pw); + pw.close(); + buf.append(sw.toString()); + } + + buf.append("\n"); + // Print to the appropriate destination + return buf.toString(); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]