如何更改log4j中的htmllayout [英] how to change the htmllayout in log4j

查看:103
本文介绍了如何更改log4j中的htmllayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中更改log4j的htmllayout(5列 - 时间,线程,级别,类别,消息)。

i want to change the htmllayout (the 5 column - Time, Thread, Level, Category, Message) of log4j in Java.

我的log4j.properties是:

my log4j.properties is:

log4j.rootLogger=DEBUG, Console, File

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.File=org.apache.log4j.FileAppender
log4j.appender.File.File =${logfilename}

log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.Title=HTML Layout Example
log4j.appender.File.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
#[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n 

log4j.appender.File.Threshold=INFO
log4j.appender.Console.Threshold=DEBUG

我想改变时间颜色的名称并显示当前时间。
任何人都可以帮助我如何做到这一点?

i want to change the name of the "TIME" coloumn and to show there the current time. Could anyone help me how to do it?

推荐答案

我找到了一个可行的解决方案。

I found a solution, which works.

package com.mypackage;  

import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  

import org.apache.log4j.spi.LoggingEvent;  

/** 
 * This HTML Log Formatter is a simple replacement for the standard Log4J HTMLLayout formatter and 
 * replaces the default timestamp (milliseconds, relative to the start of the log) with a more readable 
 * timestamp (an example of the default format is 2008-11-21-18:35:21.472-0800). 
 * */  

public class   MyLayout  
       extends org.apache.log4j.HTMLLayout  

{  
// RegEx pattern looks for <tr> <td> nnn...nnn </td> (all whitespace ignored)  

private static final String rxTimestamp = "\\s*<\\s*tr\\s*>\\s*<\\s*td\\s*>\\s*(\\d*)\\s*<\\s*/td\\s*>";  

//* The timestamp format. The format can be overriden by including the following   
  * property in the Log4J configuration file:  
  *  
  * log4j.appender.<category>.layout.TimestampFormat  
  *  
  * using the same format string as would be specified with SimpleDateFormat.  
  *  
  */  

private String timestampFormat = "yyyy-MM-dd-HH:mm:ss.SZ"; // Default format. Example: 2008-11-21-18:35:21.472-0800  

private SimpleDateFormat sdf = new SimpleDateFormat(timestampFormat);  

public MyLayout()  
{  
super();  
}  

/** Override HTMLLayout's format() method */  

public String format(LoggingEvent event)  
{  
String record = super.format(event); // Get the log record in the default HTMLLayout format.  

Pattern pattern = Pattern.compile(rxTimestamp);  // RegEx to find the default timestamp  
Matcher matcher = pattern.matcher(record);  

if (!matcher.find()) // If default timestamp cannot be found,  
{  
return record; // Just return the unmodified log record.  
}  

StringBuffer buffer = new StringBuffer(record);   

buffer.replace(matcher.start(1), // Replace the default timestamp with one formatted as desired.  
       matcher.end(1),  
       sdf.format(new Date(event.timeStamp)));  

return buffer.toString(); // Return the log record with the desired timestamp format.  
}  

/** Setter for timestamp format. Called if log4j.appender.<category>.layout.TimestampFormat property is specfied */  

public void setTimestampFormat(String format)   
{  
    this.timestampFormat = format;  
this.sdf = new SimpleDateFormat(format); // Use the format specified by the TimestampFormat property  
}  

/** Getter for timestamp format being used. */  

public String getTimestampFormat()  
{  
return this.timestampFormat;  
}  

} 

这篇关于如何更改log4j中的htmllayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆