Three Web Development Technical Series - Improve JBoss Log Function

xiaoxiao2021-03-06  41

I. JBoss's log

JBoss uses the log4j engine to log the log, we can configure the log by configuring log4j.xml under the conf directory. But unfortunately, jboss has no log feature of one file per day, and Tomcat is there.

After analysis, Tomcat used to expand a log recorder that expanded to log4j, and JBoss did not.

2 implementation daily log function

In view of this idea, I extended the JBoss's logger to implement the same log logger as Tomcat, which can form a log file daily.

/ **

* FileName: datedfileappender.java

* Created by: Mao Xiang

* CREATED ON: 2004-12-30 10:11:45

* Last Modified by: $ author $

* Last Modified on: $ DATE $

* Revision: $ Revision $

* /

Package commit.logging;

Import java.text.SIMPLEDATEFORMAT;

Import java.util.date;

Import org.apache.log4j.spi.loggingevent;

Import Org.jboss.logging.Appender.fileAppender;

Public class datedfileappender extends

Org.apache.log4j.dailyRollingFileAppender {

Private static string lastdate;

Private static string OriginalFile;

Public void setfile (final string filename) {

// save the Original File

Originalfile = filename;

// Add TimeStamp

Final String NewFileName = AddTimeStamp (filename);

FileAppender.Helper.makepath (NewFileName);

// set LastDate

Lastdate = getTimeStamp ();

Super.setfile (NewFileName);

}

/ **

* @see org.apache.log4j.writrappender # Subappend (org.apache.log4j.spi.loggingEvent)

* /

Protected void subappend (loggingEvent evenet) {

String current = getTimestamp ();

IF (! current.equals (lastdate)) {

// if Date Has Changed, Need to log to new file

SetFile (OriginalFile);

Super.ActivateOptions ();

}

Super.subappend (evening);

}

Protected string addttimestamp (string filename) {

String Ret = filename; String Dot = "."

INT i = filename.lastIndexof (dot);

IF (i> -1) {

String prefix = filename.substring (0, i);

String suffix = filename.substring (i);

Ret = prefix getTimeStamp () SUFFIX;

} else {

RET = RET GETTIMESTAMP ();

}

Return Ret;

}

/ **

* Get the date

* @Return YYYYMMDD EXAMPLE: 20041230

* /

Private string gettimestamp () {

SimpleDateFormat DF = New SimpleDateFormat ("YYYYMMDD");

Return DF.FORMAT (New Date ());

}

/ **

* Simple Test

* @Param Args

* /

Public static void main (String [] args) {

DatedFileAppender test = new datedfileapplender ();

System.out.println (Test.AddtimeStamp ("Server.log");

System.out.println (TEST.ADDTimeStamp ("Server");

System.out.println (Test.AddtimeStamp ("Server.Test.log");

}

}

three. Use example

Modify log4j.xml under Conf in JBoss.

->

->

four. effect

转载请注明原文地址:https://www.9cbs.com/read-74828.html

New Post(0)