Skip to content

Commit 2159777

Browse files
Added simple event viewer servlet.
1 parent 7585b25 commit 2159777

File tree

10 files changed

+302
-26
lines changed

10 files changed

+302
-26
lines changed

logback-amqp-appender/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@
88
</parent>
99
<artifactId>logback-amqp-appender</artifactId>
1010
<dependencies>
11-
<dependency>
12-
<groupId>ch.qos.logback</groupId>
13-
<artifactId>logback-core</artifactId>
14-
<version>0.9.28</version>
15-
</dependency>
16-
<dependency>
17-
<groupId>ch.qos.logback</groupId>
18-
<artifactId>logback-classic</artifactId>
19-
<version>0.9.28</version>
20-
</dependency>
2111
<dependency>
2212
<groupId>com.rabbitmq</groupId>
2313
<artifactId>amqp-client</artifactId>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
package ch.qos.logback.amqp.tools;
3+
4+
5+
import ch.qos.logback.classic.Level;
6+
import org.slf4j.Logger;
7+
8+
9+
public class DefaultLoggerCallbacks
10+
implements
11+
Callbacks
12+
{
13+
public DefaultLoggerCallbacks (final Logger logger)
14+
{
15+
super ();
16+
this.logger = logger;
17+
}
18+
19+
public void handleException (final Throwable exception, final String messageFormat, final Object ... messageArguments)
20+
{
21+
this.handleLogEvent (Level.ERROR, exception, messageFormat, messageArguments);
22+
}
23+
24+
public void handleLogEvent (
25+
final Level level, final Throwable exception, final String messageFormat, final Object ... messageArguments)
26+
{
27+
final String message = String.format (messageFormat, messageArguments);
28+
switch (level.levelInt) {
29+
case Level.ERROR_INT :
30+
if (exception != null)
31+
this.logger.error (message, exception);
32+
else
33+
this.logger.error (message);
34+
break;
35+
case Level.WARN_INT :
36+
if (exception != null)
37+
this.logger.warn (message, exception);
38+
else
39+
this.logger.warn (message);
40+
break;
41+
case Level.INFO_INT :
42+
if (exception != null)
43+
this.logger.info (message, exception);
44+
else
45+
this.logger.info (message);
46+
break;
47+
case Level.DEBUG_INT :
48+
if (exception != null)
49+
this.logger.debug (message, exception);
50+
else
51+
this.logger.debug (message);
52+
break;
53+
case Level.TRACE_INT :
54+
if (exception != null)
55+
this.logger.trace (message, exception);
56+
else
57+
this.logger.trace (message);
58+
break;
59+
default:
60+
if (exception != null)
61+
this.logger.info (message, exception);
62+
else
63+
this.logger.info (message);
64+
break;
65+
}
66+
}
67+
68+
protected Logger logger;
69+
}

logback-amqp-consumer/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@
1313
<artifactId>logback-amqp-appender</artifactId>
1414
<version>${project.version}</version>
1515
</dependency>
16-
<dependency>
17-
<groupId>ch.qos.logback</groupId>
18-
<artifactId>logback-core</artifactId>
19-
<version>0.9.28</version>
20-
</dependency>
21-
<dependency>
22-
<groupId>ch.qos.logback</groupId>
23-
<artifactId>logback-classic</artifactId>
24-
<version>0.9.28</version>
25-
</dependency>
2616
<dependency>
2717
<groupId>com.rabbitmq</groupId>
2818
<artifactId>amqp-client</artifactId>

logback-amqp-consumer/src/main/java/ch/qos/logback/amqp/AmqpConsumer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public AmqpConsumer (
2222
final LinkedBlockingQueue<AmqpMessage> sink)
2323
{
2424
super (host, port, virtualHost, username, password, callbacks);
25-
this.exchange = exchange;
26-
this.queue = queue;
27-
this.routingKey = routingKey;
25+
this.exchange = (exchange != null) ? exchange : "logback";
26+
this.queue = (queue != null) ? queue : "";
27+
this.routingKey = (routingKey != null) ? routingKey : "#";
2828
this.queue1 = null;
2929
this.sink = sink;
3030
}

logback-amqp-consumer/src/main/java/ch/qos/logback/amqp/AmqpConsumerAgentMain.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ public static final void main (final String[] arguments)
7272
public static final class AgentAction
7373
extends Action
7474
{
75-
public AgentAction (final List<AmqpConsumerAgent> agents)
75+
public AgentAction ()
76+
{
77+
this (null, true);
78+
}
79+
80+
public AgentAction (final List<AmqpConsumerAgent> agents, final boolean autoStart)
7681
{
7782
super ();
7883
this.agents = agents;
84+
this.autoStart = autoStart;
7985
this.agent = null;
8086
}
8187

@@ -94,11 +100,15 @@ public void end (final InterpretationContext ic, final String name)
94100
throw (new IllegalStateException ());
95101
if (ic.popObject () != this.agent)
96102
throw (new IllegalStateException ());
97-
this.agents.add (this.agent);
103+
if (this.autoStart)
104+
this.agent.start ();
105+
if (this.agents != null)
106+
this.agents.add (this.agent);
98107
this.agent = null;
99108
}
100109

101110
private AmqpConsumerAgent agent;
111+
private final boolean autoStart;
102112
private final List<AmqpConsumerAgent> agents;
103113
}
104114

@@ -108,7 +118,7 @@ public static final class Configurator
108118
public Configurator (final List<AmqpConsumerAgent> agents)
109119
{
110120
super ();
111-
this.agentAction = new AgentAction (agents);
121+
this.agentAction = new AgentAction (agents, false);
112122
this.agentAction.setContext (this.getContext ());
113123
}
114124

logback-webapp/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>ch.qos.logback.amqp</groupId>
6+
<artifactId>logback-amqp-parent</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>logback-webapp</artifactId>
10+
<packaging>war</packaging>
11+
<dependencies>
12+
<dependency>
13+
<groupId>ch.qos.logback.amqp</groupId>
14+
<artifactId>logback-amqp-consumer</artifactId>
15+
<version>${project.version}</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>javax.servlet</groupId>
19+
<artifactId>servlet-api</artifactId>
20+
<version>2.5</version>
21+
<scope>provided</scope>
22+
</dependency>
23+
</dependencies>
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>net.sf.alchim</groupId>
28+
<artifactId>winstone-maven-plugin</artifactId>
29+
<version>1.2</version>
30+
<configuration>
31+
<cmdLineOptions>
32+
<property>
33+
<name>httpPort</name>
34+
<value>8181</value>
35+
</property>
36+
<property>
37+
<name>ajp13Port</name>
38+
<value>-1</value>
39+
</property>
40+
<property>
41+
<name>controlPort</name>
42+
<value>-1</value>
43+
</property>
44+
<property>
45+
<name>directoryListings</name>
46+
<value>false</value>
47+
</property>
48+
<property>
49+
<name>useInvoker</name>
50+
<value>false</value>
51+
</property>
52+
</cmdLineOptions>
53+
</configuration>
54+
<executions>
55+
<execution>
56+
<phase>package</phase>
57+
<goals>
58+
<goal>embed</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
package ch.qos.logback.webapp;
3+
4+
5+
import java.io.IOException;
6+
import java.io.PrintWriter;
7+
import java.util.LinkedList;
8+
import java.util.concurrent.LinkedBlockingQueue;
9+
10+
import javax.servlet.http.HttpServlet;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
import ch.qos.logback.classic.Logger;
15+
import ch.qos.logback.classic.LoggerContext;
16+
import ch.qos.logback.classic.html.HTMLLayout;
17+
import ch.qos.logback.classic.spi.ILoggingEvent;
18+
import ch.qos.logback.core.AppenderBase;
19+
import org.slf4j.LoggerFactory;
20+
21+
22+
public class EventViewer
23+
extends HttpServlet
24+
{
25+
public EventViewer ()
26+
{
27+
super ();
28+
this.events = new LinkedList<ILoggingEvent> ();
29+
this.rootLogger = (Logger) LoggerFactory.getLogger (org.slf4j.Logger.ROOT_LOGGER_NAME);
30+
final LoggerContext context = this.rootLogger.getLoggerContext ();
31+
this.appender = new Appender (this.events);
32+
this.appender.setContext (context);
33+
this.appender.start ();
34+
this.rootLogger.addAppender (this.appender);
35+
this.layout = new HTMLLayout ();
36+
this.layout.setContext (context);
37+
this.layout.start ();
38+
}
39+
40+
protected void doGet (final HttpServletRequest request, final HttpServletResponse response)
41+
throws IOException
42+
{
43+
this.appender.drain ();
44+
response.setHeader ("Content-Type", "text/html");
45+
final PrintWriter stream = response.getWriter ();
46+
stream.write (this.layout.getFileHeader ());
47+
stream.write (this.layout.getPresentationHeader ());
48+
for (final ILoggingEvent event : this.events)
49+
stream.write (this.layout.doLayout (event));
50+
stream.write (this.layout.getPresentationFooter ());
51+
stream.write (this.layout.getFileFooter ());
52+
stream.close ();
53+
}
54+
55+
private final Appender appender;
56+
private final LinkedList<ILoggingEvent> events;
57+
private final HTMLLayout layout;
58+
private final Logger rootLogger;
59+
60+
private static final long serialVersionUID = 1L;
61+
62+
public static class Appender
63+
extends AppenderBase<ILoggingEvent>
64+
{
65+
public Appender (final LinkedList<ILoggingEvent> events)
66+
{
67+
super ();
68+
this.events = events;
69+
this.buffer = new LinkedBlockingQueue<ILoggingEvent> ();
70+
}
71+
72+
public void drain ()
73+
{
74+
while (true) {
75+
final ILoggingEvent event = this.buffer.poll ();
76+
if (event == null)
77+
break;
78+
this.events.add (event);
79+
}
80+
}
81+
82+
protected void append (final ILoggingEvent event)
83+
{
84+
this.buffer.add (event);
85+
}
86+
87+
private final LinkedBlockingQueue<ILoggingEvent> buffer;
88+
private final LinkedList<ILoggingEvent> events;
89+
}
90+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<configuration debug="false">
4+
5+
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
6+
7+
<newRule pattern="/configuration/amqpConsumerAgent"
8+
actionClass="ch.qos.logback.amqp.AmqpConsumerAgentMain$AgentAction"/>
9+
10+
<amqpConsumerAgent>
11+
<host>127.0.0.1</host>
12+
<port>5672</port>
13+
<virtualHost>/</virtualHost>
14+
<username>guest</username>
15+
<password>guest</password>
16+
<exchange>logback</exchange>
17+
<queue></queue>
18+
<routingKey>#</routingKey>
19+
</amqpConsumerAgent>
20+
21+
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
22+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
23+
<pattern>[%mdc{application}/%mdc{component}/%mdc{node}] [%mdc{sequence}/%relative/%d{HH:mm:ss.SSS}] [%level] [%logger] %msg%n</pattern>
24+
</encoder>
25+
</appender>
26+
27+
<root level="trace">
28+
<!-- <appender-ref ref="console" /> -->
29+
</root>
30+
31+
</configuration>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
4+
5+
<web-app>
6+
7+
<display-name>Logback WebApp</display-name>
8+
9+
<servlet>
10+
<servlet-name>event-viewer</servlet-name>
11+
<servlet-class>ch.qos.logback.webapp.EventViewer</servlet-class>
12+
<load-on-startup>0</load-on-startup>
13+
</servlet>
14+
15+
<servlet-mapping>
16+
<servlet-name>event-viewer</servlet-name>
17+
<url-pattern>/logback</url-pattern>
18+
</servlet-mapping>
19+
20+
</web-app>

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@
88
<modules>
99
<module>logback-amqp-appender</module>
1010
<module>logback-amqp-consumer</module>
11+
<module>logback-webapp</module>
1112
</modules>
1213
<dependencies>
14+
<dependency>
15+
<groupId>ch.qos.logback</groupId>
16+
<artifactId>logback-core</artifactId>
17+
<version>0.9.28</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>ch.qos.logback</groupId>
21+
<artifactId>logback-classic</artifactId>
22+
<version>0.9.28</version>
23+
</dependency>
1324
<dependency>
1425
<groupId>junit</groupId>
1526
<artifactId>junit</artifactId>

0 commit comments

Comments
 (0)