Custom Java class
Package COM. sitech. NBC. util; import Java. io. bytearrayoutputstream; import Java. io. printwriter; import Org. apache. log4j. logger;/***** <p> title: interface for recording system debugging information and error information </P> * <p> description: interface for recording system debugging information and error messages </P> * @ version 1.0 */public class filelog {Private Static logger debuglogger = NULL; Private Static logger errorlogger = NULL; private Static logger infologger = NULL; Private Static logger counttimelogger = NULL; static {loadlogger ();} public filelog () {super ();} /*** load the log used by the system */static void loadlogger () {debuglogger = logger. getlogger (""); infologger = logger. getlogger ("info"); errorlogger = logger. getlogger ("error"); counttimelogger = logger. getlogger ("counttimelogger");} // custom output type
Public static void counttimelog (Object MSG) {counttimelogger.info (MSG);} public static void counttimelog (Object MSG, exception E) {counttimelogger.info (MSG + "\ n" + getexceptiontrace (e);}/*** @ Param MSG: Error-level error message */public static void errorlog (Object MSG) {errorlogger. error (MSG);}/*** @ Param E: Error-level exception information */public static void errorlog (exception e) {errorlogger. error (getexceptiontrace (e);}/*** @ Param E: Error-level exception information * @ Param MSG: error-level error message */public static void errorlog (exception E, object MSG) {errorlogger. error (MSG + "\ n" + getexceptiontrace (e);}/*** @ Param MSG: Debug-level error message */public static void debuglog (Object MSG) {debuglogger. debug (MSG);}/*** @ Param E: Debug-level exception information */public static void debuglog (exception e) {debuglogger. debug (getexceptiontrace (e);}/*** @ Param E: Debug-level exception information * @ Param MSG: debug-level error message */public static void debuglog (exception E, object MSG) {debuglogger. debug (MSG + "\ n" + getexceptiontrace (e);}/*** @ Param MSG: Info-level error message */public static void systemlog (Object MSG) {infologger.info (MSG);}/*** @ Param E: Info-level exception information */public static void systemlog (exception e) {infologger.info (getexceptiontrace (e ));} /*** @ Param E: Debug-level exception information * @ Param MSG: Debug-level error information */public static void systemlog (exception E, object MSG) {infologger.info (MSG + "\ n" + getexceptiontrace (e);}/*** @ Param E: exception information output */public static void exout (exception E) {string print = getexceptiontrace (E); errorlogger. error (print);}/*** @ Param E: Debug-level exception information * @ Param MSG: Debug-level error information */Private Static string getexceptiontrace (exception E) {string print = NULL; bytearrayoutputstream bout = new bytearrayoutputstream (); printwriter WRT = new printwriter (bout); E. printstacktrace (WRT); WRT. close (); print = bout. tostring (); Return print ;}}
Use filelog. counttimelog ("output log"); log4j Configuration
log4j.rootLogger=INFO,C,F,Elog4j.appender.C=org.apache.log4j.ConsoleAppender log4j.appender.C.Target=System.outlog4j.appender.C.Threshold=DEBUGlog4j.appender.C.layout=org.apache.log4j.PatternLayoutlog4j.appender.C.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%nlog4j.appender.F=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.F.Append=true #log4j.appender.F.File=//scmgt//maintenance//checkdownload//gt//resinfo.loglog4j.appender.F.File=/scmgt/shell/gt/info.loglog4j.appender.F.DatePattern=yyyy-MM-dd'.txt'log4j.appender.F.Threshold=INFOlog4j.appender.F.layout=org.apache.log4j.PatternLayoutlog4j.appender.F.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%nlog4j.appender.E=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.E.Append=true #log4j.appender.E.File=//scmgt//maintenance//checkdownload//gt//reserror.loglog4j.appender.E.File=/scmgt/shell/gt/error.loglog4j.appender.E.DatePattern=yyyy-MM-dd'.txt'log4j.appender.E.Threshold=ERRORlog4j.appender.E.layout=org.apache.log4j.PatternLayoutlog4j.appender.E.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%n#countTimeLoggerlog4j.logger.countTimeLogger=INFO,countTimeLoggerlog4j.appender.countTimeLogger=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.countTimeLogger.File=/scmgt/shell/gt/countTimeLogger.loglog4j.appender.countTimeLogger.Append=truelog4j.appender.countTimeLogger.Threshold=INFO#log4j.appender.debugout.Threshold=TRACElog4j.appender.countTimeLogger.DatePattern='.'yyyy-MM-ddlog4j.appender.countTimeLogger.layout=org.apache.log4j.PatternLayoutlog4j.appender.countTimeLogger.layout.ConversionPattern=%5p:%d - %m%n