1.新建logback-spring.xml文件
内容如下
<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
<configuration scan="true" scanPeriod="10 seconds">
<include resource="org/springframework/boot/logging/logback/base.xml" />
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<File>${LOG_PATH}/error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/error-%d{yyyyMMdd}.log.%i
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>500MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>2</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n
</Pattern>
</layout>
</appender>
<!-- hibernate日志输入 -->
<!-- <logger name="org.hibernate.type.descriptor.sql.BasicBinder"
level="TRACE" />
<logger name="org.hibernate.type.descriptor.sql.BasicExtractor"
level="TRACE" />
<logger name="org.hibernate.SQL" level="INFO" />
<logger name="org.hibernate.engine.QueryParameters" level="INFO" />
<logger name="org.hibernate.engine.query.HQLQueryPlan" level="INFO" /> -->
<root level="INFO">
<appender-ref ref="ERROR_FILE" />
</root>
</configuration>
2.在application.properties文件中引入日志配置文件
logging.config=classpath:logback-spring.xml
logging.path=log/newscreening
3.这时就可以使用log对象了
public class LoginController {
private static final Logger log = LoggerFactory.getLogger(LoginController.class);
@RequestMapping("/loginValidate")
public String loginValidate(HttpSession session,String userName,String password,String validCode){
if(条件){
log.info("=================验证码错误=====================");
}else{
log.error("================验证码正确=====================")
}
return null;
}
运行程序后,进入路径会有两个文件