1
0

Log to stderr.

A bit of change in reading the properties - I spent some time trying to
understand what was the original intention, it was quite tricky. If the
property is not set, the old code would have defaulted everything to
true.

Added a bit of code to display only the last component of the log name,
logs without log name are hard to trace back to the source, and the full
name can make things hard to read ( at least that's my experience so
far ).

Of course, feel free to change back, I'm just trying to get things
a bit easier to use 'out of box', config can override anything.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138868 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Costin Manolache
2002-02-15 05:46:36 +00:00
parent f7489ad021
commit accadb23ed

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/SimpleLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/impl/SimpleLog.java,v 1.2 2002/02/15 05:46:36 costin Exp $
* $Revision: 1.1 $ * $Revision: 1.2 $
* $Date: 2002/02/03 01:31:29 $ * $Date: 2002/02/15 05:46:36 $
* *
* ==================================================================== * ====================================================================
* *
@@ -73,23 +73,26 @@ import org.apache.commons.logging.Log;
/** /**
* <p>Simple implementation of Log that sends all enabled log messages, * <p>Simple implementation of Log that sends all enabled log messages,
* for all defined loggers, to System.out. The following system properties * for all defined loggers, to System.err. The following system properties
* are supported to configure the behavior of this logger:</p> * are supported to configure the behavior of this logger:</p>
* <ul> * <ul>
* <li><code>org.apache.commons.logging.simplelog.defaultlog</code> - * <li><code>org.apache.commons.logging.simplelog.defaultlog</code> -
* Default logging detail level for all instances of SimpleLog. * Default logging detail level for all instances of SimpleLog.
* Must be one of ("trace", "debug", "info", "warn", "error", or "fatal"). * Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
* If not specified, defaults to "error". </li> * If not specified, defaults to "info". </li>
* <li><code>org.apache.commons.logging.simplelog.log.xxxxx</code> - * <li><code>org.apache.commons.logging.simplelog.log.xxxxx</code> -
* Logging detail level for a SimpleLog instance named "xxxxx". * Logging detail level for a SimpleLog instance named "xxxxx".
* Must be one of ("trace", "debug", "info", "warn", "error", or "fatal"). * Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
* If not specified, the default logging detail level is used.</li> * If not specified, the default logging detail level is used.</li>
* <li><code>org.apache.commons.logging.simplelog.showlogname</code> - * <li><code>org.apache.commons.logging.simplelog.showlogname</code> -
* Set to <code>true</code> if you want the Log instance name to be * Set to <code>true</code> if you want the Log instance name to be
* included in output messages.</li> * included in output messages. Defaults to false</li>
* <li><code>org.apache.commons.logging.simplelog.showShortLogname</code> -
* Set to <code>true</code> if you want the last componet of the name to be
* included in output messages. Defaults to true.</li>
* <li><code>org.apache.commons.logging.simplelog.showdatetime</code> - * <li><code>org.apache.commons.logging.simplelog.showdatetime</code> -
* Set to <code>true</code> if you want the current date and time * Set to <code>true</code> if you want the current date and time
* to be included in output messages.</li> * to be included in output messages. Default is false.</li>
* </ul> * </ul>
* *
* <p>In addition to looking for system properties with the names specified * <p>In addition to looking for system properties with the names specified
@@ -101,7 +104,7 @@ import org.apache.commons.logging.Log;
* @author Rod Waldhoff * @author Rod Waldhoff
* @author Robert Burrell Donkin * @author Robert Burrell Donkin
* *
* @version $Id: SimpleLog.java,v 1.1 2002/02/03 01:31:29 sanders Exp $ * @version $Id: SimpleLog.java,v 1.2 2002/02/15 05:46:36 costin Exp $
*/ */
public class SimpleLog implements Log { public class SimpleLog implements Log {
@@ -116,6 +119,11 @@ public class SimpleLog implements Log {
static protected final Properties simpleLogProps = new Properties(); static protected final Properties simpleLogProps = new Properties();
/** Include the instance name in the log message? */ /** Include the instance name in the log message? */
static protected boolean showLogName = false; static protected boolean showLogName = false;
/** Include the short name ( last component ) of the logger in the log
message. Default to true - otherwise we'll be lost in a flood of
messages without knowing who sends them.
*/
static protected boolean showShortName = true;
/** Include the current time in the log message */ /** Include the current time in the log message */
static protected boolean showDateTime = false; static protected boolean showDateTime = false;
/** Used to format times */ /** Used to format times */
@@ -168,13 +176,28 @@ public class SimpleLog implements Log {
} }
} }
showLogName = "true".equalsIgnoreCase( /* That's a strange way to set properties. If the property
simpleLogProps.getProperty( is not set, we'll override the default
showLogName = "true".equalsIgnoreCase(
simpleLogProps.getProperty(
systemPrefix + "showlogname","true")); systemPrefix + "showlogname","true"));
*/
showDateTime = "true".equalsIgnoreCase( String prop=simpleLogProps.getProperty( systemPrefix + "showlogname");
simpleLogProps.getProperty(
systemPrefix + "showdatetime","true")); if( prop!= null )
showLogName = "true".equalsIgnoreCase(prop);
prop=simpleLogProps.getProperty( systemPrefix + "showShortLogname");
if( prop!=null ) {
showShortName = "true".equalsIgnoreCase(prop);
}
prop=simpleLogProps.getProperty( systemPrefix + "showdatetime");
if( prop!=null ) {
showDateTime = "true".equalsIgnoreCase(prop);
}
if(showDateTime) { if(showDateTime) {
dateFormatter = new SimpleDateFormat( dateFormatter = new SimpleDateFormat(
@@ -191,7 +214,9 @@ public class SimpleLog implements Log {
/** The current log level */ /** The current log level */
protected int currentLogLevel; protected int currentLogLevel;
private String prefix=null;
// ------------------------------------------------------------ Constructor // ------------------------------------------------------------ Constructor
/** /**
@@ -204,8 +229,9 @@ public class SimpleLog implements Log {
logName = name; logName = name;
// set initial log level // set initial log level
// set default log level to ERROR // Used to be: set default log level to ERROR
setLevel(SimpleLog.LOG_LEVEL_ERROR); // IMHO it should be lower, but at least info ( costin ).
setLevel(SimpleLog.LOG_LEVEL_INFO);
// set log level from properties // set log level from properties
String lvl = simpleLogProps.getProperty(systemPrefix + "log." + logName); String lvl = simpleLogProps.getProperty(systemPrefix + "log." + logName);
@@ -251,6 +277,7 @@ public class SimpleLog implements Log {
public void setLevel(int currentLogLevel) { public void setLevel(int currentLogLevel) {
this.currentLogLevel = currentLogLevel; this.currentLogLevel = currentLogLevel;
} }
@@ -269,7 +296,7 @@ public class SimpleLog implements Log {
/** /**
* <p> Do the actual logging. * <p> Do the actual logging.
* This method assembles the message * This method assembles the message
* and then prints to <code>System.out</code>.</p> * and then prints to <code>System.err</code>.</p>
*/ */
protected void log(int type, Object message, Throwable t) { protected void log(int type, Object message, Throwable t) {
// use a string buffer for better performance // use a string buffer for better performance
@@ -292,7 +319,14 @@ public class SimpleLog implements Log {
} }
// append the name of the log instance if so configured // append the name of the log instance if so configured
if(showLogName) { if( showShortName) {
if( prefix==null ) {
// cut all but the last component of the name for both styles
prefix = logName.substring( logName.lastIndexOf(".") +1) + " - ";
prefix = prefix.substring( prefix.lastIndexOf("/") +1) + "-";
}
buf.append( prefix );
} else if(showLogName) {
buf.append(String.valueOf(logName)).append(" - "); buf.append(String.valueOf(logName)).append(" - ");
} }
@@ -307,8 +341,8 @@ public class SimpleLog implements Log {
t.printStackTrace(); t.printStackTrace();
} }
// print to System.out // print to System.err
System.out.println(buf.toString()); System.err.println(buf.toString());
} }