wqRevert "358649 - StdErrLog system properties for package/class logging LEVEL."

This reverts commit 12dbcadede.
This commit is contained in:
Jesse McConnell 2011-10-06 14:16:03 -05:00
parent 12dbcadede
commit 9ac4e35b96
2 changed files with 158 additions and 519 deletions

View File

@ -13,69 +13,59 @@
package org.eclipse.jetty.util.log;
import java.io.PrintStream;
import java.security.AccessControlException;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.eclipse.jetty.util.DateCache;
/**
* StdErr Logging. This implementation of the Logging facade sends all logs to StdErr with minimal formatting.
* StdErr Logging. This implementation of the Logging facade sends all logs to
* StdErr with minimal formatting.
* <p>
* If the system property "org.eclipse.jetty.LEVEL" is set to one of the following (ALL, DEBUG, INFO, WARN), then set
* the eclipse jetty root level logger level to that specified level. (Default level is INFO)
* If the system property "org.eclipse.jetty.util.log.DEBUG" is set, then debug
* logs are printed if stderr is being used. For named debuggers, the system
* property name+".DEBUG" is checked. If it is not not set, then
* "org.eclipse.jetty.util.log.DEBUG" is used as the default.
* <p>
* If the system property "org.eclipse.jetty.util.log.SOURCE" is set, then the source method/file of a log is logged.
* For named debuggers, the system property name+".SOURCE" is checked. If it is not not set, then
* If the system property "org.eclipse.jetty.util.log.SOURCE" is set, then the
* source method/file of a log is logged. For named debuggers, the system
* property name+".SOURCE" is checked. If it is not not set, then
* "org.eclipse.jetty.util.log.SOURCE" is used as the default.
* <p>
* If the system property "org.eclipse.jetty.util.log.LONG" is set, then the full, unabbreviated name of the logger is
* used for logging. For named debuggers, the system property name+".LONG" is checked. If it is not not set, then
* "org.eclipse.jetty.util.log.LONG" is used as the default.
* If the system property "org.eclipse.jetty.util.log.LONG" is set, then the
* full, unabbreviated name of the logger is used for logging.
* For named debuggers, the system property name+".LONG" is checked.
* If it is not not set, then "org.eclipse.jetty.util.log.LONG" is used as the default.
*/
public class StdErrLog implements Logger
{
private static DateCache _dateCache;
private final static boolean __source = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.SOURCE",
private final static boolean __debug = Boolean.parseBoolean(
System.getProperty("org.eclipse.jetty.util.log.DEBUG",
System.getProperty("org.eclipse.jetty.util.log.stderr.DEBUG", "false")));
private final static boolean __source = Boolean.parseBoolean(
System.getProperty("org.eclipse.jetty.util.log.SOURCE",
System.getProperty("org.eclipse.jetty.util.log.stderr.SOURCE", "false")));
private final static boolean __long = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.stderr.LONG","false"));
private final static boolean __long = Boolean.parseBoolean(
System.getProperty("org.eclipse.jetty.util.log.stderr.LONG", "false"));
private final static ConcurrentMap<String,StdErrLog> __loggers = new ConcurrentHashMap<String, StdErrLog>();
static
{
String deprecatedProperites[] =
{ "DEBUG", "org.eclipse.jetty.util.log.DEBUG", "org.eclipse.jetty.util.log.stderr.DEBUG" };
// Toss a message to users about deprecated system properties
for (String deprecatedProp : deprecatedProperites)
{
if (System.getProperty(deprecatedProp) != null)
{
System.err.printf("System Property [%s] has been deprecated! (Use org.eclipse.jetty.LEVEL=DEBUG instead)%n",deprecatedProp);
}
}
try
{
_dateCache = new DateCache("yyyy-MM-dd HH:mm:ss");
}
catch (Exception x)
{
x.printStackTrace(System.err);
x.printStackTrace();
}
}
public static final int LEVEL_ALL = 0;
public static final int LEVEL_DEBUG = 1;
public static final int LEVEL_INFO = 2;
public static final int LEVEL_WARN = 3;
private int _level = LEVEL_INFO;
private PrintStream _stderr = System.err;
private boolean _debug = __debug;
private boolean _source = __source;
// Print the long form names, otherwise use abbreviated
private boolean _printLongNames = __long;
@ -94,7 +84,15 @@ public class StdErrLog implements Logger
{
this._name = name == null ? "" : name;
this._abbrevname = condensePackageString(this._name);
this._level = getLoggingLevel(System.getProperties(),this._name);
try
{
_debug = Boolean.parseBoolean(System.getProperty(_name + ".DEBUG", Boolean.toString(_debug)));
}
catch (AccessControlException ace)
{
_debug = __debug;
}
try
{
@ -106,67 +104,9 @@ public class StdErrLog implements Logger
}
}
/**
* Get the Logging Level for the provided log name. Using the FQCN first, then each package segment from longest to
* shortest.
*
* @param props
* the properties to check
* @param name
* the name to get log for
* @return the logging level
*/
public static int getLoggingLevel(Properties props, final String name)
{
// Calculate the level this named logger should operate under.
// Checking with FQCN first, then each package segment from longest to shortest.
String nameSegment = name;
while ((nameSegment != null) && (nameSegment.length() > 0))
{
String levelStr = props.getProperty(nameSegment + ".LEVEL");
// System.err.printf("[StdErrLog.CONFIG] Checking for property [%s.LEVEL] = %s%n",nameSegment,levelStr);
if (levelStr == null)
{
// Trim and try again.
int idx = nameSegment.lastIndexOf('.');
if (idx >= 0)
{
nameSegment = nameSegment.substring(0,idx);
}
else
{
nameSegment = null;
}
}
else
{
if ("ALL".equalsIgnoreCase(levelStr.trim()))
{
return LEVEL_ALL;
}
else if ("DEBUG".equalsIgnoreCase(levelStr.trim()))
{
return LEVEL_DEBUG;
}
else if ("INFO".equalsIgnoreCase(levelStr.trim()))
{
return LEVEL_INFO;
}
else if ("WARN".equalsIgnoreCase(levelStr.trim()))
{
return LEVEL_WARN;
}
}
}
// Default Logging Level
return LEVEL_INFO;
}
/**
* Condenses a classname by stripping down the package name to just the first character of each package name
* segment.Configured
* segment.
* <p>
*
* <pre>
@ -221,9 +161,7 @@ public class StdErrLog implements Logger
}
/* ------------------------------------------------------------ */
/**
* Is the source of a log, logged
*
/** Is the source of a log, logged
* @return true if the class, method, file and line number of a log is logged.
*/
public boolean isSource()
@ -232,11 +170,8 @@ public class StdErrLog implements Logger
}
/* ------------------------------------------------------------ */
/**
* Set if a log source is logged.
*
* @param source
* true if the class, method, file and line number of a log is logged.
/** Set if a log source is logged.
* @param source true if the class, method, file and line number of a log is logged.
*/
public void setSource(boolean source)
{
@ -244,13 +179,10 @@ public class StdErrLog implements Logger
}
public void warn(String msg, Object... args)
{
if (_level <= LEVEL_WARN)
{
StringBuilder buffer = new StringBuilder(64);
format(buffer, ":WARN:", msg, args);
_stderr.println(buffer);
}
System.err.println(buffer);
}
public void warn(Throwable thrown)
@ -259,23 +191,17 @@ public class StdErrLog implements Logger
}
public void warn(String msg, Throwable thrown)
{
if (_level <= LEVEL_WARN)
{
StringBuilder buffer = new StringBuilder(64);
format(buffer, ":WARN:", msg, thrown);
_stderr.println(buffer);
}
System.err.println(buffer);
}
public void info(String msg, Object... args)
{
if (_level <= LEVEL_INFO)
{
StringBuilder buffer = new StringBuilder(64);
format(buffer, ":INFO:", msg, args);
_stderr.println(buffer);
}
System.err.println(buffer);
}
public void info(Throwable thrown)
@ -284,61 +210,29 @@ public class StdErrLog implements Logger
}
public void info(String msg, Throwable thrown)
{
if (_level <= LEVEL_INFO)
{
StringBuilder buffer = new StringBuilder(64);
format(buffer, ":INFO:", msg, thrown);
_stderr.println(buffer);
}
System.err.println(buffer);
}
public boolean isDebugEnabled()
{
return (_level >= LEVEL_DEBUG);
return _debug;
}
/**
* @deprecated use {@link #setLevel(int)} instead.
*/
@Deprecated
public void setDebugEnabled(boolean enabled)
{
_level = LEVEL_DEBUG;
}
public int getLevel()
{
return _level;
}
/**
* Set the level for this logger.
* <p>
* Available values ({@link StdErrLog#LEVEL_ALL}, {@link StdErrLog#LEVEL_DEBUG}, {@link StdErrLog#LEVEL_INFO},
* {@link StdErrLog#LEVEL_WARN})
*
* @param level
* the level to set the logger to
*/
public void setLevel(int level)
{
this._level = level;
}
public void setStdErrStream(PrintStream stream)
{
this._stderr = stream;
_debug = enabled;
}
public void debug(String msg, Object... args)
{
if (_level <= LEVEL_DEBUG)
{
if (!_debug)
return;
StringBuilder buffer = new StringBuilder(64);
format(buffer, ":DBUG:", msg, args);
_stderr.println(buffer);
}
System.err.println(buffer);
}
public void debug(Throwable thrown)
@ -348,12 +242,11 @@ public class StdErrLog implements Logger
public void debug(String msg, Throwable thrown)
{
if (_level <= LEVEL_DEBUG)
{
if (!_debug)
return;
StringBuilder buffer = new StringBuilder(64);
format(buffer, ":DBUG:", msg, thrown);
_stderr.println(buffer);
}
System.err.println(buffer);
}
private void format(StringBuilder buffer, String level, String msg, Object... args)
@ -368,38 +261,25 @@ public class StdErrLog implements Logger
{
format(buffer, level, msg);
if (isHideStacks())
{
format(buffer, String.valueOf(thrown));
}
else
{
format(buffer, thrown);
}
}
private void tag(StringBuilder buffer, String d, int ms, String tag)
{
buffer.setLength(0);
buffer.append(d);
if (ms > 99)
{
buffer.append('.');
}
else if (ms > 9)
{
buffer.append(".0");
}
else
{
buffer.append(".00");
}
buffer.append(ms).append(tag);
if (_printLongNames)
{
if(_printLongNames) {
buffer.append(_name);
}
else
{
} else {
buffer.append(_abbrevname);
}
buffer.append(':');
@ -412,22 +292,15 @@ public class StdErrLog implements Logger
final StackTraceElement frame = frames[i];
String clazz = frame.getClassName();
if (clazz.equals(StdErrLog.class.getName())|| clazz.equals(Log.class.getName()))
{
continue;
}
if (!_printLongNames && clazz.startsWith("org.eclipse.jetty."))
{
if (!_printLongNames && clazz.startsWith("org.eclipse.jetty.")) {
buffer.append(condensePackageString(clazz));
}
else
{
} else {
buffer.append(clazz);
}
buffer.append('#').append(frame.getMethodName());
if (frame.getFileName()!=null)
{
buffer.append('(').append(frame.getFileName()).append(':').append(frame.getLineNumber()).append(')');
}
buffer.append(':');
break;
}
@ -439,11 +312,9 @@ public class StdErrLog implements Logger
if (msg==null)
{
msg="";
for (int i = 0; i < args.length; i++)
{
for (Object o : args)
msg+="{} ";
}
}
String braces = "{}";
int start = 0;
for (Object arg : args)
@ -474,24 +345,16 @@ public class StdErrLog implements Logger
if (Character.isISOControl(c))
{
if (c == '\n')
{
builder.append('|');
}
else if (c == '\r')
{
builder.append('<');
}
else
{
builder.append('?');
}
}
else
{
builder.append(c);
}
}
}
private void format(StringBuilder buffer, Throwable thrown)
{
@ -524,9 +387,7 @@ public class StdErrLog implements Logger
String fullname=_name == null || _name.length() == 0?name:_name + "." + name;
if ((name == null && this._name == null) || fullname.equals(_name))
{
return this;
}
StdErrLog logger = __loggers.get(name);
if (logger==null)
@ -534,14 +395,12 @@ public class StdErrLog implements Logger
StdErrLog sel=new StdErrLog(fullname);
// Preserve configuration for new loggers configuration
sel.setPrintLongNames(_printLongNames);
sel.setLevel(_level);
sel.setDebugEnabled(_debug);
sel.setSource(_source);
logger=__loggers.putIfAbsent(fullname,sel);
if (logger==null)
{
logger=sel;
}
}
return logger;
}
@ -549,38 +408,18 @@ public class StdErrLog implements Logger
@Override
public String toString()
{
StringBuilder s = new StringBuilder();
s.append("StdErrLog:");
s.append(_name);
s.append(":LEVEL=");
switch (_level)
{
case LEVEL_ALL:
s.append("ALL");
break;
case LEVEL_DEBUG:
s.append("DEBUG");
break;
case LEVEL_INFO:
s.append("INFO");
break;
case LEVEL_WARN:
s.append("WARN");
break;
default:
s.append("?");
break;
}
return s.toString();
return "StdErrLog:" + _name + ":DEBUG=" + _debug;
}
public void ignore(Throwable ignored)
{
if (_level <= LEVEL_ALL)
if (Log.isIgnored())
{
StringBuilder buffer = new StringBuilder(64);
format(buffer,":IGNORED:","",ignored);
_stderr.println(buffer);
warn(Log.IGNORED, ignored);
}
else
{
debug("Ignored {}",ignored.toString());
}
}
}

View File

@ -13,31 +13,20 @@
package org.eclipse.jetty.util.log;
import static org.hamcrest.Matchers.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import org.junit.Assert;
import org.junit.Test;
import junit.framework.TestCase;
/**
* Tests for StdErrLog
*/
public class StdErrLogTest
* @author mgorovoy
* */
public class StdErrLogTest extends TestCase
{
/**
* Test to make sure that using a Null parameter on parameterized messages does not result in a NPE
*/
@Test
public void testParameterizedMessage_NullValues() throws NullPointerException
public void testNullValues()
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setLevel(StdErrLog.LEVEL_DEBUG);
log.setDebugEnabled(true);
log.setHideStacks(true);
try {
log.info("Testing info(msg,null,null) - {} {}","arg0","arg1");
log.info("Testing info(msg,null,null) - {} {}",null,null);
log.info("Testing info(msg,null,null) - {}",null,null);
@ -65,218 +54,29 @@ public class StdErrLogTest
log.warn("Testing warn(msg,null)");
log.warn(null,new Throwable("Testing warn(msg,thrw)").fillInStackTrace());
}
@Test
public void testGetLoggingLevel_Default()
catch (NullPointerException npe)
{
Properties props = new Properties();
// Default Levels
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,null));
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,""));
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
Assert.assertEquals("Default Logging Level",StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,StdErrLogTest.class.getName()));
System.err.println(npe);
npe.printStackTrace();
assertTrue("NullPointerException in StdErrLog.", false);
}
}
@Test
public void testGetLoggingLevel_FQCN()
{
String name = StdErrLogTest.class.getName();
Properties props = new Properties();
props.setProperty(name + ".LEVEL","ALL");
// Default Levels
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,null));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,""));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
// Specified Level
Assert.assertEquals(StdErrLog.LEVEL_ALL,StdErrLog.getLoggingLevel(props,name));
}
@Test
public void testGetLoggingLevel_UtilLevel()
{
Properties props = new Properties();
props.setProperty("org.eclipse.jetty.util.LEVEL","DEBUG");
// Default Levels
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,null));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,""));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.server.BogusObject"));
// Configured Level
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,StdErrLogTest.class.getName()));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util.Bogus"));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util"));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util.resource.FileResource"));
}
@Test
public void testGetLoggingLevel_MixedLevels()
{
Properties props = new Properties();
props.setProperty("org.eclipse.jetty.util.LEVEL","DEBUG");
props.setProperty("org.eclipse.jetty.util.ConcurrentHashMap.LEVEL","ALL");
// Default Levels
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,null));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,""));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty"));
Assert.assertEquals(StdErrLog.LEVEL_INFO,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.server.ServerObject"));
// Configured Level
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,StdErrLogTest.class.getName()));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util.MagicUtil"));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util"));
Assert.assertEquals(StdErrLog.LEVEL_DEBUG,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util.resource.FileResource"));
Assert.assertEquals(StdErrLog.LEVEL_ALL,StdErrLog.getLoggingLevel(props,"org.eclipse.jetty.util.ConcurrentHashMap"));
}
/**
* Tests StdErrLog.warn() methods with level filtering.
* <p>
* Should always see WARN level messages, regardless of set level.
*/
@Test
public void testWarnFiltering() throws UnsupportedEncodingException
public void testIgnores()
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(true);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
// Start with default level
log.warn("See Me");
// Set to debug level
log.setLevel(StdErrLog.LEVEL_DEBUG);
log.warn("Hear Me");
// Set to warn level
log.setLevel(StdErrLog.LEVEL_WARN);
log.warn("Cheer Me");
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
System.err.print(output);
Assert.assertThat(output,containsString("See Me"));
Assert.assertThat(output,containsString("Hear Me"));
Assert.assertThat(output,containsString("Cheer Me"));
}
/**
* Tests StdErrLog.info() methods with level filtering.
* <p>
* Should only see INFO level messages when level is set to {@link StdErrLog#LEVEL_INFO} and below.
*/
@Test
public void testInfoFiltering() throws UnsupportedEncodingException
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(true);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
// Normal/Default behavior
log.info("I will not buy");
// Level Debug
log.setLevel(StdErrLog.LEVEL_DEBUG);
log.info("this record");
// Level All
log.setLevel(StdErrLog.LEVEL_ALL);
log.info("it is scratched.");
// Level Warn
log.setLevel(StdErrLog.LEVEL_WARN);
log.info("sorry?");
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
System.err.print(output);
Assert.assertThat(output,containsString("I will not buy"));
Assert.assertThat(output,containsString("this record"));
Assert.assertThat(output,containsString("it is scratched."));
Assert.assertThat(output,not(containsString("sorry?")));
}
/**
* Tests StdErrLog.debug() methods with level filtering.
* <p>
* Should only see DEBUG level messages when level is set to {@link StdErrLog#LEVEL_DEBUG} and below.
*/
@Test
public void testDebugFiltering() throws UnsupportedEncodingException
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(true);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
// Normal/Default behavior
log.debug("Tobacconist");
// Level Debug
log.setLevel(StdErrLog.LEVEL_DEBUG);
log.debug("my hovercraft is");
// Level All
log.setLevel(StdErrLog.LEVEL_ALL);
log.debug("full of eels.");
// Level Warn
log.setLevel(StdErrLog.LEVEL_WARN);
log.debug("what?");
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
System.err.print(output);
Assert.assertThat(output,not(containsString("Tobacconist")));
Assert.assertThat(output,containsString("my hovercraft is"));
Assert.assertThat(output,containsString("full of eels."));
Assert.assertThat(output,not(containsString("what?")));
}
/**
* Tests StdErrLog with {@link Logger#ignore(Throwable)} use.
* <p>
* Should only see IGNORED level messages when level is set to {@link StdErrLog#LEVEL_ALL}.
*/
@Test
public void testIgnores() throws UnsupportedEncodingException
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(true);
ByteArrayOutputStream test = new ByteArrayOutputStream();
PrintStream err = new PrintStream(test);
log.setStdErrStream(err);
// Normal/Default behavior
Log.__ignored=false;
log.setDebugEnabled(false);
log.ignore(new Throwable("IGNORE ME"));
// Show Ignored
log.setLevel(StdErrLog.LEVEL_ALL);
Log.__ignored=true;
log.setDebugEnabled(false);
log.ignore(new Throwable("Don't ignore me"));
// Set to Debug level
log.setLevel(StdErrLog.LEVEL_DEBUG);
Log.__ignored=false;
log.setDebugEnabled(true);
log.ignore(new Throwable("Debug me"));
// Validate Output
String output = new String(test.toByteArray(),"UTF-8");
System.err.print(output);
Assert.assertThat(output,not(containsString("IGNORE ME")));
Assert.assertThat(output,containsString("Don't ignore me"));
Assert.assertThat(output,not(containsString("Debug me")));
}
}