提交 460a1bb3 编写于 作者: F Frankie Wu

log view enhancement

上级 0b308eae
......@@ -30,7 +30,6 @@ public class ComponentsConfigurator extends AbstractResourceConfigurator {
all.add(C(MessageConsumer.class, "realtime", RealtimeConsumer.class) //
.req(AnalyzerFactory.class).config(E("consumerId").value("realtime") //
, E("domain").value("Review") //
, E("extraTime").value("300000")//
, E("analyzerNames").value("failure,transaction")));
......
......@@ -15,7 +15,6 @@
<implementation>com.dianping.cat.consumer.RealtimeConsumer</implementation>
<configuration>
<consumerId>realtime</consumerId>
<domain>Review</domain>
<extraTime>300000</extraTime>
<analyzerNames>failure,transaction</analyzerNames>
</configuration>
......
......@@ -4,6 +4,10 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
......@@ -13,24 +17,30 @@ import com.dianping.cat.message.spi.MessagePathBuilder;
import com.dianping.cat.message.spi.MessageTree;
import com.site.lookup.annotation.Inject;
public class DumpToHtmlConsumer implements MessageConsumer {
public class DumpToHtmlConsumer implements MessageConsumer, Initializable, LogEnabled {
@Inject
private MessageCodec m_codec;
@Inject
private MessagePathBuilder m_builder;
private Logger m_logger;
@Override
public void consume(MessageTree tree) {
File baseDir = m_builder.getLogViewBaseDir();
File file = new File(baseDir, m_builder.getLogViewPath(tree));
FileOutputStream fos = null;
file.getParentFile().mkdirs();
try {
ChannelBuffer buf = ChannelBuffers.dynamicBuffer(8192);
m_codec.encode(tree, buf);
fos = new FileOutputStream(file);
buf.readInt(); // get rid of count
buf.getBytes(buf.readerIndex(), fos, buf.readableBytes());
} catch (Exception e) {
throw new RuntimeException(String.format("Error when dumping to HTML file(%s)!", file), e);
......@@ -45,6 +55,11 @@ public class DumpToHtmlConsumer implements MessageConsumer {
}
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
@Override
public String getConsumerId() {
return "dump-to-file";
......@@ -55,4 +70,12 @@ public class DumpToHtmlConsumer implements MessageConsumer {
// no limitation
return null;
}
@Override
public void initialize() throws InitializationException {
File baseDir = m_builder.getLogViewBaseDir();
baseDir.mkdirs();
m_logger.info(String.format("Message will be dumpped to %s in HTML.", baseDir));
}
}
......@@ -20,7 +20,7 @@ import com.site.lookup.annotation.Inject;
public class DefaultMessageHandler extends ContainerHolder implements MessageHandler, Initializable, Runnable {
@Inject
private MessageManager m_manager;
@Inject
private MessageConsumerRegistry m_registry;
......@@ -77,4 +77,8 @@ public class DefaultMessageHandler extends ContainerHolder implements MessageHan
public void setRegistry(MessageConsumerRegistry registry) {
m_registry = registry;
}
public void shutdown() {
m_receiver.shutdown();
}
}
......@@ -36,7 +36,7 @@ public class DefaultMessagePathBuilder implements MessagePathBuilder, Initializa
@Override
public String getLogViewPath(MessageTree tree) {
MessageFormat format = new MessageFormat("{0,date,yyyyMMdd}/{1}/{2}");
MessageFormat format = new MessageFormat("{0,date,yyyyMMdd}/{0,date,HH}/{1}/{2}.html");
Date date = new Date(tree.getMessage().getTimestamp());
String path = format.format(new Object[] { date, tree.getDomain(), tree.getMessageId() });
......
......@@ -17,7 +17,7 @@ public class ComponentsConfigurator extends AbstractWebComponentsConfigurator {
List<Component> all = new ArrayList<Component>();
all.add(C(MessageConsumerRegistry.class, DefaultMessageConsumerRegistry.class) //
.req(MessageConsumer.class, new String[] { "realtime" }, "m_consumers"));
.req(MessageConsumer.class, new String[] { "realtime", "dump-to-html" }, "m_consumers"));
defineModuleRegistry(all, ReportModule.class, ReportModule.class);
......
......@@ -5,59 +5,68 @@ import com.site.web.mvc.annotation.ModuleMeta;
public enum ReportPage implements Page {
HOME("home", "Home", true),
HOME("home", "home", "Home", true),
TRANSACTION("transaction", "Transaction", true),
TRANSACTION("transaction", "t", "Transaction", true),
FAILURE("failure", "Failure", true),
FAILURE("failure", "f", "Failure", true),
LOGVIEW("logview", "Logview", true);
LOGVIEW("logview", "m", "Logview", true);
private String m_name;
private String m_name;
private String m_description;
private String m_path;
private boolean m_realPage;
private String m_description;
private ReportPage(String name, String description, boolean realPage) {
m_name = name;
m_description = description;
m_realPage = realPage;
}
private boolean m_realPage;
public static ReportPage getByName(String name, ReportPage defaultPage) {
for (ReportPage action : ReportPage.values()) {
if (action.getName().equals(name)) {
return action;
}
}
private ReportPage(String name, String path, String description, boolean realPage) {
m_name = name;
m_path = path;
m_description = description;
m_realPage = realPage;
}
return defaultPage;
}
public static ReportPage getByName(String name, ReportPage defaultPage) {
for (ReportPage action : ReportPage.values()) {
if (action.getName().equals(name)) {
return action;
}
}
public String getName() {
return m_name;
}
return defaultPage;
}
public String getDescription() {
return m_description;
}
public String getDescription() {
return m_description;
}
public String getModuleName() {
ModuleMeta meta = ReportModule.class.getAnnotation(ModuleMeta.class);
public String getModuleName() {
ModuleMeta meta = ReportModule.class.getAnnotation(ModuleMeta.class);
if (meta != null) {
return meta.name();
} else {
return null;
}
}
if (meta != null) {
return meta.name();
} else {
return null;
}
}
public boolean isRealPage() {
return m_realPage;
}
@Override
public String getName() {
return m_name;
}
public ReportPage[] getValues() {
return ReportPage.values();
}
@Override
public String getPath() {
return m_path;
}
public boolean isRealPage() {
return m_realPage;
}
public ReportPage[] getValues() {
return ReportPage.values();
}
}
package com.dianping.cat.report.page.logview;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.dianping.cat.message.spi.MessagePathBuilder;
import com.dianping.cat.report.ReportPage;
import com.ebay.webres.helper.Files;
import com.ebay.webres.helper.Joiners;
import com.site.lookup.annotation.Inject;
import com.site.web.mvc.PageHandler;
import com.site.web.mvc.annotation.InboundActionMeta;
......@@ -16,6 +21,9 @@ public class Handler implements PageHandler<Context> {
@Inject
private JspViewer m_jspViewer;
@Inject
private MessagePathBuilder m_pathBuilder;
@Override
@PayloadMeta(Payload.class)
@InboundActionMeta(name = "m")
......@@ -30,12 +38,34 @@ public class Handler implements PageHandler<Context> {
model.setAction(Action.VIEW);
model.setPage(ReportPage.LOGVIEW);
Payload payload = ctx.getPayload();
System.out.println(payload.getIdentifier());
System.out.println(Arrays.asList(payload.getPath()));
String[] path = payload.getPath();
if (path != null && path.length > 0) {
File baseDir = m_pathBuilder.getLogViewBaseDir();
String relativePath = Joiners.by('/').join(path);
File file = new File(baseDir, relativePath);
if (file.exists()) {
byte[] content = Files.forIO().readFrom(file);
showHtml(ctx, content);
return;
}
}
m_jspViewer.view(ctx, model);
}
private void showHtml(Context ctx, byte[] content) throws IOException {
HttpServletResponse response = ctx.getHttpServletResponse();
ServletOutputStream out = response.getOutputStream();
response.setContentLength(content.length);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
out.write(content);
}
}
......@@ -36,9 +36,15 @@ public class CatServlet extends AbstractContainerServlet {
manager.initializeServer(config);
DefaultMessageHandler handler = (DefaultMessageHandler) lookup(MessageHandler.class);
final DefaultMessageHandler handler = (DefaultMessageHandler) lookup(MessageHandler.class);
new Thread(handler).start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
handler.shutdown();
}
});
} catch (Exception e) {
m_exception = e;
throw new RuntimeException("Error when initializing CatServlet, "
......
......@@ -8,6 +8,7 @@
<role>com.dianping.cat.message.spi.MessageConsumer</role>
<role-hints>
<role-hint>realtime</role-hint>
<role-hint>dump-to-html</role-hint>
</role-hints>
<field-name>m_consumers</field-name>
</requirement>
......@@ -61,6 +62,9 @@
<role>com.dianping.cat.consumer.RealtimeConsumer</role>
<implementation>com.dianping.cat.consumer.RealtimeConsumer</implementation>
<requirements>
<requirement>
<role>org.codehaus.plexus.logging.Logger</role>
</requirement>
<requirement>
<role>java.lang.String</role>
</requirement>
......@@ -106,6 +110,9 @@
<requirement>
<role>com.dianping.cat.report.page.logview.JspViewer</role>
</requirement>
<requirement>
<role>com.dianping.cat.message.spi.MessagePathBuilder</role>
</requirement>
</requirements>
</component>
<component>
......
......@@ -2,9 +2,9 @@
<wizard package="com.dianping.cat" webres="true">
<module name="report" path="r">
<page name="home" description="Home" default="true" />
<page name="transaction" description="Transaction" />
<page name="failure" description="Failure" />
<page name="logview" description="Logview" />
<page name="transaction" path="t" description="Transaction" />
<page name="failure" path="f" description="Failure" />
<page name="logview" path="m" description="Logview" />
</module>
</wizard>
......@@ -18,7 +18,7 @@
<ul class="tabs">
<c:forEach var="page" items="${navBar.visiblePages}">
<c:if test="${page.realPage}">
<li ${model.page.name == page.name ? 'class="selected"' : ''}><a href="${model.webapp}/${page.moduleName}/${page.name}">${page.description}</a></li>
<li ${model.page.name == page.name ? 'class="selected"' : ''}><a href="${model.webapp}/${page.moduleName}/${page.path}">${page.description}</a></li>
</c:if>
</c:forEach>
</ul>
......
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="a" uri="/WEB-INF/app.tld"%>
<jsp:useBean id="ctx" type="com.dianping.cat.report.page.logview.Context" scope="request"/>
<jsp:useBean id="payload" type="com.dianping.cat.report.page.logview.Payload" scope="request"/>
<jsp:useBean id="model" type="com.dianping.cat.report.page.logview.Model" scope="request"/>
View of logview page under report
\ No newline at end of file
<a:body>
Sorry, this message had already been archived.
</a:body>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册