log4j的使用
Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
配置
1.在maven中加入依赖包,slf4j-log4j12、log4j
1 <dependency> 2 <groupId>org.slf4j</groupId> 3 <artifactId>slf4j-log4j12</artifactId> 4 <version>${slf4j.version}</version> 5 </dependency> 6 <dependency> 7 <groupId>log4j</groupId> 8 <artifactId>log4j</artifactId> 9 <version>${log4j.version}</version> 10 </dependency> 11 <dependency> 12 13 14 <log4j.version>1.2.16</log4j.version> 15 <slf4j.version>1.6.1</slf4j.version>
2.在CLASSPATH下建立log4j.properties。内容如下(数字为行号):
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 4 5 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> 6 <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 7 <param name="Encoding" value="UTF-8" /> 8 <layout class="org.apache.log4j.PatternLayout"> 9 <param name="ConversionPattern" value="%c - %m%n" /> 10 </layout> 11 </appender> 12 13 <appender name="fileError" class="org.apache.log4j.DailyRollingFileAppender"> 14 <param name="File" value="../logs/scm.log" /> 15 <param name="Encoding" value="UTF-8" /> 16 <layout class="org.apache.log4j.PatternLayout"> 17 <param name="ConversionPattern" value="%d{yy-MM-dd HH:mm:ss} %m %n" /> 18 </layout> 19 </appender> 20 <!-- 开发环境 --> 21 <logger name="com.gta.scm" additivity="true"> 22 <level value="debug" /> 23 <appender-ref ref="stdout" /> 24 <appender-ref ref="fileError" /> 25 </logger> 26 <logger name="org.springframework" additivity="true"> 27 <level value="error" /> 28 <appender-ref ref="stdout" /> 29 <appender-ref ref="fileError" /> 30 </logger> 31 <logger name="org.mybatis" additivity="true"> 32 <level value="error" /> 33 <appender-ref ref="stdout" /> 34 <appender-ref ref="fileError" /> 35 </logger> 36 37 <logger name="java.sql.Connection" additivity="true"> 38 <level value="debug" /> 39 <appender-ref ref="stdout" /> 40 <appender-ref ref="fileError" /> 41 </logger> 42 43 <logger name="java.sql.Statement" additivity="true"> 44 <level value="debug" /> 45 <appender-ref ref="stdout" /> 46 <appender-ref ref="fileError" /> 47 </logger> 48 49 <logger name="java.sql.PreparedStatement" additivity="true"> 50 <level value="debug" /> 51 <appender-ref ref="stdout" /> 52 <appender-ref ref="fileError" /> 53 </logger> 54 55 <logger name="java.sql.ResultSet" additivity="true"> 56 <level value="error" /> 57 <appender-ref ref="stdout" /> 58 </logger> 59 <logger name="org.apache.ibatis.logging.jdbc.ConnectionLogger" additivity="true"> 60 <level value="debug" /> 61 <appender-ref ref="stdout" /> 62 </logger> 63 <!-- 生产环境 --> 64 <!-- <root> 65 <level value="error" /> 66 <appender-ref ref="fileError" /> 67 </root> --> 68 </log4j:configuration>
3.在要输出日志的类中加入相关语句:
1 public class SimpleLogger { 2 3 private Logger simpleLogger = null; 4 5 static class SingletonHolder { 6 static SimpleLogger instance = new SimpleLogger(); 7 } 8 9 public static SimpleLogger getLogger() { 10 return SingletonHolder.instance; 11 } 12 13 public static SimpleLogger getLogger(Class<?> clazz) { 14 return new SimpleLogger(clazz); 15 } 16 17 private SimpleLogger() { 18 simpleLogger = null; 19 simpleLogger = LoggerFactory.getLogger("pango logger"); 20 } 21 22 private SimpleLogger(Class<?> clazz) { 23 simpleLogger = LoggerFactory.getLogger(clazz); 24 } 25 26 public void error(String message) { 27 simpleLogger.error(message); 28 } 29 30 public void error(String message, Throwable t) { 31 simpleLogger.error(message, t); 32 } 33 34 public void error(String format, Object arg1, Object arg2) { 35 simpleLogger.error(format, arg1, arg2); 36 } 37 38 public void error(String format, Object... args) { 39 simpleLogger.error(format, args); 40 } 41 42 public void debug(String message) { 43 simpleLogger.debug(message); 44 } 45 46 public void debug(String message, Throwable t) { 47 simpleLogger.debug(message, t); 48 } 49 50 public void warn(String message) { 51 simpleLogger.warn(message); 52 } 53 54 public void warn(String message, Throwable t) { 55 simpleLogger.warn(message, t); 56 } 57 58 public void warn(String format, Object... args) { 59 simpleLogger.warn(format, args); 60 } 61 62 public void info(String message) { 63 simpleLogger.info(message); 64 } 65 66 public void info(String message, Throwable t) { 67 simpleLogger.info(message, t); 68 } 69 70 public boolean isDebugEnabled() { 71 return simpleLogger.isDebugEnabled(); 72 } 73 }
4.在项目中的使用
private static SimpleLogger logger = SimpleLogger.getLogger(SpringContextHelper.class);
private static final Logger logger = LoggerFactory.getLogger(SalesManageController.class);
logger.error(e.getMessage(), e);