package com.wakeup.module.commons.log;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class LoggerFactory {
private LoggerFactory(){}
public static Logger getLogger(Class<?> clazz){
LoggerConfigLoader l = LoggerConfigLoader.getAndLoadLoggerConfig();
return new Logger(clazz, l.getLogStatus());
}
}
class LoggerConfigLoader{
/* 설정 파일 값들 */
/* 값 */
protected static final int NO_STATUS = 0b00000000;
protected static final int INFO_STATUS = 0b00000001;
protected static final int DEBUG_STATUS = 0b00000010;
/* 이름 */
private final String NO_STATUS_NAME = "no";
private final String INFO_STATUS_NAME = "info";
private final String DEBUG_STATUS_NAME = "debug";
/* 설정파일 이름 */
private final String LOGGER_XML_CONFIG_NAME = "wk5logger.xml";
/* 싱글턴을 위함. */
private static LoggerConfigLoader instance = null;
/* 설정 상태 값. */
private int logStatus = NO_STATUS;
/* 내부 작동을 위한 변수들 */
private List<String> statusNameList =
Arrays.asList(new String[]{NO_STATUS_NAME, INFO_STATUS_NAME, DEBUG_STATUS_NAME});
private List<Integer> statusValueList =
Arrays.asList(new Integer[]{NO_STATUS, INFO_STATUS, DEBUG_STATUS});
private final Map<String, Integer> statusMap = new HashMap<String, Integer>();
/* 생성자 */
private LoggerConfigLoader(){
/* 상태값을 맵에 넣음 */
for(int i = 0; i < statusNameList.size(); i++){
statusMap.put(statusNameList.get(i), statusValueList.get(i));
}
loadConfig();
}
/* 싱글턴을 위함. */
protected static LoggerConfigLoader getAndLoadLoggerConfig(){
synchronized (LoggerConfigLoader.class) {
if(instance == null){
instance = new LoggerConfigLoader();
}
}
return instance;
}
/* xml설정파일을 로드하여 설정된 값을 logStatus에 넣음. */
private void loadConfig(){
try {
File xmlConfigFile = getXmlConfigFile(LOGGER_XML_CONFIG_NAME);
//Wk5LoggerConfigLoadFailedException
String getLogStatusString = getStatusAtConfigXmlfile(xmlConfigFile);
if(statusNameList.contains(getLogStatusString)){
logStatus = statusMap.get(getLogStatusString).intValue();
}else{
logStatus = NO_STATUS;
}
} catch (Wk5LoggerConfigLoadException e) {
System.out.println(e.getMessage());
logStatus = NO_STATUS;
}
}
private File getXmlConfigFile(String xmlFileName) throws Wk5LoggerConfigLoadException{
URL configPath = this.getClass().getClassLoader().getResource(xmlFileName);
if(configPath == null) throw new Wk5LoggerConfigLoadException();
File xmlFile = new File(configPath.getFile());
return xmlFile;
}
private String getStatusAtConfigXmlfile(File xmlConfigFile){
try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document xmlConfig = documentBuilder.parse(xmlConfigFile);
xmlConfig.getDocumentElement().normalize();
Node rootNode = xmlConfig.getFirstChild();
String LogStatusString = rootNode.getOwnerDocument()
.getElementsByTagName("status").item(0).getTextContent();
return LogStatusString;
} catch (ParserConfigurationException e) {
e.printStackTrace();
return NO_STATUS_NAME;
} catch (SAXException e) {
e.printStackTrace();
return NO_STATUS_NAME;
} catch (IOException e) {
e.printStackTrace();
return NO_STATUS_NAME;
}
}
public int getLogStatus() {
return logStatus;
}
}
class Wk5LoggerConfigLoadException extends Exception{
private static final long serialVersionUID = 1L;
public Wk5LoggerConfigLoadException(){
super("wk5logger error! - "
+ "Set the log file does not exist or the file location is incorrect. "
+ "Set file at top-level source directory.");
}
public Wk5LoggerConfigLoadException(String messege){
super(messege);
}
}
|