一个完整的xml配置文件(propertities文件功能不全,不能使用过滤器,并且个人认为xml格式比较清晰,易读)。

附带测试程序。

Java代码

importjava.net.URL;

importorg.apache.log4j.xml.DOMConfigurator;

/**

* 单实例 读取log4j配置文件,初始化log4j

*

*/

publicclassLog4jXMLConfig

{

privatestaticLog4jXMLConfig instance;

publicstaticsynchronizedLog4jXMLConfig initia()

{

if(instance ==null)

{

returnnewLog4jXMLConfig();

}

returninstance;

}

privateLog4jXMLConfig()

{

finalURL url = Log4jXMLConfig.class.getResource("log4j.xml");

DOMConfigurator.configure(url);

}

}

import java.net.URL;import org.apache.log4j.xml.DOMConfigurator;/** * 单实例 读取log4j配置文件,初始化log4j * */public class Log4jXMLConfig{ private static Log4jXMLConfig instance; public static synchronized Log4jXMLConfig initia() { if (instance == null) { return new Log4jXMLConfig(); } return instance; } private Log4jXMLConfig() { final URL url = Log4jXMLConfig.class.getResource("log4j.xml"); DOMConfigurator.configure(url); }}

Xml代码

log4j:configuration SYSTEM "log4j.dtd">

value="%d - %c -%-4r [%t] %-5p %x - %m%n"/>

value="%d - %c -%-4r [%t] %-5p %x - %m%n"/>

value="INSERT INTO hdczoa.LOG4J(stamp,thread, info_level,class,message) VALUES ('%d', '%t', '%p', '%c', %m)"/>

class="org.apache.log4j.net.SMTPAppender">

value="%-d{yyyy-MM-dd HH:mm:ss.SSS} [%p]-[%c] %m%n"/>

另外一个配置文件:

Xml代码

log4j:configuration SYSTEM "log4j.dtd">

class="org.apache.log4j.RollingFileAppender">

value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %m%n"/>

Web配置log4j,需求增加以下内容到WEB-INF/web.xml

webAppRootKey

smilecargo.root

log4jConfigLocation

classpath:log4j.xml

log4jRefreshInterval

60000

org.springframework.web.util.Log4jConfigListener

${smilecargo.root}是web工程相对路径

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐