1
0

Few small (?) fixes:

- for JDK1.4, include the correct class/method. This uses a hack to
extract the information from the stack trace - probably slow, but
it's better to get the correct information.

- for log4j, check if log4j is initialized ( by checking if any appenders
are present ). Set a default configuration if it is not initialized.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138884 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Costin Manolache
2002-05-06 21:32:37 +00:00
parent 739f6cecfe
commit b75a58b4af
3 changed files with 67 additions and 25 deletions

View File

@@ -3,7 +3,7 @@
<!--
"Logging" component of the Jakarta Commons Subproject
$Id: build.xml,v 1.13 2002/02/20 23:02:01 craigmcc Exp $
$Id: build.xml,v 1.14 2002/05/06 21:32:37 costin Exp $
-->
@@ -72,7 +72,7 @@
<property name="compile.deprecation" value="false"/>
<!-- Should Java compilations set the 'optimize' compiler option? -->
<property name="compile.optimize" value="true"/>
<property name="compile.optimize" value="false"/>
<!-- Construct compile classpath -->
<path id="compile.classpath">
@@ -170,6 +170,9 @@
<copy todir="${build.home}/classes" filtering="on">
<fileset dir="${source.home}" excludes="**/*.java"/>
</copy>
<jar jarfile="${build.home}/commons-${component.name}.jar"
basedir="${build.home}/classes"
manifest="${build.home}/conf/MANIFEST.MF"/>
</target>

View File

@@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/Jdk14Logger.java,v 1.2 2002/04/29 16:48:09 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2002/04/29 16:48:09 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/Jdk14Logger.java,v 1.3 2002/05/06 21:32:37 costin Exp $
* $Revision: 1.3 $
* $Date: 2002/05/06 21:32:37 $
*
* ====================================================================
*
@@ -77,7 +77,7 @@ import org.apache.commons.logging.Log;
* @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
* @version $Revision: 1.2 $ $Date: 2002/04/29 16:48:09 $
* @version $Revision: 1.3 $ $Date: 2002/05/06 21:32:37 $
*/
public final class Jdk14Logger implements Log {
@@ -109,13 +109,32 @@ public final class Jdk14Logger implements Log {
// --------------------------------------------------------- Public Methods
private void log( Level level, String msg, Throwable ex ) {
// Hack (?) to get the stack trace.
Throwable dummyException=new Throwable();
StackTraceElement locations[]=dummyException.getStackTrace();
// Caller will be the third element
String cname="unknown";
String method="unknown";
if( locations!=null && locations.length >2 ) {
StackTraceElement caller=locations[2];
cname=caller.getClassName();
method=caller.getMethodName();
}
if( ex==null ) {
logger.logp( level, cname, method, msg );
} else {
logger.logp( level, cname, method, msg, ex );
}
}
/**
* Log a message with debug log level.
*/
public void debug(Object message) {
logger.log(Level.FINE, message.toString());
log(Level.FINE, message.toString(), null);
}
@@ -125,7 +144,7 @@ public final class Jdk14Logger implements Log {
*/
public void debug(Object message, Throwable exception) {
logger.log(Level.FINE, message.toString(), exception);
log(Level.FINE, message.toString(), exception);
}
@@ -135,7 +154,7 @@ public final class Jdk14Logger implements Log {
*/
public void error(Object message) {
logger.log(Level.SEVERE, message.toString());
log(Level.SEVERE, message.toString(), null);
}
@@ -145,7 +164,7 @@ public final class Jdk14Logger implements Log {
*/
public void error(Object message, Throwable exception) {
logger.log(Level.SEVERE, message.toString(), exception);
log(Level.SEVERE, message.toString(), exception);
}
@@ -155,7 +174,7 @@ public final class Jdk14Logger implements Log {
*/
public void fatal(Object message) {
logger.log(Level.SEVERE, message.toString());
log(Level.SEVERE, message.toString(), null);
}
@@ -165,7 +184,7 @@ public final class Jdk14Logger implements Log {
*/
public void fatal(Object message, Throwable exception) {
logger.log(Level.SEVERE, message.toString(), exception);
log(Level.SEVERE, message.toString(), exception);
}
@@ -185,7 +204,7 @@ public final class Jdk14Logger implements Log {
*/
public void info(Object message) {
logger.log(Level.INFO, message.toString());
log(Level.INFO, message.toString(), null);
}
@@ -195,7 +214,7 @@ public final class Jdk14Logger implements Log {
*/
public void info(Object message, Throwable exception) {
logger.log(Level.INFO, message.toString(), exception);
log(Level.INFO, message.toString(), exception);
}
@@ -265,7 +284,7 @@ public final class Jdk14Logger implements Log {
*/
public void trace(Object message) {
logger.log(Level.FINEST, message.toString());
log(Level.FINEST, message.toString(), null);
}
@@ -275,7 +294,7 @@ public final class Jdk14Logger implements Log {
*/
public void trace(Object message, Throwable exception) {
logger.log(Level.FINEST, message.toString(), exception);
log(Level.FINEST, message.toString(), exception);
}
@@ -285,7 +304,7 @@ public final class Jdk14Logger implements Log {
*/
public void warn(Object message) {
logger.log(Level.WARNING, message.toString());
log(Level.WARNING, message.toString(), null);
}
@@ -295,7 +314,7 @@ public final class Jdk14Logger implements Log {
*/
public void warn(Object message, Throwable exception) {
logger.log(Level.WARNING, message.toString(), exception);
log(Level.WARNING, message.toString(), exception);
}

View File

@@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/Attic/Log4JCategoryLog.java,v 1.3 2002/03/07 22:32:47 costin Exp $
* $Revision: 1.3 $
* $Date: 2002/03/07 22:32:47 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/Attic/Log4JCategoryLog.java,v 1.4 2002/05/06 21:32:37 costin Exp $
* $Revision: 1.4 $
* $Date: 2002/05/06 21:32:37 $
*
* ====================================================================
*
@@ -62,9 +62,9 @@
package org.apache.commons.logging.impl;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.apache.log4j.*;
import org.apache.commons.logging.Log;
import java.util.Enumeration;
/**
* <p>Implementation of {@link Log} that maps directly to a Log4J
@@ -75,7 +75,7 @@ import org.apache.commons.logging.Log;
* @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
* @author Rod Waldhoff
* @author Robert Burrell Donkin
* @version $Id: Log4JCategoryLog.java,v 1.3 2002/03/07 22:32:47 costin Exp $
* @version $Id: Log4JCategoryLog.java,v 1.4 2002/05/06 21:32:37 costin Exp $
*/
public final class Log4JCategoryLog implements Log {
@@ -85,6 +85,8 @@ public final class Log4JCategoryLog implements Log {
/** The fully qualified name of the Log4JCategoryLog class. */
private static final String FQCN = Log4JCategoryLog.class.getName();
private static boolean initialized=false;
private static String LAYOUT="%r [%t] %p %c{2} %x - %m%n";
/** Log to this category */
private Category category = null;
@@ -97,18 +99,36 @@ public final class Log4JCategoryLog implements Log {
* Base constructor
*/
public Log4JCategoryLog(String name) {
category = Category.getInstance(name);
this( Category.getInstance(name));
}
/** For use with a log4j factory
*/
public Log4JCategoryLog(Category category ) {
if( ! initialized ) {
initialize();
}
this.category=category;
}
// ---------------------------------------------------------- Implmentation
private void initialize() {
Category root=Category.getRoot();
Enumeration appenders=root.getAllAppenders();
if( appenders==null || ! appenders.hasMoreElements() ) {
// No config, set some defaults ( consistent with
// commons-logging patterns ).
ConsoleAppender app=new ConsoleAppender(new PatternLayout( LAYOUT ),
ConsoleAppender.SYSTEM_ERR );
root.addAppender( app );
root.setPriority( Priority.INFO );
}
initialized=true;
}
/**
* Log a message to the Log4j Category with <code>TRACE</code> priority.