All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.utils.log.XmlLogConfigParser Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.firefly.utils.log;

import java.nio.charset.Charset;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.firefly.utils.ConvertUtils;
import com.firefly.utils.dom.DefaultDom;
import com.firefly.utils.dom.Dom;
import com.firefly.utils.function.Action1;
import com.firefly.utils.log.file.FileLog;

public class XmlLogConfigParser extends AbstractLogConfigParser {

	@Override
	public boolean parse(Action1 action) {
		Dom dom = new DefaultDom();
		Document doc = dom.getDocument(DEFAULT_XML_CONFIG_FILE_NAME);
		if (doc == null) {
			return false;
		}
		Element root = dom.getRoot(doc);
		List loggerList = dom.elements(root, "logger");
		if (loggerList == null || loggerList.isEmpty()) {
			return false;
		} else {
			for (Element e : loggerList) {
				String name = dom.getTextValueByTagName(e, "name", DEFAULT_LOG_NAME);
				String level = dom.getTextValueByTagName(e, "level", DEFAULT_LOG_LEVEL);
				String path = dom.getTextValueByTagName(e, "path", DEFAULT_LOG_DIRECTORY.getAbsolutePath());
				boolean consoleEnabled = ConvertUtils.convert(dom.getTextValueByTagName(e, "enable-console"),
						DEFAULT_CONSOLE_ENABLED);
				int maxFileSize = ConvertUtils.convert(dom.getTextValueByTagName(e, "max-file-size"),
						DEFAULT_MAX_FILE_SIZE);
				if (maxFileSize < 1024 * 1024 * 10) {
					System.err.println("the max log file less than 10MB, please set a larger file size");
				}
				String charset = dom.getTextValueByTagName(e, "charset", DEFAULT_CHARSET.name());
				action.call(createLog(name, level, path, consoleEnabled, maxFileSize, Charset.forName(charset)));
			}
		}
		return true;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy