alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  
" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); if(locationInfo) { LocationInfo locInfo = event.getLocationInformation(); sbuf.append("" + Layout.LINE_SEP); } sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); if (event.getNDC() != null) { sbuf.append("" + Layout.LINE_SEP); } String[] s = event.getThrowableStrRep(); if(s != null) { sbuf.append("" + Layout.LINE_SEP); } return sbuf.toString(); } void appendThrowableAsHTML(String[] s, StringBuffer 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. */ public String getHeader() { StringBuffer sbuf = new StringBuffer(); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + title + "" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("
" + Layout.LINE_SEP); sbuf.append("Log session start time " + new java.util.Date() + "
" + Layout.LINE_SEP); sbuf.append("
" + Layout.LINE_SEP); sbuf.append("

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.  */

package org.apache.log4j;

import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.helpers.Transform;

/**
   This layout outputs events in a HTML table.

   @author Ceki Gülcü
 */
public class HTMLLayout extends Layout {

  protected final int BUF_SIZE = 256;
  protected final int MAX_CAPACITY = 1024;

  static String TRACE_PREFIX = "
    "; // output buffer appended to when format() is invoked private StringBuffer sbuf = new StringBuffer(BUF_SIZE); /** A string constant used in naming the option for setting the the location information flag. Current value of this string constant is LocationInfo.

Note that all option keys are case sensitive. @deprecated Options are now handled using the JavaBeans paradigm. This constant is not longer needed and will be removed in the near term. */ public static final String LOCATION_INFO_OPTION = "LocationInfo"; /** A string constant used in naming the option for setting the the HTML document title. Current value of this string constant is Title. */ public static final String TITLE_OPTION = "Title"; // Print no location info by default boolean locationInfo = false; String title = "Log4J Log Messages"; /** The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no location information output by this layout. If the the option is set to true, then the file name and line number of the statement at the origin of the log statement will be output.

If you are embedding this layout within an {@link org.apache.log4j.net.SMTPAppender} then make sure to set the LocationInfo option of that appender as well. */ public void setLocationInfo(boolean flag) { locationInfo = flag; } /** Returns the current value of the LocationInfo option. */ public boolean getLocationInfo() { return locationInfo; } /** The Title option takes a String value. This option sets the document title of the generated HTML document.

Defaults to 'Log4J Log Messages'. */ public void setTitle(String title) { this.title = title; } /** Returns the current value of the Title option. */ public String getTitle() { return title; } /** Returns the content type output by this layout, i.e "text/html". */ public String getContentType() { return "text/html"; } /** No options to activate. */ public void activateOptions() { } public String format(LoggingEvent event) { if(sbuf.capacity() > MAX_CAPACITY) { sbuf = new StringBuffer(BUF_SIZE); } else { sbuf.setLength(0); } sbuf.append(Layout.LINE_SEP + "

"); sbuf.append(event.timeStamp - event.getStartTime()); sbuf.append(""); sbuf.append(Transform.escapeTags(event.getThreadName())); sbuf.append(""); if (event.getLevel().equals(Level.DEBUG)) { sbuf.append(""); sbuf.append(event.getLevel()); sbuf.append(""); } else if(event.getLevel().isGreaterOrEqual(Level.WARN)) { sbuf.append(""); sbuf.append(event.getLevel()); sbuf.append(""); } else { sbuf.append(event.getLevel()); } sbuf.append(""); sbuf.append(Transform.escapeTags(event.getLoggerName())); sbuf.append(""); sbuf.append(Transform.escapeTags(locInfo.getFileName())); sbuf.append(':'); sbuf.append(locInfo.getLineNumber()); sbuf.append(""); sbuf.append(Transform.escapeTags(event.getRenderedMessage())); sbuf.append("
"); sbuf.append("NDC: " + Transform.escapeTags(event.getNDC())); sbuf.append("
"); appendThrowableAsHTML(s, sbuf); sbuf.append("
" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); if(locationInfo) { sbuf.append("" + Layout.LINE_SEP); } sbuf.append("" + Layout.LINE_SEP); sbuf.append("" + Layout.LINE_SEP); return sbuf.toString(); } /** Returns the appropriate HTML footers. */ public String getFooter() { StringBuffer sbuf = new StringBuffer(); sbuf.append("
TimeThreadLevelCategoryFile:LineMessage
" + Layout.LINE_SEP); sbuf.append("
" + Layout.LINE_SEP); sbuf.append(""); return sbuf.toString(); } /** The HTML layout handles the throwable contained in logging events. Hence, this method return false. */ public boolean ignoresThrowable() { return false; } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.