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; package org.eclipse.jetty.util.log;
import java.io.PrintStream;
import java.security.AccessControlException; import java.security.AccessControlException;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import org.eclipse.jetty.util.DateCache; 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> * <p>
* If the system property "org.eclipse.jetty.LEVEL" is set to one of the following (ALL, DEBUG, INFO, WARN), then set * If the system property "org.eclipse.jetty.util.log.DEBUG" is set, then debug
* the eclipse jetty root level logger level to that specified level. (Default level is INFO) * 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> * <p>
* If the system property "org.eclipse.jetty.util.log.SOURCE" is set, then the source method/file of a log is logged. * If the system property "org.eclipse.jetty.util.log.SOURCE" is set, then the
* For named debuggers, the system property name+".SOURCE" is checked. If it is not not set, then * 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. * "org.eclipse.jetty.util.log.SOURCE" is used as the default.
* <p> * <p>
* If the system property "org.eclipse.jetty.util.log.LONG" is set, then the full, unabbreviated name of the logger is * If the system property "org.eclipse.jetty.util.log.LONG" is set, then the
* used for logging. For named debuggers, the system property name+".LONG" is checked. If it is not not set, then * full, unabbreviated name of the logger is used for logging.
* "org.eclipse.jetty.util.log.LONG" is used as the default. * 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 public class StdErrLog implements Logger
{ {
private static DateCache _dateCache; 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.stderr.SOURCE","false"))); System.getProperty("org.eclipse.jetty.util.log.DEBUG",
private final static boolean __long = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.stderr.LONG","false")); 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 ConcurrentMap<String, StdErrLog> __loggers = new ConcurrentHashMap<String, StdErrLog>(); private final static ConcurrentMap<String,StdErrLog> __loggers = new ConcurrentHashMap<String, StdErrLog>();
static 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 try
{ {
_dateCache = new DateCache("yyyy-MM-dd HH:mm:ss"); _dateCache = new DateCache("yyyy-MM-dd HH:mm:ss");
} }
catch (Exception x) catch (Exception x)
{ {
x.printStackTrace(System.err); x.printStackTrace();
} }
} }
public static final int LEVEL_ALL = 0; private boolean _debug = __debug;
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 _source = __source; private boolean _source = __source;
// Print the long form names, otherwise use abbreviated // Print the long form names, otherwise use abbreviated
private boolean _printLongNames = __long; private boolean _printLongNames = __long;
@ -92,13 +82,21 @@ public class StdErrLog implements Logger
public StdErrLog(String name) public StdErrLog(String name)
{ {
this._name = name == null?"":name; this._name = name == null ? "" : name;
this._abbrevname = condensePackageString(this._name); this._abbrevname = condensePackageString(this._name);
this._level = getLoggingLevel(System.getProperties(),this._name);
try try
{ {
_source = Boolean.parseBoolean(System.getProperty(_name + ".SOURCE",Boolean.toString(_source))); _debug = Boolean.parseBoolean(System.getProperty(_name + ".DEBUG", Boolean.toString(_debug)));
}
catch (AccessControlException ace)
{
_debug = __debug;
}
try
{
_source = Boolean.parseBoolean(System.getProperty(_name + ".SOURCE", Boolean.toString(_source)));
} }
catch (AccessControlException ace) catch (AccessControlException ace)
{ {
@ -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 * Condenses a classname by stripping down the package name to just the first character of each package name
* segment.Configured * segment.
* <p> * <p>
* *
* <pre> * <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. * @return true if the class, method, file and line number of a log is logged.
*/ */
public boolean isSource() public boolean isSource()
@ -232,11 +170,8 @@ public class StdErrLog implements Logger
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /** Set if a log source 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.
*
* @param source
* true if the class, method, file and line number of a log is logged.
*/ */
public void setSource(boolean source) public void setSource(boolean source)
{ {
@ -245,136 +180,90 @@ public class StdErrLog implements Logger
public void warn(String msg, Object... args) public void warn(String msg, Object... args)
{ {
if (_level <= LEVEL_WARN) StringBuilder buffer = new StringBuilder(64);
{ format(buffer, ":WARN:", msg, args);
StringBuilder buffer = new StringBuilder(64); System.err.println(buffer);
format(buffer,":WARN:",msg,args);
_stderr.println(buffer);
}
} }
public void warn(Throwable thrown) public void warn(Throwable thrown)
{ {
warn("",thrown); warn("", thrown);
} }
public void warn(String msg, Throwable thrown) public void warn(String msg, Throwable thrown)
{ {
if (_level <= LEVEL_WARN) StringBuilder buffer = new StringBuilder(64);
{ format(buffer, ":WARN:", msg, thrown);
StringBuilder buffer = new StringBuilder(64); System.err.println(buffer);
format(buffer,":WARN:",msg,thrown);
_stderr.println(buffer);
}
} }
public void info(String msg, Object... args) public void info(String msg, Object... args)
{ {
if (_level <= LEVEL_INFO) StringBuilder buffer = new StringBuilder(64);
{ format(buffer, ":INFO:", msg, args);
StringBuilder buffer = new StringBuilder(64); System.err.println(buffer);
format(buffer,":INFO:",msg,args);
_stderr.println(buffer);
}
} }
public void info(Throwable thrown) public void info(Throwable thrown)
{ {
info("",thrown); info("", thrown);
} }
public void info(String msg, Throwable thrown) public void info(String msg, Throwable thrown)
{ {
if (_level <= LEVEL_INFO) StringBuilder buffer = new StringBuilder(64);
{ format(buffer, ":INFO:", msg, thrown);
StringBuilder buffer = new StringBuilder(64); System.err.println(buffer);
format(buffer,":INFO:",msg,thrown);
_stderr.println(buffer);
}
} }
public boolean isDebugEnabled() public boolean isDebugEnabled()
{ {
return (_level >= LEVEL_DEBUG); return _debug;
} }
/**
* @deprecated use {@link #setLevel(int)} instead.
*/
@Deprecated
public void setDebugEnabled(boolean enabled) public void setDebugEnabled(boolean enabled)
{ {
_level = LEVEL_DEBUG; _debug = enabled;
}
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;
} }
public void debug(String msg, Object... args) public void debug(String msg, Object... args)
{ {
if (_level <= LEVEL_DEBUG) if (!_debug)
{ return;
StringBuilder buffer = new StringBuilder(64); StringBuilder buffer = new StringBuilder(64);
format(buffer,":DBUG:",msg,args); format(buffer, ":DBUG:", msg, args);
_stderr.println(buffer); System.err.println(buffer);
}
} }
public void debug(Throwable thrown) public void debug(Throwable thrown)
{ {
debug("",thrown); debug("", thrown);
} }
public void debug(String msg, Throwable thrown) public void debug(String msg, Throwable thrown)
{ {
if (_level <= LEVEL_DEBUG) if (!_debug)
{ return;
StringBuilder buffer = new StringBuilder(64); StringBuilder buffer = new StringBuilder(64);
format(buffer,":DBUG:",msg,thrown); format(buffer, ":DBUG:", msg, thrown);
_stderr.println(buffer); System.err.println(buffer);
}
} }
private void format(StringBuilder buffer, String level, String msg, Object... args) private void format(StringBuilder buffer, String level, String msg, Object... args)
{ {
String d = _dateCache.now(); String d = _dateCache.now();
int ms = _dateCache.lastMs(); int ms = _dateCache.lastMs();
tag(buffer,d,ms,level); tag(buffer, d, ms, level);
format(buffer,msg,args); format(buffer, msg, args);
} }
private void format(StringBuilder buffer, String level, String msg, Throwable thrown) private void format(StringBuilder buffer, String level, String msg, Throwable thrown)
{ {
format(buffer,level,msg); format(buffer, level, msg);
if (isHideStacks()) if (isHideStacks())
{ format(buffer, String.valueOf(thrown));
format(buffer,String.valueOf(thrown));
}
else else
{ format(buffer, thrown);
format(buffer,thrown);
}
} }
private void tag(StringBuilder buffer, String d, int ms, String tag) private void tag(StringBuilder buffer, String d, int ms, String tag)
@ -382,52 +271,36 @@ public class StdErrLog implements Logger
buffer.setLength(0); buffer.setLength(0);
buffer.append(d); buffer.append(d);
if (ms > 99) if (ms > 99)
{
buffer.append('.'); buffer.append('.');
}
else if (ms > 9) else if (ms > 9)
{
buffer.append(".0"); buffer.append(".0");
}
else else
{
buffer.append(".00"); buffer.append(".00");
}
buffer.append(ms).append(tag); buffer.append(ms).append(tag);
if (_printLongNames) if(_printLongNames) {
{
buffer.append(_name); buffer.append(_name);
} } else {
else
{
buffer.append(_abbrevname); buffer.append(_abbrevname);
} }
buffer.append(':'); buffer.append(':');
if (_source) if (_source)
{ {
Throwable source = new Throwable(); Throwable source = new Throwable();
StackTraceElement[] frames = source.getStackTrace(); StackTraceElement[] frames = source.getStackTrace();
for (int i = 0; i < frames.length; i++) for (int i=0;i<frames.length;i++)
{ {
final StackTraceElement frame = frames[i]; final StackTraceElement frame = frames[i];
String clazz = frame.getClassName(); String clazz = frame.getClassName();
if (clazz.equals(StdErrLog.class.getName()) || clazz.equals(Log.class.getName())) if (clazz.equals(StdErrLog.class.getName())|| clazz.equals(Log.class.getName()))
{
continue; continue;
} if (!_printLongNames && clazz.startsWith("org.eclipse.jetty.")) {
if (!_printLongNames && clazz.startsWith("org.eclipse.jetty."))
{
buffer.append(condensePackageString(clazz)); buffer.append(condensePackageString(clazz));
} } else {
else
{
buffer.append(clazz); buffer.append(clazz);
} }
buffer.append('#').append(frame.getMethodName()); buffer.append('#').append(frame.getMethodName());
if (frame.getFileName() != null) if (frame.getFileName()!=null)
{
buffer.append('(').append(frame.getFileName()).append(':').append(frame.getLineNumber()).append(')'); buffer.append('(').append(frame.getFileName()).append(':').append(frame.getLineNumber()).append(')');
}
buffer.append(':'); buffer.append(':');
break; break;
} }
@ -436,34 +309,32 @@ public class StdErrLog implements Logger
private void format(StringBuilder builder, String msg, Object... args) private void format(StringBuilder builder, String msg, Object... args)
{ {
if (msg == null) if (msg==null)
{ {
msg = ""; msg="";
for (int i = 0; i < args.length; i++) for (Object o : args)
{ msg+="{} ";
msg += "{} ";
}
} }
String braces = "{}"; String braces = "{}";
int start = 0; int start = 0;
for (Object arg : args) for (Object arg : args)
{ {
int bracesIndex = msg.indexOf(braces,start); int bracesIndex = msg.indexOf(braces, start);
if (bracesIndex < 0) if (bracesIndex < 0)
{ {
escape(builder,msg.substring(start)); escape(builder, msg.substring(start));
builder.append(" "); builder.append(" ");
builder.append(arg); builder.append(arg);
start = msg.length(); start = msg.length();
} }
else else
{ {
escape(builder,msg.substring(start,bracesIndex)); escape(builder, msg.substring(start, bracesIndex));
builder.append(String.valueOf(arg)); builder.append(String.valueOf(arg));
start = bracesIndex + braces.length(); start = bracesIndex + braces.length();
} }
} }
escape(builder,msg.substring(start)); escape(builder, msg.substring(start));
} }
private void escape(StringBuilder builder, String string) private void escape(StringBuilder builder, String string)
@ -474,22 +345,14 @@ public class StdErrLog implements Logger
if (Character.isISOControl(c)) if (Character.isISOControl(c))
{ {
if (c == '\n') if (c == '\n')
{
builder.append('|'); builder.append('|');
}
else if (c == '\r') else if (c == '\r')
{
builder.append('<'); builder.append('<');
}
else else
{
builder.append('?'); builder.append('?');
}
} }
else else
{
builder.append(c); builder.append(c);
}
} }
} }
@ -502,16 +365,16 @@ public class StdErrLog implements Logger
else else
{ {
buffer.append('\n'); buffer.append('\n');
format(buffer,thrown.toString()); format(buffer, thrown.toString());
StackTraceElement[] elements = thrown.getStackTrace(); StackTraceElement[] elements = thrown.getStackTrace();
for (int i = 0; elements != null && i < elements.length; i++) for (int i = 0; elements != null && i < elements.length; i++)
{ {
buffer.append("\n\tat "); buffer.append("\n\tat ");
format(buffer,elements[i].toString()); format(buffer, elements[i].toString());
} }
Throwable cause = thrown.getCause(); Throwable cause = thrown.getCause();
if (cause != null && cause != thrown) if (cause!=null && cause!=thrown)
{ {
buffer.append("\nCaused by: "); buffer.append("\nCaused by: ");
format(buffer,cause); format(buffer,cause);
@ -521,26 +384,22 @@ public class StdErrLog implements Logger
public Logger getLogger(String name) public Logger getLogger(String name)
{ {
String fullname = _name == null || _name.length() == 0?name:_name + "." + name; String fullname=_name == null || _name.length() == 0?name:_name + "." + name;
if ((name == null && this._name == null) || fullname.equals(_name)) if ((name == null && this._name == null) || fullname.equals(_name))
{
return this; return this;
}
StdErrLog logger = __loggers.get(name); StdErrLog logger = __loggers.get(name);
if (logger == null) if (logger==null)
{ {
StdErrLog sel = new StdErrLog(fullname); StdErrLog sel=new StdErrLog(fullname);
// Preserve configuration for new loggers configuration // Preserve configuration for new loggers configuration
sel.setPrintLongNames(_printLongNames); sel.setPrintLongNames(_printLongNames);
sel.setLevel(_level); sel.setDebugEnabled(_debug);
sel.setSource(_source); sel.setSource(_source);
logger = __loggers.putIfAbsent(fullname,sel); logger=__loggers.putIfAbsent(fullname,sel);
if (logger == null) if (logger==null)
{ logger=sel;
logger = sel;
}
} }
return logger; return logger;
@ -549,38 +408,18 @@ public class StdErrLog implements Logger
@Override @Override
public String toString() public String toString()
{ {
StringBuilder s = new StringBuilder(); return "StdErrLog:" + _name + ":DEBUG=" + _debug;
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();
} }
public void ignore(Throwable ignored) public void ignore(Throwable ignored)
{ {
if (_level <= LEVEL_ALL) if (Log.isIgnored())
{ {
StringBuilder buffer = new StringBuilder(64); warn(Log.IGNORED, ignored);
format(buffer,":IGNORED:","",ignored);
_stderr.println(buffer);
} }
else
{
debug("Ignored {}",ignored.toString());
}
} }
} }

View File

@ -13,270 +13,70 @@
package org.eclipse.jetty.util.log; package org.eclipse.jetty.util.log;
import static org.hamcrest.Matchers.*; import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import org.junit.Assert;
import org.junit.Test;
/** /**
* Tests for StdErrLog * @author mgorovoy
*/ * */
public class StdErrLogTest public class StdErrLogTest extends TestCase
{ {
/** public void testNullValues()
* 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
{ {
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName()); StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setLevel(StdErrLog.LEVEL_DEBUG); log.setDebugEnabled(true);
log.setHideStacks(true); log.setHideStacks(true);
log.info("Testing info(msg,null,null) - {} {}","arg0","arg1"); try {
log.info("Testing info(msg,null,null) - {} {}",null,null); 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);
log.info("Testing info(msg,null,null)",null,null); log.info("Testing info(msg,null,null) - {}",null,null);
log.info(null,"Testing","info(null,arg0,arg1)"); log.info("Testing info(msg,null,null)",null,null);
log.info(null,null,null); log.info(null,"Testing","info(null,arg0,arg1)");
log.info(null,null,null);
log.debug("Testing debug(msg,null,null) - {} {}","arg0","arg1"); log.debug("Testing debug(msg,null,null) - {} {}","arg0","arg1");
log.debug("Testing debug(msg,null,null) - {} {}",null,null); log.debug("Testing debug(msg,null,null) - {} {}",null,null);
log.debug("Testing debug(msg,null,null) - {}",null,null); log.debug("Testing debug(msg,null,null) - {}",null,null);
log.debug("Testing debug(msg,null,null)",null,null); log.debug("Testing debug(msg,null,null)",null,null);
log.debug(null,"Testing","debug(null,arg0,arg1)"); log.debug(null,"Testing","debug(null,arg0,arg1)");
log.debug(null,null,null); log.debug(null,null,null);
log.debug("Testing debug(msg,null)"); log.debug("Testing debug(msg,null)");
log.debug(null,new Throwable("Testing debug(null,thrw)").fillInStackTrace()); log.debug(null,new Throwable("Testing debug(null,thrw)").fillInStackTrace());
log.warn("Testing warn(msg,null,null) - {} {}","arg0","arg1"); log.warn("Testing warn(msg,null,null) - {} {}","arg0","arg1");
log.warn("Testing warn(msg,null,null) - {} {}",null,null); log.warn("Testing warn(msg,null,null) - {} {}",null,null);
log.warn("Testing warn(msg,null,null) - {}",null,null); log.warn("Testing warn(msg,null,null) - {}",null,null);
log.warn("Testing warn(msg,null,null)",null,null); log.warn("Testing warn(msg,null,null)",null,null);
log.warn(null,"Testing","warn(msg,arg0,arg1)"); log.warn(null,"Testing","warn(msg,arg0,arg1)");
log.warn(null,null,null); log.warn(null,null,null);
log.warn("Testing warn(msg,null)"); log.warn("Testing warn(msg,null)");
log.warn(null,new Throwable("Testing warn(msg,thrw)").fillInStackTrace()); log.warn(null,new Throwable("Testing warn(msg,thrw)").fillInStackTrace());
}
catch (NullPointerException npe)
{
System.err.println(npe);
npe.printStackTrace();
assertTrue("NullPointerException in StdErrLog.", false);
}
} }
@Test public void testIgnores()
public void testGetLoggingLevel_Default()
{
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()));
}
@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
{ {
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName()); StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setHideStacks(true); log.setHideStacks(true);
ByteArrayOutputStream test = new ByteArrayOutputStream(); Log.__ignored=false;
PrintStream err = new PrintStream(test); log.setDebugEnabled(false);
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.ignore(new Throwable("IGNORE ME")); log.ignore(new Throwable("IGNORE ME"));
// Show Ignored Log.__ignored=true;
log.setLevel(StdErrLog.LEVEL_ALL); log.setDebugEnabled(false);
log.ignore(new Throwable("Don't ignore me")); log.ignore(new Throwable("Don't ignore me"));
// Set to Debug level Log.__ignored=false;
log.setLevel(StdErrLog.LEVEL_DEBUG); log.setDebugEnabled(true);
log.ignore(new Throwable("Debug me")); 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")));
} }
} }