1 17 18 package com.finalist.util.log; 19 20 import org.xml.sax.InputSource ; 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.NamedNodeMap ; 25 import org.w3c.dom.Node ; 26 27 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.DocumentBuilder ; 30 31 import java.net.URL ; 32 import java.util.HashMap ; 33 34 import com.finalist.util.PropertyReader; 35 36 41 public class XMLLogPropertyReader { 42 43 44 public static final String APPENDER = "appender"; 45 46 47 public static final String NAME = "name"; 48 49 50 public static final String LOG_FILE = "logfile"; 51 52 53 public static final String APPEND = "append"; 54 55 56 public static final String MAX_BACKUP_INDEX = "maxbackupindex"; 57 58 59 public static final String MAX_FILE_SIZE = "maxfilesize"; 60 61 62 public static final String SHOW_NUMBER_OF_LAST_PACKAGES = "shownumberoflastpackages"; 63 64 65 public static final String DATE_PATTERN = "datepattern"; 66 67 68 public static final String MESSAGE_SEPARATOR = "messageseparator"; 69 70 71 public static final String LOG_LEVEL = "loglevel"; 72 73 78 public static HashMap getLogConfigs(String location) { 79 HashMap logConfigs = new HashMap (); 80 try { 81 URL url = PropertyReader.getPropertiesURL(location); 83 InputSource xmlInp = new InputSource (url.openStream()); 84 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlInp); 85 NodeList appenders = doc.getElementsByTagName(APPENDER); 86 for (int i = 0; i < appenders.getLength(); i++) { 87 Node curNode = appenders.item(i); 88 if (curNode.getNodeName().equals(APPENDER)) { 89 NamedNodeMap nnm = curNode.getAttributes(); 90 String appenderName = nnm.getNamedItem(NAME).getNodeValue(); 91 String logFile = nnm.getNamedItem(LOG_FILE).getNodeValue(); 92 String append = nnm.getNamedItem(APPEND).getNodeValue(); 93 94 String maxBackupIndexString = nnm.getNamedItem(MAX_BACKUP_INDEX).getNodeValue(); 95 int maxBackupIndex = -1; 96 try { 97 maxBackupIndex = Integer.parseInt(maxBackupIndexString); 98 } 99 catch (Exception e) { 100 System.out.println("Incorrect number for maxBackupIndex with appender: " + appenderName + ": " + e); 101 } 102 103 String maxFileSizeString = nnm.getNamedItem(MAX_FILE_SIZE).getNodeValue(); 104 int maxFileSize = -1; 105 try { 106 maxFileSize = Integer.parseInt(maxFileSizeString); 107 } 108 catch (Exception e) { 109 System.out.println("Incorrect number for maxfilesize with appender: " + appenderName + ": " + e); 110 } 111 112 String showNumberOfLastPackagesString = nnm.getNamedItem(SHOW_NUMBER_OF_LAST_PACKAGES).getNodeValue(); 113 int showNumberOfLastPackages = -1; 114 try { 115 showNumberOfLastPackages = Integer.parseInt(showNumberOfLastPackagesString); 116 } 117 catch (Exception e) { 118 System.out.println("Incorrect number for shownumberoflastpackages with appender: " + 119 appenderName + ": " + e); 120 } 121 122 String datePattern = nnm.getNamedItem(DATE_PATTERN).getNodeValue(); 123 String messageSeparator = nnm.getNamedItem(MESSAGE_SEPARATOR).getNodeValue(); 124 String logLevel = nnm.getNamedItem(LOG_LEVEL).getNodeValue(); 125 LogConfig lg = new LogConfig(logFile, append.equals("true"), maxBackupIndex, maxFileSize, 126 showNumberOfLastPackages, datePattern, messageSeparator, logLevel); 127 System.out.println("Logging for filter [" + appenderName + "] with setting\n"); 128 System.out.println(lg); 129 logConfigs.put(appenderName, lg); 130 } 131 } 132 } 133 catch (Exception e) { 134 System.out.println("Exception: " + e); 135 } 136 return logConfigs; 137 } 138 } 139 | Popular Tags |