293739 - Deprecate static Jetty Log usage in favor of named logs

+ Adding StdErrLog.setPrintLongNames(boolean) to allow configuration
  of long form or condensed form log names.
This commit is contained in:
Joakim Erdfelt 2011-08-25 11:23:46 -07:00
parent f6d45d5650
commit 953b3512f7
5 changed files with 111 additions and 18 deletions

View File

@ -120,9 +120,19 @@ public class Log
return __log;
}
/**
* Get the root logger.
* @return the root logger
*/
public static Logger getRootLogger() {
initialized();
return __log;
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
static boolean isIgnored()
{
return __ignored;

View File

@ -32,7 +32,11 @@ import org.eclipse.jetty.util.DateCache;
* 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.
*/
public class StdErrLog implements Logger
{
@ -44,6 +48,8 @@ public class StdErrLog implements Logger
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>();
@ -61,7 +67,12 @@ public class StdErrLog implements Logger
private boolean _debug = __debug;
private boolean _source = __source;
// Print the long form names, otherwise use abbreviated
private boolean _printLongNames = __long;
// The full log name, as provided by the system.
private final String _name;
// The abbreviated log name (used by default, unless _long is specified)
private final String _abbrevname;
private boolean _hideStacks = false;
public StdErrLog()
@ -72,6 +83,7 @@ public class StdErrLog implements Logger
public StdErrLog(String name)
{
this._name = name == null ? "" : name;
this._abbrevname = condensePackageString(this._name);
try
{
@ -92,11 +104,52 @@ public class StdErrLog implements Logger
}
}
/**
* Condenses a classname by stripping down the package name to just the first character of each package name
* segment.
* <p>
*
* <pre>
* Examples:
* "org.eclipse.jetty.test.FooTest" = "oejt.FooTest"
* "org.eclipse.jetty.server.logging.LogTest" = "orjsl.LogTest"
* </pre>
*
* @param classname
* the fully qualified class name
* @return the condensed name
*/
protected static String condensePackageString(String classname)
{
String parts[] = classname.split("\\.");
StringBuilder dense = new StringBuilder();
for (int i = 0; i < (parts.length - 1); i++)
{
dense.append(parts[i].charAt(0));
}
if (dense.length() > 0)
{
dense.append('.');
}
dense.append(parts[parts.length - 1]);
return dense.toString();
}
public String getName()
{
return _name;
}
public void setPrintLongNames(boolean printLongNames)
{
this._printLongNames = printLongNames;
}
public boolean isPrintLongNames()
{
return this._printLongNames;
}
public boolean isHideStacks()
{
return _hideStacks;
@ -223,7 +276,13 @@ public class StdErrLog implements Logger
buffer.append(".0");
else
buffer.append(".00");
buffer.append(ms).append(tag).append(_name).append(':');
buffer.append(ms).append(tag);
if(_printLongNames) {
buffer.append(_name);
} else {
buffer.append(_abbrevname);
}
buffer.append(':');
if (_source)
{
Throwable source = new Throwable();
@ -234,10 +293,11 @@ public class StdErrLog implements Logger
String clazz = frame.getClassName();
if (clazz.equals(StdErrLog.class.getName())|| clazz.equals(Log.class.getName()))
continue;
if (clazz.startsWith("org.eclipse.jetty."))
buffer.append("o.e.j.").append(clazz,18,clazz.length());
else
if (!_printLongNames && clazz.startsWith("org.eclipse.jetty.")) {
buffer.append(condensePackageString(clazz));
} else {
buffer.append(clazz);
}
buffer.append('#').append(frame.getMethodName());
if (frame.getFileName()!=null)
buffer.append('(').append(frame.getFileName()).append(':').append(frame.getLineNumber()).append(')');
@ -328,6 +388,10 @@ public class StdErrLog implements Logger
if (logger==null)
{
StdErrLog sel=new StdErrLog(fullname);
// Preserve configuration for new loggers configuration
sel.setPrintLongNames(_printLongNames);
sel.setDebugEnabled(_debug);
sel.setSource(_source);
logger=__loggers.putIfAbsent(fullname,sel);
if (logger==null)
logger=sel;

View File

@ -13,7 +13,7 @@
package org.eclipse.jetty.util.log;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@ -77,28 +77,28 @@ public class LogTest
@Test
public void testStdErrLogFormat()
{
StdErrLog log = new StdErrLog("test");
StdErrLog log = new StdErrLog(LogTest.class.getName());
log.info("testing:{},{}","test","format");
logContains("INFO:test:testing:test,format");
logContains("INFO:oejul.LogTest:testing:test,format");
log.info("testing:{}","test","format");
logContains("INFO:test:testing:test format");
logContains("INFO:oejul.LogTest:testing:test format");
log.info("testing","test","format");
logContains("INFO:test:testing test format");
logContains("INFO:oejul.LogTest:testing test format");
log.info("testing:{},{}","test",null);
logContains("INFO:test:testing:test,null");
logContains("INFO:oejul.LogTest:testing:test,null");
log.info("testing {} {}",null,null);
logContains("INFO:test:testing null null");
logContains("INFO:oejul.LogTest:testing null null");
log.info("testing:{}",null,null);
logContains("INFO:test:testing:null");
logContains("INFO:oejul.LogTest:testing:null");
log.info("testing",null,null);
logContains("INFO:test:testing");
logContains("INFO:oejul.LogTest:testing");
}
@Test
@ -125,6 +125,7 @@ public class LogTest
public void testStdErrLogName()
{
StdErrLog log = new StdErrLog("test");
log.setPrintLongNames(true);
Assert.assertEquals("test",log.getName());
Logger next=log.getLogger("next");

View File

@ -1,6 +1,7 @@
package org.eclipse.jetty.util.log;
import static org.hamcrest.Matchers.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@ -14,18 +15,19 @@ public class NamedLogTest
private PrintStream orig;
private ByteArrayOutputStream logstream;
private PrintStream perr;
private Logger origLogger;
@Before
public void setUp()
{
origLogger = Log.getRootLogger();
orig = System.err;
logstream = new ByteArrayOutputStream();
perr = new PrintStream(logstream);
System.setErr(perr);
StdErrLog logger = new StdErrLog();
logger.setDebugEnabled(true);
logger.setHideStacks(false);
Log.setLog(logger);
}
@ -34,6 +36,8 @@ public class NamedLogTest
{
System.out.println(logstream.toString());
System.setErr(orig);
Log.setLog(origLogger);
}
@Test
@ -43,6 +47,10 @@ public class NamedLogTest
Green green = new Green();
Blue blue = new Blue();
setLoggerOptions(Red.class);
setLoggerOptions(Green.class);
setLoggerOptions(Blue.class);
red.generateLogs();
green.generateLogs();
blue.generateLogs();
@ -53,4 +61,14 @@ public class NamedLogTest
Assert.assertThat(rawlog,containsString(Green.class.getName()));
Assert.assertThat(rawlog,containsString(Blue.class.getName()));
}
private void setLoggerOptions(Class<?> clazz)
{
Logger logger = Log.getLogger(clazz);
logger.setDebugEnabled(true);
if(logger instanceof StdErrLog) {
((StdErrLog)logger).setPrintLongNames(true);
}
}
}

View File

@ -22,7 +22,7 @@ public class StdErrLogTest extends TestCase
{
public void testNullValues()
{
StdErrLog log = new StdErrLog();
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
log.setDebugEnabled(true);
log.setHideStacks(true);