您的位置:首页 > 编程语言 > Java开发

SpringBoot入门(四)日志输出

2017-08-15 13:47 239 查看
Java程序,日志输出,我认为是第一位的,把它的应用拿到这里。

spring-Boot对日志的处理,和我们往常的处理完全可以一致,通过logback.xml进行处理,即使有更先进的东西,我们也不用去管它。

这里,为了简便,我们任然使用前一篇的工程spring-boot-sample-data

第一步,在src/main/resources中增加logback.xml文件,文件内容为(这里仅最简单的,根据工程情况,进行相应的配置):

<?xml version="1.0" encoding="UTF-8"?>
<configuration  scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<property name="log.path" value="E:\\test\\logback.log" />
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>-->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!--输出到文件-->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logback.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>

<!-- logback为java中的包 -->
<logger name="com.dudu.controller"/>
<!--logback.LogbackDemo:类的全路径 -->
<logger name="com.dudu.controller.LearnController" level="WARN" additivity="false">
<appender-ref ref="console"/>
</logger>
</configuration>


注:1、控制台和日志文件的字符集

2、日志文件的存放位置,须要遵守Linux的命名规则


第二步,改造HelloController文件,改造结果如下

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/controller")
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;
protected static Logger logger=LoggerFactory.getLogger(HelloController.class);
@GetMapping(value="/helloctrl")
@DeleteMapping
public String say(@RequestParam(value="id",required=false,defaultValue="20") Integer myid) {
logger.info("访问helloName,Name={}",myid);
logger.info("访问");
logger.error("error");
return "id"+myid;
}
}


注:在添加引用时,日志的包一定是org.slf4j.Logger、org.slf4j.LoggerFactory

第三步、测试

1、运行程序

2、在浏览器中依次输入

http://localhost:8088/controller/helloctrl



参考:http://blog.csdn.net/lxhjh/article/details/51752419
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: