Log 4 jのステップアップ使用-カスタムクラスと+カスタム出力ログタイプ+出力ファイル
5062 ワード
Javaクラスのカスタマイズ
使用
FileLog.countTimeLog(「出力ログ」);
Log 4 j構成
package com.sitech.nbc.util;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import org.apache.log4j.Logger;
/**
*
* <p>Title: </p>
* <p>Description: </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();
}
/**
* log
*/
static void loadLogger() {
debugLogger = Logger.getLogger("");
infoLogger = Logger.getLogger("info");
errorLogger = Logger.getLogger("error");
countTimeLogger = Logger.getLogger("countTimeLogger");
}
//
public static void countTimeLog(Object msg) {
countTimeLogger.info(msg);
}
public static void countTimeLog(Object msg,Exception e) {
countTimeLogger.info(msg+"
"+getExceptionTrace(e));
}
/**
* @param msg: error
*/
public static void errorLog(Object msg) {
errorLogger.error(msg);
}
/**
* @param e: error
*/
public static void errorLog(Exception e) {
errorLogger.error(getExceptionTrace(e));
}
/**
* @param e: error
* @param msg: error
*/
public static void errorLog(Exception e, Object msg) {
errorLogger.error(msg + "
" + getExceptionTrace(e));
}
/**
* @param msg: debug
*/
public static void debugLog(Object msg) {
debugLogger.debug(msg);
}
/**
* @param e: debug
*/
public static void debugLog(Exception e) {
debugLogger.debug(getExceptionTrace(e));
}
/**
* @param e: debug
* @param msg: debug
*/
public static void debugLog(Exception e, Object msg) {
debugLogger.debug(msg + "
" + getExceptionTrace(e));
}
/**
* @param msg: info
*/
public static void systemLog(Object msg) {
infoLogger.info(msg);
}
/**
* @param e: info
*/
public static void systemLog(Exception e) {
infoLogger.info(getExceptionTrace(e));
}
/**
* @param e: debug
* @param msg: debug
*/
public static void systemLog(Exception e, Object msg) {
infoLogger.info(msg + "
" + getExceptionTrace(e));
}
/**
* @param e:
*/
public static void exOut(Exception e) {
String print = getExceptionTrace(e);
errorLogger.error(print);
}
/**
* @param e: debug
* @param msg: debug
*/
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;
}
}
使用
FileLog.countTimeLog(「出力ログ」);
Log 4 j構成
log4j.rootLogger=INFO,C,F,E
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.Threshold=DEBUG
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%n
log4j.appender.F=org.apache.log4j.DailyRollingFileAppender
log4j.appender.F.Append=true
#log4j.appender.F.File=//scmgt//maintenance//checkdownload//gt//resinfo.log
log4j.appender.F.File=/scmgt/shell/gt/info.log
log4j.appender.F.DatePattern=yyyy-MM-dd'.txt'
log4j.appender.F.Threshold=INFO
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%n
log4j.appender.E=org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.Append=true
#log4j.appender.E.File=//scmgt//maintenance//checkdownload//gt//reserror.log
log4j.appender.E.File=/scmgt/shell/gt/error.log
log4j.appender.E.DatePattern=yyyy-MM-dd'.txt'
log4j.appender.E.Threshold=ERROR
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%p] - %m%n
#countTimeLogger
log4j.logger.countTimeLogger=INFO,countTimeLogger
log4j.appender.countTimeLogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.countTimeLogger.File=/scmgt/shell/gt/countTimeLogger.log
log4j.appender.countTimeLogger.Append=true
log4j.appender.countTimeLogger.Threshold=INFO
#log4j.appender.debugout.Threshold=TRACE
log4j.appender.countTimeLogger.DatePattern='.'yyyy-MM-dd
log4j.appender.countTimeLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.countTimeLogger.layout.ConversionPattern=%5p:%d - %m%n