2012-09-25 7 views
6

Voglio cambiare il linguaggio html (la colonna 5 - Tempo, Discussione, Livello, Categoria, Messaggio) di log4j in Java.come cambiare l'htmllayout in log4j

miei log4j.properties è:

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 

voglio cambiare il nome della colonnina "tempo" e per indicare che l'ora corrente. Qualcuno potrebbe aiutarmi a farlo?

risposta

0

Configurare il layout in seguito la configurazione

Log4j.rootLogger=DEBUG 

log4j.appender.FileAppender =org.apache.log4j.FileAppender 
log4j.appender.FileAppender.File= C:\test.html 
log4j.appender.FileAppender.layout=org.apache.log4j.HTMLLayout 

log4j.logger.<your packge>=FileAppender   
+0

ho fatto come hai detto tu "log4j.logger.com.financial.scotiatester = File.layout" , ma non è cambiato nulla? –

+0

@Emrah Karakoc Aggiornare la risposta, è sufficiente ricontrollarla – CycDemo

+0

log4j: WARN Non è stato possibile trovare appendici per il registratore (com.financial.scotiatester.ScotiaJunoLevel1Tester). log4j: WARN Si prega di inizializzare correttamente il sistema log4j. log4j: WARN Vedi http://logging.apache.org/log4j/1.2/faq.html#noconfig per maggiori informazioni. ho cancellato il mio codice nel file delle proprietà e annotato il vostro. ma ho l'errore ora? –

4

ho trovato una soluzione che funziona.

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; 
} 

} 
1

creare un layout personalizzato che si estende org.apache.log4j.HTMLLayout di classe e sovrascrivere il metodo format.

Puoi vedere come la formattazione è la code of HTMLLayout e creare la tua versione che si adatta alle tue esigenze.

1

Il pezzo che sembrava mancare da questa soluzione è come configurare la nuova classe estesa HTMLLayout in modo che venga rilevata e utilizzata quando vengono inviate le eccezioni.

Il nostro è funzionante e avevamo solo bisogno di un solo liner aggiunto al nostro file log4j.properties.

log4j.appender.email.layout=com.<yourPackage>.utils.CustomizedHTMLLayout