前几篇说完了spring+maven+mybatis,今天把logback加上
1、首先加上 logback 的jar包依赖
<span style="white-space:pre"> </span><!-- logback --> <span style="white-space:pre"> </span><dependency> <span style="white-space:pre"> </span><groupId>ch.qos.logback</groupId> <span style="white-space:pre"> </span><artifactId>logback-classic</artifactId> <span style="white-space:pre"> </span><version>1.1.2</version> <span style="white-space:pre"> </span></dependency> <span style="white-space:pre"> </span><dependency> <span style="white-space:pre"> </span><groupId>ch.qos.logback</groupId> <span style="white-space:pre"> </span><artifactId>logback-core</artifactId> <span style="white-space:pre"> </span><version>1.1.2</version> <span style="white-space:pre"> </span></dependency>2、加入logback.xml的配置
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <!-- 控制台 --> <appender name="Console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!-- 日志文件 --> <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滚 daily --> <fileNamePattern>${catalina.home}/logs/alijk/dataView-%d{yyyy-MM-dd}.log </fileNamePattern> <!-- 日志最大的历史 10天 --> <maxHistory>10</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="RollingFile" /> </root> <logger name="alijk.dataView" level="DEBUG"> <appender-ref ref="Console" /> </logger> </configuration>3、使用logger进行日志的记录
public static final Logger logger = LoggerFactory .getLogger(GarudaDataViewController.class);
logger.info("本地执行的SQL:"+sql);4、系统运行之后的日志效果
2014-11-13 09:44:12.878 [http-bio-8090-exec-7] INFO alijk.dataView.controller.GarudaDataViewController - 本地执行的SQL:SELECT PROV_ID, CITY_ID, AREA_ID, PROV_NAME, CITY_NAME, AREA_NAME, ENT_INFO_ID, ENT_INFO_NAME AS ENT_NAME, DRUG_BASE_INFO_NAME AS PHYSIC_NAME