1.使用log4j提供的方法自定义类并引入到spring中.
<!--配置log4j自动加载日志--> <bean class="com.roy.util.Log4jConfig"> <constructor-arg name="reload" value="true"/> <constructor-arg name="interval" value="20000"/> </bean>2.代码如下:
package com.roy.util; import org.apache.log4j.PropertyConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Log4jConfig { private boolean reload = true; private int interval = 10000; private static final Logger logger = LoggerFactory.getLogger(Log4jConfig.class); /** * log4j日志自动加载 * * reload 是否开启自动加载 * interval 自动加载时间(ms) */ public Log4jConfig(boolean reload, int interval) { this.reload = reload; this.interval = interval; this.loadConfig(); } public void loadConfig() { String log4jPath = Log4jConfig.class.getClassLoader().getResource("log4j.properties").getPath(); logger.debug("log4j file path: " + log4jPath); // 间隔特定时间,检测文件是否修改,自动重新读取配置 PropertyConfigurator.configureAndWatch(log4jPath, this.interval); } }