<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="log.level" value="debug"/>
<property name="log.maxHistory" value="30"/>
<property name="log.filePath" value="${catalina.base}/logs/webapps"/>
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS}[%thread]%-5level%logger{50}-%msg%n"/>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}pattern>
encoder>
appender>
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.filePath}/debug.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gzfileNamePattern>
<maxHistory>${log.maxHistory}maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUGlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.filePath}/info.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gzfileNamePattern>
<maxHistory>${log.maxHistory}maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.filePath}/error.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gzfileNamePattern>
<maxHistory>${log.maxHistory}maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<logger name="com" level="${log.level}" additivity="true">
<appender-ref ref="debugAppender"/>
<appender-ref ref="infoAppender"/>
<appender-ref ref="errorAppender"/>
logger>
<root level="info">
<appender-ref ref="consoleAppender"/>
root>
configuration>
package com.web.superadmin;
import com.entity.Area;
import com.service.AreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
/**
* @Created with IntelliJ IDEA
* @Description:
* @Package: com.web.superadmin
* @author: FLy-Fly-Zhang
* @Date: 2019/11/12
* @Time: 14:32
*/
@Controller
@RequestMapping("/superadmin")
public class AreaController {
//创建日志
Logger logger= LoggerFactory.getLogger(AreaController.class);
@Autowired
private AreaService areaService;
@RequestMapping(value="/listarea",method = RequestMethod.GET)
@ResponseBody
private Map<String,Object> listArea(){
logger.info("==start==");//info级别日志
long startTime=System.currentTimeMillis();
Map<String,Object> modelMap=new HashMap<String,Object>();
List<Area> list=new ArrayList<Area>();
try{
list=areaService.getAreaList();
modelMap.put("rows",list);
modelMap.put("total",list.size());
}catch (Exception e){
e.printStackTrace();
modelMap.put("success",false);
modelMap.put("errMsg",e.toString());
}
logger.error("test error!");
long endTime=System.currentTimeMillis();
//将后面的值穿入{}中
logger.debug("costTime: [{}ms]",endTime-startTime);//{}为占位符,会将后面传入的数据添加进去
logger.info("==end==");
return modelMap;
}
}
我们在访问此url路径,打印台会出现以下信息:
2020-02-06 13:28:15.193[http-nio-8080-exec-7]INFO com.web.superadmin.AreaController-==start==
2020-02-06 13:28:15.291[http-nio-8080-exec-7]INFO c.m.v2.c3p0.impl.AbstractPoolBackedDataSource-Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 2, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 10000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge9esa86xf37rhp1hdq|1860881, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge9esa86xf37rhp1hdq|1860881, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/store?useUnicode=true&characterEncoding=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 30, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 20, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
2020-02-06 13:28:15.316[http-nio-8080-exec-7]DEBUGcom.mchange.v2.cfg.MConfig-The configuration file for resource identifier '/mchange-commons.properties' could not be found. Skipping.
2020-02-06 13:28:15.317[http-nio-8080-exec-7]DEBUGcom.mchange.v2.cfg.MConfig-The configuration file for resource identifier '/mchange-log.properties' could not be found. Skipping.
2020-02-06 13:28:15.317[http-nio-8080-exec-7]DEBUGcom.mchange.v2.cfg.MConfig-The configuration file for resource identifier '/c3p0.properties' could not be found. Skipping.
2020-02-06 13:28:15.317[http-nio-8080-exec-7]DEBUGcom.mchange.v2.cfg.MConfig-The configuration file for resource identifier 'hocon:/reference,/application,/c3p0,/' could not be found. Skipping.
2020-02-06 13:28:15.317[http-nio-8080-exec-7]WARN com.mchange.v2.resourcepool.BasicResourcePool-Bad pool size config, start 3 < min 20. Using 20 as start.
2020-02-06 13:28:15.320[http-nio-8080-exec-7]DEBUGcom.mchange.v2.resourcepool.BasicResourcePool-com.mchange.v2.resourcepool.BasicResourcePool@1b1a524 config: [start -> 20; min -> 20; max -> 30; inc -> 3; num_acq_attempts -> 2; acq_attempt_delay -> 1000; check_idle_resources_delay -> 0; max_resource_age -> 0; max_idle_time -> 0; excess_max_idle_time -> 0; destroy_unreturned_resc_time -> 0; expiration_enforcement_delay -> 0; break_on_acquisition_failure -> false; debug_store_checkout_exceptions -> false; force_synchronous_checkins -> false]
2020-02-06 13:28:15.320[http-nio-8080-exec-7]DEBUGc.m.v2.c3p0.impl.C3P0PooledConnectionPoolManager-Created new pool for auth, username (masked): 'ro******'.
2020-02-06 13:28:15.320[http-nio-8080-exec-7]DEBUGcom.mchange.v2.resourcepool.BasicResourcePool-acquire test -- pool size: 0; target_pool_size: 20; desired target? 1
2020-02-06 13:28:15.320[http-nio-8080-exec-7]DEBUGcom.mchange.v2.resourcepool.BasicResourcePool-awaitAvailable(): [unknown]
2020-02-06 13:28:15.629[http-nio-8080-exec-7]DEBUGcom.dao.AreaDao.queryArea-==> Preparing: select area_id,area_name,priority,create_time,last_edit_time from tb_area order by priority desc
2020-02-06 13:28:15.727[http-nio-8080-exec-7]DEBUGcom.dao.AreaDao.queryArea-==> Parameters:
2020-02-06 13:28:15.750[http-nio-8080-exec-7]DEBUGcom.dao.AreaDao.queryArea-<== Total: 2
2020-02-06 13:28:15.759[http-nio-8080-exec-7]ERRORcom.web.superadmin.AreaController-test error!
2020-02-06 13:28:15.759[http-nio-8080-exec-7]DEBUGcom.web.superadmin.AreaController-costTime: [566ms]
2020-02-06 13:28:15.761[http-nio-8080-exec-7]INFO com.web.superadmin.AreaController-==end==
我们发现,上面的日志信息中,不仅有我们测试用例中日志信息,还有一些额外的日志信息,这些额外的日志信息是mybatis自己调用log打印的debug信息。
<property name="log.filePath" value="${catalina.base}/logs/webapps"/>
我们在logback中定义日志的存储路径为tomcat根路径。如何找到tomcat根路径呢?
在我们启动tomcat实例后,在控制台找下面关键字 Dcatalina.base
其后面的url就是tomcat实例的根目录。
我们顺利找到了需要的日志文件,上面的文件夹保存的是以前日志的压缩包,下面的三个文档就是当面的运行日志。
此为我们的error的日志信息。
2020-02-06 13:28:15.759[http-nio-8080-exec-7]ERRORcom.web.superadmin.AreaController-test error!