mybatis java config
마이바티스 자바 설정 방법.
1 번 방법 java로 세팅
이 방법으로 했을때는 필터가 되지 않아서 이방법은 제외함.
FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile("/logs/mylog.log");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(fa);
2 번째 방법.
xml을 그냥 스트링으로 해서 처리함. 이건 똑같이 작동함. ㅋ
주의 할것은 Doctype을 넣으니 log4j.dtd.를 찾기 못해서 에러가 남
그래서 doctype은 그냥 주석처리함.
StringBuffer xml = new StringBuffer();
xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
//xml.append("<!DOCTYPE log4j:configuration SYSTEM \"log4j.dtd\">");
xml.append("<log4j:configuration xmlns:log4j=\"http://jakarta.apache.org/log4j/\">");
xml.append("<!-- Appenders -->");
xml.append(" <appender name=\"dynamicCompileAppender\" class=\"org.apache.log4j.DailyRollingFileAppender\">");
xml.append(" <param name=\"File\" value=\""+System.getProperty("pub.compile.log.path", DynamicPubConfig.getInstance().getProperty("log.path"))+"\" />");
xml.append(" <param name=\"DatePattern\" value=\"'.'yyyy-MM-dd\" />");
xml.append(" <param name=\"Append\" value=\"true\" />");
xml.append(" <layout class=\"org.apache.log4j.PatternLayout\">");
xml.append(" <param name=\"ConversionPattern\" value=\"%d %-5p %c - %m%n\" />");
xml.append(" </layout>");
xml.append(" </appender>");
xml.append(" <logger name=\"com.pub\" additivity=\"false\">");
xml.append(" <level value=\""+DynamicPubConfig.getInstance().getProperty("log.level","debug")+"\" />");
xml.append(" <appender-ref ref=\"dynamicCompileAppender\"/>");
xml.append(" </logger>");
xml.append("</log4j:configuration>");
ByteArrayInputStream bis = new ByteArrayInputStream(xml.toString().getBytes("utf-8"));
Document document= DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bis);
DOMConfigurator.configure(document.getDocumentElement());
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
package test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.xml.DOMConfigurator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import com.pub.dynamic.compile.config.DynamicPubConfig;
public class PubLogger {
static String FQCN = PubLogger.class.getName() + ".";
private static void initLog4j() {
try {
StringBuffer xml = new StringBuffer();
xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
//xml.append("<!DOCTYPE log4j:configuration SYSTEM \"log4j.dtd\">");
xml.append("<log4j:configuration xmlns:log4j=\"http://jakarta.apache.org/log4j/\">");
xml.append("<!-- Appenders -->");
xml.append(" <appender name=\"dynamicCompileAppender\" class=\"org.apache.log4j.DailyRollingFileAppender\">");
xml.append(" <param name=\"File\" value=\"c:/zzz/logs.log\" />");
xml.append(" <param name=\"DatePattern\" value=\"'.'yyyy-MM-dd\" />");
xml.append(" <param name=\"Append\" value=\"true\" />");
xml.append(" <layout class=\"org.apache.log4j.PatternLayout\">");
xml.append(" <param name=\"ConversionPattern\" value=\"%d %-5p %c - %m%n\" />");
xml.append(" </layout>");
xml.append(" </appender>");
xml.append(" <logger name=\"com.pub\" additivity=\"false\">");
xml.append(" <level value=\"debug\" />");
xml.append(" <appender-ref ref=\"dynamicCompileAppender\"/>");
xml.append(" </logger>");
xml.append("</log4j:configuration>");
ByteArrayInputStream bis = new ByteArrayInputStream(xml.toString().getBytes("utf-8"));
Document document= DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bis);
DOMConfigurator.configure(document.getDocumentElement());
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile("/logs/mylog.log");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(fa);
}
private static Map<Class, PubLogger> loggers= new HashMap<Class, PubLogger>();
private Logger logger;
public PubLogger(Class clazz){
this.logger = Logger.getLogger(clazz);
}
public static PubLogger getLogger(Class c) {
initLog4j();
synchronized(Object.class){
if(loggers.containsKey(c)){
return loggers.get(c);
}else{
PubLogger newLogger = new PubLogger(c);
loggers.put(c, newLogger);
return newLogger;
}
}
//return Logger.getLogger(c.getName(), loggerFactory);
}
public void fatal(String message) {
logger.log(FQCN, Level.FATAL, message, null);
}
public void fatal(String message, Throwable t) {
logger.log(FQCN, Level.FATAL, message, t);
}
public void error(String message) {
logger.log(FQCN, Level.ERROR, message, null);
}
public void error(String message, Throwable t) {
logger.log(FQCN, Level.ERROR, message, t);
}
public void warn(String message) {
logger.log(FQCN, Level.WARN, message, null);
}
public void warn(String message, Throwable t) {
logger.log(FQCN, Level.WARN, message, t);
}
public void info(String message) {
logger.log(FQCN, Level.INFO, message, null);
}
public void debug(String message) {
logger.log(FQCN, Level.DEBUG, message, null);
}
public void trace(String message) {
logger.log(FQCN, Level.TRACE, message, null);
}
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
public boolean isTraceEnabled() {
return logger.isTraceEnabled();
}
private String sfLoggerInfo(String format,Object...args) {
StringBuffer sb = new StringBuffer();
sb.append(format);
for (int i = 0; i < args.length; i++) {
sb.append(args[i]);
}
return sb.toString();
}
public void debug(String format, Object...args) {
logger.log(FQCN, Level.DEBUG, sfLoggerInfo(format,args), null);
//LogSF.debug(logger, format, args);
}
public void trace(String format, Object...args) {
logger.log(FQCN, Level.TRACE, sfLoggerInfo(format,args), null);
//LogSF.trace(logger, format, args);
}
public void warn(String format, Object...args) {
logger.log(FQCN, Level.WARN, sfLoggerInfo(format,args), null);
//LogSF.warn(logger, format, args);
}
public void error(String format, Object...args) {
logger.log(FQCN, Level.ERROR, sfLoggerInfo(format,args), null);
//LogSF.warn(logger, format, args);
}
public void info(String format, Object...args) {
logger.log(FQCN, Level.INFO, sfLoggerInfo(format,args), null);
//LogSF.warn(logger, format, args);
}
}