Maven下log4j的使用
1.添加slf4j log4j相关的依赖包
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency>
2.在src下添加log4j属性配置文件 log4j.properties
1 log4j.rootLogger=INFO, RegularAirDetail_24h_log,RegularAirDetail_30d_log,RegularAirDetail_AQIRank_log,RegularAirDetail30dsSlice_log,RegularAirQuality_log,RegularLifeIndex_log,RegularPolForTrendWrf_log,RegularSpider_log,RegularMainpageDTO_log,RegularRealtimepageDTO_log,RegularAirdetailpageDTO_log 2 3 4 #RegularAirDetail_24h_log 5 log4j.logger.RegularAirDetail_24h_log=INFO, RegularAirDetail_24h_log 6 log4j.appender.RegularAirDetail_24h_log=org.apache.log4j.DailyRollingFileAppender 7 log4j.appender.RegularAirDetail_24h_log.Append=true 8 log4j.appender.RegularAirDetail_24h_log.DatePattern='.'yyyy-MM-dd 9 log4j.appender.RegularAirDetail_24h_log.File=F\:\\logs\\RegularAirDetail_24h_log\\RegularAirDetail_24h_log.log 10 log4j.appender.RegularAirDetail_24h_log.encoding=UTF-8 11 log4j.appender.RegularAirDetail_24h_log.Threshold=INFO 12 log4j.appender.RegularAirDetail_24h_log.layout=org.apache.log4j.PatternLayout 13 log4j.appender.RegularAirDetail_24h_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 14 log4j.additivity.RegularAirDetail_24h_log=false 15 16 #RegularAirDetail_30d_log 17 log4j.logger.RegularAirDetail_30d_log=INFO, RegularAirDetail_30d_log 18 log4j.appender.RegularAirDetail_30d_log=org.apache.log4j.DailyRollingFileAppender 19 log4j.appender.RegularAirDetail_30d_log.Append=true 20 log4j.appender.RegularAirDetail_30d_log.DatePattern='.'yyyy-MM-dd 21 log4j.appender.RegularAirDetail_30d_log.File=F\:\\logs\\RegularAirDetail_30d_log\\RegularAirDetail_30d_log.log 22 log4j.appender.RegularAirDetail_30d_log.encoding=UTF-8 23 log4j.appender.RegularAirDetail_30d_log.Threshold=INFO 24 log4j.appender.RegularAirDetail_30d_log.layout=org.apache.log4j.PatternLayout 25 log4j.appender.RegularAirDetail_30d_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 26 log4j.additivity.RegularAirDetail_30d_log=false 27 28 #RegularAirDetail_AQIRank_log 29 log4j.logger.RegularAirDetail_AQIRank_log=INFO, RegularAirDetail_AQIRank_log 30 log4j.appender.RegularAirDetail_AQIRank_log=org.apache.log4j.DailyRollingFileAppender 31 log4j.appender.RegularAirDetail_AQIRank_log.Append=true 32 log4j.appender.RegularAirDetail_AQIRank_log.DatePattern='.'yyyy-MM-dd 33 log4j.appender.RegularAirDetail_AQIRank_log.File=F\:\\logs\\RegularAirDetail_AQIRank_log\\RegularAirDetail_AQIRank_log.log 34 log4j.appender.RegularAirDetail_AQIRank_log.encoding=UTF-8 35 log4j.appender.RegularAirDetail_AQIRank_log.Threshold=INFO 36 log4j.appender.RegularAirDetail_AQIRank_log.layout=org.apache.log4j.PatternLayout 37 log4j.appender.RegularAirDetail_AQIRank_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 38 log4j.additivity.RegularAirDetail_AQIRank_log=false 39 40 #RegularAirDetail30dsSlice_log 41 log4j.logger.RegularAirDetail30dsSlice_log=INFO, RegularAirDetail30dsSlice_log 42 log4j.appender.RegularAirDetail30dsSlice_log=org.apache.log4j.DailyRollingFileAppender 43 log4j.appender.RegularAirDetail30dsSlice_log.Append=true 44 log4j.appender.RegularAirDetail30dsSlice_log.DatePattern='.'yyyy-MM-dd 45 log4j.appender.RegularAirDetail30dsSlice_log.File=F\:\\logs\\RegularAirDetail30dsSlice_log\\RegularAirDetail30dsSlice_log.log 46 log4j.appender.RegularAirDetail30dsSlice_log.encoding=UTF-8 47 log4j.appender.RegularAirDetail30dsSlice_log.Threshold=INFO 48 log4j.appender.RegularAirDetail30dsSlice_log.layout=org.apache.log4j.PatternLayout 49 log4j.appender.RegularAirDetail30dsSlice_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 50 log4j.additivity.RegularAirDetail30dsSlice_log=false 51 52 #RegularAirQuality_log 53 log4j.logger.RegularAirQuality_log=INFO, RegularAirQuality_log 54 log4j.appender.RegularAirQuality_log=org.apache.log4j.DailyRollingFileAppender 55 log4j.appender.RegularAirQuality_log.Append=true 56 log4j.appender.RegularAirQuality_log.DatePattern='.'yyyy-MM-dd 57 log4j.appender.RegularAirQuality_log.File=F\:\\logs\\RegularAirQuality_log\\RegularAirQuality_log.log 58 log4j.appender.RegularAirQuality_log.encoding=UTF-8 59 log4j.appender.RegularAirQuality_log.Threshold=INFO 60 log4j.appender.RegularAirQuality_log.layout=org.apache.log4j.PatternLayout 61 log4j.appender.RegularAirQuality_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 62 log4j.additivity.RegularAirQuality_log=false 63 64 #RegularLifeIndex_log 65 log4j.logger.RegularLifeIndex_log=INFO, RegularLifeIndex_log 66 log4j.appender.RegularLifeIndex_log=org.apache.log4j.DailyRollingFileAppender 67 log4j.appender.RegularLifeIndex_log.Append=true 68 log4j.appender.RegularLifeIndex_log.DatePattern='.'yyyy-MM-dd 69 log4j.appender.RegularLifeIndex_log.File=F\:\\logs\\RegularLifeIndex_log\\RegularLifeIndex_log.log 70 log4j.appender.RegularLifeIndex_log.encoding=UTF-8 71 log4j.appender.RegularLifeIndex_log.Threshold=INFO 72 log4j.appender.RegularLifeIndex_log.layout=org.apache.log4j.PatternLayout 73 log4j.appender.RegularLifeIndex_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 74 log4j.additivity.RegularLifeIndex_log=false 75 76 #RegularPolForTrendWrf_log 77 log4j.logger.RegularPolForTrendWrf_log=INFO, RegularPolForTrendWrf_log 78 log4j.appender.RegularPolForTrendWrf_log=org.apache.log4j.DailyRollingFileAppender 79 log4j.appender.RegularPolForTrendWrf_log.Append=true 80 log4j.appender.RegularPolForTrendWrf_log.DatePattern='.'yyyy-MM-dd 81 log4j.appender.RegularPolForTrendWrf_log.File=F\:\\logs\\RegularPolForTrendWrf_log\\RegularPolForTrendWrf_log.log 82 log4j.appender.RegularPolForTrendWrf_log.encoding=UTF-8 83 log4j.appender.RegularPolForTrendWrf_log.Threshold=INFO 84 log4j.appender.RegularPolForTrendWrf_log.layout=org.apache.log4j.PatternLayout 85 log4j.appender.RegularPolForTrendWrf_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 86 log4j.additivity.RegularPolForTrendWrf_log=false 87 88 #RegularSpider_log 89 log4j.logger.RegularSpider_log=INFO, RegularSpider_log 90 log4j.appender.RegularSpider_log=org.apache.log4j.DailyRollingFileAppender 91 log4j.appender.RegularSpider_log.Append=true 92 log4j.appender.RegularSpider_log.DatePattern='.'yyyy-MM-dd 93 log4j.appender.RegularSpider_log.File=F\:\\logs\\RegularSpider_log\\RegularSpider_log.log 94 log4j.appender.RegularSpider_log.encoding=UTF-8 95 log4j.appender.RegularSpider_log.Threshold=INFO 96 log4j.appender.RegularSpider_log.layout=org.apache.log4j.PatternLayout 97 log4j.appender.RegularSpider_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 98 log4j.additivity.RegularSpider_log=false 99 100 #RegularMainpageDTO_log 101 log4j.logger.RegularMainpageDTO_log=INFO, RegularMainpageDTO_log 102 log4j.appender.RegularMainpageDTO_log=org.apache.log4j.DailyRollingFileAppender 103 log4j.appender.RegularMainpageDTO_log.Append=true 104 log4j.appender.RegularMainpageDTO_log.DatePattern='.'yyyy-MM-dd 105 log4j.appender.RegularMainpageDTO_log.File=F\:\\logs\\RegularMainpageDTO_log\\RegularMainpageDTO_log.log 106 log4j.appender.RegularMainpageDTO_log.encoding=UTF-8 107 log4j.appender.RegularMainpageDTO_log.Threshold=INFO 108 log4j.appender.RegularMainpageDTO_log.layout=org.apache.log4j.PatternLayout 109 log4j.appender.RegularMainpageDTO_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 110 log4j.additivity.RegularMainpageDTO_log=false 111 112 #RegularRealtimepageDTO_log 113 log4j.logger.RegularRealtimepageDTO_log=INFO, RegularRealtimepageDTO_log 114 log4j.appender.RegularRealtimepageDTO_log=org.apache.log4j.DailyRollingFileAppender 115 log4j.appender.RegularRealtimepageDTO_log.Append=true 116 log4j.appender.RegularRealtimepageDTO_log.DatePattern='.'yyyy-MM-dd 117 log4j.appender.RegularRealtimepageDTO_log.File=F\:\\logs\\RegularRealtimepageDTO_log\\RegularRealtimepageDTO_log.log 118 log4j.appender.RegularRealtimepageDTO_log.encoding=UTF-8 119 log4j.appender.RegularRealtimepageDTO_log.Threshold=INFO 120 log4j.appender.RegularRealtimepageDTO_log.layout=org.apache.log4j.PatternLayout 121 log4j.appender.RegularRealtimepageDTO_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 122 log4j.additivity.RegularRealtimepageDTO_log=false 123 124 #RegularAirdetailpageDTO_log 125 log4j.logger.RegularAirdetailpageDTO_log=INFO, RegularAirdetailpageDTO_log 126 log4j.appender.RegularAirdetailpageDTO_log=org.apache.log4j.DailyRollingFileAppender 127 log4j.appender.RegularAirdetailpageDTO_log.Append=true 128 log4j.appender.RegularAirdetailpageDTO_log.DatePattern='.'yyyy-MM-dd 129 log4j.appender.RegularAirdetailpageDTO_log.File=F\:\\logs\\RegularAirdetailpageDTO_log\\RegularAirdetailpageDTO_log.log 130 log4j.appender.RegularAirdetailpageDTO_log.encoding=UTF-8 131 log4j.appender.RegularAirdetailpageDTO_log.Threshold=INFO 132 log4j.appender.RegularAirdetailpageDTO_log.layout=org.apache.log4j.PatternLayout 133 log4j.appender.RegularAirdetailpageDTO_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n 134 log4j.additivity.RegularAirdetailpageDTO_log=false
3.在web.xml中对log4j进行配置
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener>
4.在pom.xml中对log4j.properties在war包中可见
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>*.properties</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
5.使用
Logger chargeLogger = LoggerFactory.getLogger("RegularAirDetail_24h_log");
chargeLogger.info("hello world");