com.ovea.tadjin.util.logging.DefaultHTMLLayout Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2011 Ovea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ovea.tadjin.util.logging;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.helpers.Transform;
import org.apache.log4j.spi.LoggingEvent;
import java.util.Date;
/**
* This layout outputs events in a HTML table.
*
* Appenders using this layout should have their encoding
* set to UTF-8 or UTF-16, otherwise events containing
* non ASCII characters could result in corrupted
* log files.
*
* @author Ceki Gülcü
*/
public class DefaultHTMLLayout extends Layout {
static String TRACE_PREFIX = "
";
/**
* Returns the content type output by this layout, i.e "text/html".
*/
@Override
public String getContentType() {
return "text/html";
}
/**
* No options to activate.
*/
@Override
public void activateOptions() {
}
@Override
public String format(LoggingEvent event) {
StringBuilder sbuf = new StringBuilder();
sbuf.append(Layout.LINE_SEP).append("").append(Layout.LINE_SEP);
sbuf.append("");
sbuf.append(new Date(event.timeStamp));
sbuf.append(" ").append(Layout.LINE_SEP);
String escapedThread = Transform.escapeTags(event.getThreadName());
sbuf.append("");
sbuf.append(escapedThread);
sbuf.append(" ").append(Layout.LINE_SEP);
sbuf.append("");
if (event.getLevel().equals(Level.DEBUG)) {
sbuf.append("");
sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
sbuf.append("");
} else if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
sbuf.append("");
sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
sbuf.append("");
} else {
sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
}
sbuf.append(" ").append(Layout.LINE_SEP);
String escapedLogger = Transform.escapeTags(event.getLoggerName());
sbuf.append("");
sbuf.append(escapedLogger);
sbuf.append(" ").append(Layout.LINE_SEP);
sbuf.append("");
sbuf.append(Transform.escapeTags(event.getRenderedMessage()));
sbuf.append(" ").append(Layout.LINE_SEP);
sbuf.append(" ").append(Layout.LINE_SEP);
if (event.getNDC() != null) {
sbuf.append("");
sbuf.append("NDC: ").append(Transform.escapeTags(event.getNDC()));
sbuf.append(" ").append(Layout.LINE_SEP);
}
String[] s = event.getThrowableStrRep();
if (s != null) {
sbuf.append("");
appendThrowableAsHTML(s, sbuf);
sbuf.append(" ").append(Layout.LINE_SEP);
}
return sbuf.toString();
}
private void appendThrowableAsHTML(String[] s, StringBuilder sbuf) {
if (s != null) {
int len = s.length;
if (len == 0)
return;
sbuf.append(Transform.escapeTags(s[0]));
sbuf.append(Layout.LINE_SEP);
for (int i = 1; i < len; i++) {
sbuf.append(TRACE_PREFIX);
sbuf.append(Transform.escapeTags(s[i]));
sbuf.append(Layout.LINE_SEP);
}
}
}
/**
* Returns appropriate HTML headers.
*/
@Override
public String getHeader() {
StringBuilder sbuf = new StringBuilder();
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("
").append(Layout.LINE_SEP);
sbuf.append("Log session start time ").append(new Date()).append("
").append(Layout.LINE_SEP);
sbuf.append("
").append(Layout.LINE_SEP);
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("").append(Layout.LINE_SEP);
sbuf.append("Time ").append(Layout.LINE_SEP);
sbuf.append("Thread ").append(Layout.LINE_SEP);
sbuf.append("Level ").append(Layout.LINE_SEP);
sbuf.append("Category ").append(Layout.LINE_SEP);
sbuf.append("Message ").append(Layout.LINE_SEP);
sbuf.append(" ").append(Layout.LINE_SEP);
return sbuf.toString();
}
/**
* Returns the appropriate HTML footers.
*/
@Override
public String getFooter() {
StringBuilder sbuf = new StringBuilder();
sbuf.append("
").append(Layout.LINE_SEP);
sbuf.append("
").append(Layout.LINE_SEP);
sbuf.append("");
return sbuf.toString();
}
/**
* The HTML layout handles the throwable contained in logging
* events. Hence, this method return false
.
*/
@Override
public boolean ignoresThrowable() {
return false;
}
}