通用Log4J配置文件

来源:互联网 发布:java官网教程 编辑:程序博客网 时间:2024/05/25 23:56
#### Use two appenders, one to log to console, another to log to a filelog4j.rootCategory=DEBUG, stdout, debugout, R, R2#log4j.rootCategory=DEBUG, debugout, R, R2log4j.logger.org=INFO#### First appender writes to consolelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%-5.5p:%d [ %c - %L ] - %m%n#### Second appender writes to a filelog4j.appender.R=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R.File=./LOG/INFO.loglog4j.appender.R.Append=truelog4j.appender.R.Threshold=INFOlog4j.appender.R.DatePattern='.'yyyy-MM-ddlog4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%5p:%d - %m%n#### Third appender writes to a filelog4j.appender.R2=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R2.File=./LOG/ERROR.loglog4j.appender.R2.Append=truelog4j.appender.R2.Threshold=WARNlog4j.appender.R2.DatePattern='.'yyyy-MM-ddlog4j.appender.R2.layout=org.apache.log4j.PatternLayoutlog4j.appender.R2.layout.ConversionPattern=%5p:%d - %m%nlog4j.appender.debugout=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.debugout.File=./LOG/DEBUG.loglog4j.appender.debugout.Append=truelog4j.appender.debugout.Threshold=DEBUGlog4j.appender.debugout.DatePattern='.'yyyy-MM-ddlog4j.appender.debugout.layout=org.apache.log4j.PatternLayoutlog4j.appender.debugout.layout.ConversionPattern=%5p:%d - %m%n

解析文件

package examples; import java.io.File;import java.io.FileInputStream;import java.net.URI;import java.net.URISyntaxException;import java.util.Properties;import org.apache.log4j.Logger;/** * 动态读取配置文件类 *  * @author liubing */public class ConfigReadUtil {private static Logger log = Logger.getLogger(ConfigReadUtil.class);/** * 属性文件全名 */private static final String PFILE = "config.properties";/** * 配置文件路径 *///private URI uri = null;/** * 属性文件所对应的属性对象变量 */private long m_lastModifiedTime = 0;/** * 对应于属性文件的文件对象变量 */private File m_file = null;/** * 属性文件所对应的属性对象变量 */private Properties m_props = null;/** * 唯一实例 */private static ConfigReadUtil m_instance = new ConfigReadUtil();/** * 私有构造函数 *  * @throws URISyntaxException */private ConfigReadUtil() {try {m_lastModifiedTime = getFile().lastModified();if (m_lastModifiedTime == 0) {log.info(PFILE + "file does not exist!");}m_props = new Properties();m_props.load(new FileInputStream(getFile()));} catch (URISyntaxException e) {System.err.println("文件路径不正确");e.printStackTrace();} catch (Exception e) {System.err.println("文件读取异常");e.printStackTrace();}}/** * 查找ClassPath路径获取文件 *  * @return File对象 * @throws URISyntaxException */private File getFile() throws URISyntaxException {URI fileUri = this.getClass().getClassLoader().getResource(PFILE).toURI();m_file = new File(fileUri);return m_file;}/** * 静态工厂方法 *  * @return 返回ConfigurationRead的单一实例 */public synchronized static ConfigReadUtil getInstance() {return m_instance;}/** * 读取一特定的属性项 */public String getConfigItem(String name, String defaultVal) {long newTime = m_file.lastModified();// 检查属性文件是否被修改if (newTime == 0) {// 属性文件不存在if (m_lastModifiedTime == 0) {System.err.println(PFILE + " file does not exist!");} else {System.err.println(PFILE + " file was deleted!!");}return defaultVal;} else if (newTime > m_lastModifiedTime) {m_props.clear();try {m_props.load(new FileInputStream(getFile()));} catch (Exception e) {System.err.println("文件重新读取异常");e.printStackTrace();}}m_lastModifiedTime = newTime;String val = m_props.getProperty(name);if (val == null) {return defaultVal;} else {return val;}}/** * 读取一特定的属性项 *  * @param name *            属性项的项名 * @return 属性项的值(如此项存在), 空(如此项不存在) */public String getConfigItem(String name) {return getConfigItem(name, "");}}

-----------------------------------程序员 闫帆原创---------------------------------------

转载请注明原创人信息  程序员 闫帆yanfanvip



原创粉丝点击