Adding tests for Named Logging

This commit is contained in:
Joakim Erdfelt 2011-08-24 09:31:28 -07:00
parent 644694b704
commit 786098e9f1
6 changed files with 114 additions and 0 deletions

7
jetty-util/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.project
.classpath
.settings/
.pmd
target/
*.swp
*.log

View File

@ -327,6 +327,18 @@ public class Log
return;
__log.warn(EXCEPTION, th);
}
/**
* Obtain a named Logger based on the fully qualified class name.
*
* @param clazz
* the class to base the Logger name off of
* @return the Logger with the given name
*/
public static Logger getLogger(Class<?> clazz)
{
return getLogger(clazz.getName());
}
/**
* Obtain a named Logger or the default Logger if null is passed.

View File

@ -0,0 +1,13 @@
package org.eclipse.jetty.util.log;
public class Blue
{
private static final Logger LOG = Log.getLogger(Blue.class);
public void generateLogs() {
LOG.debug("My color is {}", Blue.class.getSimpleName());
LOG.info("I represent the emotion Admiration");
LOG.warn("I can also mean Disgust");
LOG.ignore(new RuntimeException("Yawn"));
}
}

View File

@ -0,0 +1,13 @@
package org.eclipse.jetty.util.log;
public class Green
{
private static final Logger LOG = Log.getLogger(Green.class);
public void generateLogs() {
LOG.debug("My color is {}", Green.class.getSimpleName());
LOG.info("I represent the emotion Trust");
LOG.warn("I can also mean Fear");
LOG.ignore(new RuntimeException("Ick"));
}
}

View File

@ -0,0 +1,56 @@
package org.eclipse.jetty.util.log;
import static org.hamcrest.Matchers.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class NamedLogTest
{
private PrintStream orig;
private ByteArrayOutputStream logstream;
private PrintStream perr;
@Before
public void setUp()
{
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);
}
@After
public void tearDown()
{
System.out.println(logstream.toString());
System.setErr(orig);
}
@Test
public void testNamedLogging()
{
Red red = new Red();
Green green = new Green();
Blue blue = new Blue();
red.generateLogs();
green.generateLogs();
blue.generateLogs();
String rawlog = logstream.toString();
Assert.assertThat(rawlog,containsString(Red.class.getName()));
Assert.assertThat(rawlog,containsString(Green.class.getName()));
Assert.assertThat(rawlog,containsString(Blue.class.getName()));
}
}

View File

@ -0,0 +1,13 @@
package org.eclipse.jetty.util.log;
public class Red
{
private static final Logger LOG = Log.getLogger(Red.class);
public void generateLogs() {
LOG.debug("My color is {}", Red.class.getSimpleName());
LOG.info("I represent the emotion Love");
LOG.warn("I can also mean Anger");
LOG.ignore(new RuntimeException("Nom"));
}
}