package com.vci.common.log; import java.io.File; import java.io.FileInputStream; import java.net.URL; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.Configurator; //import org.apache.logging.log4j.PropertyConfigurator; /** *
Title:
*Description: use log4j to note some meaningful info. * the info has four kinds: INFO, DEBUG, WARN and ERROR. * it display log infos in console and save them into a * log file. Programeers can lookup useful info from them. * And programeers can set the level in * properties/ServerWithLog4j.properties to decide which info * they want to save.
*/ public class ServerWithLog4j { private static String log4j_properties_dev = "properties/ServerLog4j2.xml"; private static String log4j_properties = "/" + log4j_properties_dev; // private static String log4j_properties_file_path_for_developer = "properties/ServerWithLog4j.properties"; // private static String log4j_properties_file_path = "/" + log4j_properties_file_path_for_developer; private static URL log4j_properties_url = null; public static Logger logger = null;//LogManager.getLogger("ServerLog"); static { try { File file = new File(log4j_properties_dev); System.out.println("=====log4j_properties_dev = " + file.getAbsolutePath()); String loadFrom = ""; if(file.exists()){ loadFrom = file.getAbsolutePath(); } else { log4j_properties_url = ServerWithLog4j.class.getResource(log4j_properties); loadFrom = log4j_properties_url.toString(); } ConfigurationSource source = new ConfigurationSource(new FileInputStream(loadFrom)); Configurator.initialize(null, source); logger = LogManager.getLogger("ServerLog"); logger.debug("log4j init completed. use properties file is " + loadFrom); } catch (Exception e) { e.printStackTrace(); } } }