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:
parent
5bc978b51f
commit
93a7bd5d15
|
@ -26,7 +26,7 @@ public interface Appender
|
||||||
|
|
||||||
String getId();
|
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;
|
void setProperty(String key, String value) throws Exception;
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,9 @@
|
||||||
package org.eclipse.jetty.logging.impl;
|
package org.eclipse.jetty.logging.impl;
|
||||||
|
|
||||||
import java.io.IOException;
|
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.MarkerIgnoringBase;
|
||||||
import org.slf4j.helpers.MessageFormatter;
|
import org.slf4j.helpers.MessageFormatter;
|
||||||
|
|
||||||
|
@ -27,23 +28,11 @@ import org.slf4j.helpers.MessageFormatter;
|
||||||
public class CentralLogger extends MarkerIgnoringBase
|
public class CentralLogger extends MarkerIgnoringBase
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = 385001265755850685L;
|
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 Severity level = Severity.INFO;
|
||||||
private String name;
|
private String name;
|
||||||
private Appender appenders[];
|
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)
|
protected CentralLogger(String name, Appender appenders[], Severity severity)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -53,14 +42,13 @@ public class CentralLogger extends MarkerIgnoringBase
|
||||||
|
|
||||||
private void log(Severity severity, String message, Throwable t)
|
private void log(Severity severity, String message, Throwable t)
|
||||||
{
|
{
|
||||||
String now = dateCache.now();
|
String now = new SimpleDateFormat(dateFormat).format(new Date());
|
||||||
int ms = dateCache.lastMs();
|
|
||||||
|
|
||||||
for (Appender appender : appenders)
|
for (Appender appender : appenders)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
appender.append(now,ms,severity,name,message,t);
|
appender.append(now,severity,name,message,t);
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,23 +34,10 @@ public class ConsoleAppender implements Appender
|
||||||
this.id = id;
|
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();
|
StringBuffer buf = new StringBuffer();
|
||||||
buf.append(date);
|
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(':').append(severity.name()).append(':');
|
||||||
buf.append(name);
|
buf.append(name);
|
||||||
buf.append(':').append(message);
|
buf.append(':').append(message);
|
||||||
|
|
|
@ -50,23 +50,10 @@ public class RollingFileAppender implements Appender
|
||||||
this.id = id;
|
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();
|
StringBuffer buf = new StringBuffer();
|
||||||
buf.append(date);
|
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(':').append(severity.name()).append(':');
|
||||||
buf.append(name);
|
buf.append(name);
|
||||||
buf.append(':').append(message);
|
buf.append(':').append(message);
|
||||||
|
|
|
@ -85,22 +85,22 @@ public class CentralizedLoggingTest extends TestCase
|
||||||
if (isSurefireExecuting)
|
if (isSurefireExecuting)
|
||||||
{
|
{
|
||||||
expectedLogs = new LogEvent[]
|
expectedLogs = new LogEvent[]
|
||||||
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
|
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null),
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"),
|
||||||
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
|
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
expectedLogs = new LogEvent[]
|
expectedLogs = new LogEvent[]
|
||||||
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized",null),
|
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested",null),
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested"),
|
||||||
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
|
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null),
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"),
|
||||||
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized",null),
|
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested",null),
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested"),
|
||||||
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
|
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
|
||||||
}
|
}
|
||||||
|
|
||||||
assertContainsLogEvents(testAppender,expectedLogs);
|
assertContainsLogEvents(testAppender,expectedLogs);
|
||||||
|
|
|
@ -147,14 +147,14 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
|
||||||
server.stop();
|
server.stop();
|
||||||
|
|
||||||
TestAppender.LogEvent expectedLogs[] =
|
TestAppender.LogEvent expectedLogs[] =
|
||||||
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized",null),
|
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested",null),
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested"),
|
||||||
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
|
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null),
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"),
|
||||||
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized",null),
|
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested",null),
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested"),
|
||||||
new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
|
new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
|
||||||
|
|
||||||
assertContainsLogEvents(testAppender,expectedLogs);
|
assertContainsLogEvents(testAppender,expectedLogs);
|
||||||
}
|
}
|
||||||
|
@ -170,8 +170,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
|
||||||
server.stop();
|
server.stop();
|
||||||
|
|
||||||
TestAppender.LogEvent expectedLogs[] =
|
TestAppender.LogEvent expectedLogs[] =
|
||||||
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized",null),
|
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested",null) };
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested") };
|
||||||
|
|
||||||
assertContainsLogEvents(testAppender,expectedLogs);
|
assertContainsLogEvents(testAppender,expectedLogs);
|
||||||
}
|
}
|
||||||
|
@ -187,8 +187,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
|
||||||
server.stop();
|
server.stop();
|
||||||
|
|
||||||
TestAppender.LogEvent expectedLogs[] =
|
TestAppender.LogEvent expectedLogs[] =
|
||||||
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized",null),
|
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested",null) };
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") };
|
||||||
|
|
||||||
assertContainsLogEvents(testAppender,expectedLogs);
|
assertContainsLogEvents(testAppender,expectedLogs);
|
||||||
}
|
}
|
||||||
|
@ -204,8 +204,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
|
||||||
server.stop();
|
server.stop();
|
||||||
|
|
||||||
TestAppender.LogEvent expectedLogs[] =
|
TestAppender.LogEvent expectedLogs[] =
|
||||||
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized",null),
|
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested",null) };
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested") };
|
||||||
|
|
||||||
assertContainsLogEvents(testAppender,expectedLogs);
|
assertContainsLogEvents(testAppender,expectedLogs);
|
||||||
}
|
}
|
||||||
|
@ -221,8 +221,8 @@ public class EmbeddedCentralizedLoggingTest extends TestCase
|
||||||
server.stop();
|
server.stop();
|
||||||
|
|
||||||
TestAppender.LogEvent expectedLogs[] =
|
TestAppender.LogEvent expectedLogs[] =
|
||||||
{ new LogEvent(null,-1,Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized",null),
|
{ new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"),
|
||||||
new LogEvent(null,-1,Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested",null) };
|
new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested") };
|
||||||
|
|
||||||
assertContainsLogEvents(testAppender,expectedLogs);
|
assertContainsLogEvents(testAppender,expectedLogs);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,23 +27,26 @@ public class TestAppender implements Appender
|
||||||
public static class LogEvent
|
public static class LogEvent
|
||||||
{
|
{
|
||||||
String date;
|
String date;
|
||||||
int ms;
|
|
||||||
Severity severity;
|
Severity severity;
|
||||||
String name;
|
String name;
|
||||||
String message;
|
String message;
|
||||||
Throwable t;
|
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();
|
super();
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.ms = ms;
|
|
||||||
this.severity = severity;
|
this.severity = severity;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
this.t = t;
|
this.t = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LogEvent(Severity severity, String name, String message)
|
||||||
|
{
|
||||||
|
this(null,severity,name,message,null);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
|
@ -68,7 +71,7 @@ public class TestAppender implements Appender
|
||||||
this.id = id;
|
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
|
if (name.equals("org.eclipse.jetty.util.log")) // standard jetty logger
|
||||||
{
|
{
|
||||||
|
@ -79,7 +82,7 @@ public class TestAppender implements Appender
|
||||||
}
|
}
|
||||||
return; // skip storing it.
|
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
|
public void close() throws IOException
|
||||||
|
|
Loading…
Reference in New Issue