log4j.properties文件
log4j.rootLogger=info,stdout,logfile #stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%p]-[%C %M %L]-%m%n #logfile log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=/usr/local/logs/xx.log log4j.appender.logfile.MaxFileSize=128MB log4j.appender.logfile.MaxBackupIndex=100 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%p]-[%C %M %L]-%m%n
=======================================
Spring和log4j结合
<!-- 属性注入器,用于读取项目配置文件中的属性 --> <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean" id="configProperties"> <property name="locations"> <list> <value>classpath*:log4j.properties</value> </list> </property> </bean>
三种方式:
第一种(Log):
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; private static Log log = LogFactory.getLog(XXX.class); log.info("**");
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
第二种(Log4J):
import org.apache.log4j.Logger; private static Logger log = Logger.getLogger(XXX.class); log.info("**");
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
第三种(slf4j):
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger log = LoggerFactory.getLogger(XXX.class); log.info("**"); <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.0</version> </dependency>