Removing dependency on jetty-util's DateCache.

* Removing dependency on DateCache so that the jetty-centralized-logging
  can be used by projecs that want to utilize endorsed libraries,
  without bringing all of jetty into the endorsed lib area.

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@869 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Joakim Erdfelt 2009-09-10 23:46:38 +00:00
parent 5bc978b51f
commit 93a7bd5d15
7 changed files with 44 additions and 79 deletions

View File

@ -26,7 +26,7 @@ public interface Appender
String getId();
void append(String date, int ms, Severity severity, String name, String message, Throwable t) throws IOException;
void append(String date, Severity severity, String name, String message, Throwable t) throws IOException;
void setProperty(String key, String value) throws Exception;

View File

@ -16,8 +16,9 @@
package org.eclipse.jetty.logging.impl;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.eclipse.jetty.util.DateCache;
import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.helpers.MessageFormatter;
@ -27,23 +28,11 @@ import org.slf4j.helpers.MessageFormatter;
public class CentralLogger extends MarkerIgnoringBase
{
private static final long serialVersionUID = 385001265755850685L;
private static DateCache dateCache;
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
private Severity level = Severity.INFO;
private String name;
private Appender appenders[];
static
{
try
{
dateCache = new DateCache("yyyy-MM-dd HH:mm:ss");
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
protected CentralLogger(String name, Appender appenders[], Severity severity)
{
this.name = name;
@ -53,14 +42,13 @@ public class CentralLogger extends MarkerIgnoringBase
private void log(Severity severity, String message, Throwable t)
{
String now = dateCache.now();
int ms = dateCache.lastMs();
String now = new SimpleDateFormat(dateFormat).format(new Date());
for (Appender appender : appenders)
{
try
{
appender.append(now,ms,severity,name,message,t);
appender.append(now,severity,name,message,t);
}
catch (IOException e)
{

View File

@ -34,23 +34,10 @@ public class ConsoleAppender implements Appender
this.id = id;
}
public void append(String date, int ms, Severity severity, String name, String message, Throwable t)
public void append(String date, Severity severity, String name, String message, Throwable t)
{
StringBuffer buf = new StringBuffer();
buf.append(date);
if (ms > 99)
{
buf.append(".");
}
else if (ms > 0)
{
buf.append(".0");
}
else
{
buf.append(".00");
}
buf.append(ms);
buf.append(':').append(severity.name()).append(':');
buf.append(name);
buf.append(':').append(message);

View File

@ -50,23 +50,10 @@ public class RollingFileAppender implements Appender
this.id = id;
}
public void append(String date, int ms, Severity severity, String name, String message, Throwable t) throws IOException
public void append(String date, Severity severity, String name, String message, Throwable t) throws IOException
{
StringBuffer buf = new StringBuffer();
buf.append(date);
if (ms > 99)
{
buf.append(".");
}
else if (ms > 0)
{
buf.append(".0");
}
else
{
buf.append(".00");
}
buf.append(ms);
buf.append(':').append(severity.name()).append(':');
buf.append(name);
buf.append(':').append(message);

View File

@ -85,22 +85,22 @@ public class CentralizedLoggingTest extends TestCase
if (isSurefireExecuting)
{
expectedLogs = new LogEvent[]
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null),
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"),
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
}
else
{
expectedLogs = new LogEvent[]
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested",null),
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null),
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested",null),
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested"),
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"),
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested"),
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
}
assertContainsLogEvents(testAppender,expectedLogs);

View File

@ -147,14 +147,14 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
server.stop();
TestAppender.LogEvent expectedLogs[] =
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested",null),
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null),
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested",null),
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested"),
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"),
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested"),
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
assertContainsLogEvents(testAppender,expectedLogs);
}
@ -170,8 +170,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
server.stop();
TestAppender.LogEvent expectedLogs[] =
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested",null) };
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested") };
assertContainsLogEvents(testAppender,expectedLogs);
}
@ -187,8 +187,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
server.stop();
TestAppender.LogEvent expectedLogs[] =
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
assertContainsLogEvents(testAppender,expectedLogs);
}
@ -204,8 +204,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
server.stop();
TestAppender.LogEvent expectedLogs[] =
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested",null) };
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested") };
assertContainsLogEvents(testAppender,expectedLogs);
}
@ -221,8 +221,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
server.stop();
TestAppender.LogEvent expectedLogs[] =
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null) };
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested") };
assertContainsLogEvents(testAppender,expectedLogs);
}

View File

@ -27,23 +27,26 @@ public class TestAppender implements Appender
public static class LogEvent
{
String date;
int ms;
Severity severity;
String name;
String message;
Throwable t;
public LogEvent(String date, int ms, Severity severity, String name, String message, Throwable t)
public LogEvent(String date, Severity severity, String name, String message, Throwable t)
{
super();
this.date = date;
this.ms = ms;
this.severity = severity;
this.name = name;
this.message = message;
this.t = t;
}
public LogEvent(Severity severity, String name, String message)
{
this(null,severity,name,message,null);
}
@Override
public String toString()
{
@ -68,7 +71,7 @@ public class TestAppender implements Appender
this.id = id;
}
public void append(String date, int ms, Severity severity, String name, String message, Throwable t)
public void append(String date, Severity severity, String name, String message, Throwable t)
{
if (name.equals("org.eclipse.jetty.util.log")) // standard jetty logger
{
@ -79,7 +82,7 @@ public class TestAppender implements Appender
}
return; // skip storing it.
}
events.add(new LogEvent(date,ms,severity,name,message,t));
events.add(new LogEvent(date,severity,name,message,t));
}
public void close() throws IOException