java项目log4j配置
1.添加maven依赖
<dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
2.代码中设置
import org.apache.log4j.Logger; public class hello { static Logger logger = Logger.getLogger(hello.class); public static void main(String args[]) { logger.info("6666666666"); logger.warn("44444444444444"); } }
3.配置log4j.properties
##########Set Log Levels############### log4j.rootLogger = INFO, console, info, error ######### Êä³öµ½¿ØÖÆ̨ #################### log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE}%5p%c{ 1 :%L-%m%n ######### Êä³öµ½Îļþ #################### log4j.appender.info = org.apache.log4j.DailyRollingFileAppender log4j.appender.info.File = D:\\logs\\info.log log4j.appender.info.Append = true log4j.appender.info.Threshold = INFO ##Êä³öINFO¼¶±ðÒÔÉϵÄÈÕÖ¾ log4j.appender.info.layout = org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r]-[%p] %m%n ######### ±£´æÒì³£ÐÅÏ¢µ½µ¥¶ÀÎļþ #################### log4j.appender.error = org.apache.log4j.DailyRollingFileAppender log4j.appender.error.File = D:\\logs\\error.log log4j.appender.error.Append = trues log4j.appender.error.Threshold = ERROR ##Ö»Êä³öERROR¼¶±ð¼°ÒÔÉϵÄÈÕÖ¾ log4j.appender.error.layout = org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r]-[%p] %m%n