spring boot 學習筆記(006)log

spring boot的log輸出,默認不會輸出到控制檯上。


spring boot配置log4j,好久都沒配置成功。

後來網上查到這麼一句話:

Logback是log4j框架的作者開發的新一代日誌框架,它效率更高、能夠適應諸多的運行環境,同時天然支持SLF4J。

好吧,既然是更新更牛x的一個東東,那我們就學着用用唄。


1,在resources目錄下,新建:logbak.xml文件

<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->  
<configuration scan="true" scanPeriod="10 seconds">  

  <!-- Simple file output -->  
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
    <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->  
    <encoder>  
        <pattern>  
            [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n  
        </pattern>
        <!-- 此處設置字符集 -->
        <charset>UTF-8</charset>
    </encoder>  
  
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
      <!-- rollover daily 配置日誌所生成的目錄以及生成文件名的規則 -->  
      <fileNamePattern>logs/mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>  
      <timeBasedFileNamingAndTriggeringPolicy  
          class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">  
        <!-- or whenever the file size reaches 64 MB -->  
        <maxFileSize>64 MB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>  
    </rollingPolicy>  

    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
      <level>DEBUG</level>  
    </filter>  
    <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->  
    <prudent>true</prudent>  
  </appender>  
  
  
  <!-- Console output -->  
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
    <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->  
      <encoder>  
          <pattern>  
              [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n  
          </pattern>
          <!-- 此處設置字符集 -->  
          <charset>UTF-8</charset>
      </encoder>  
    <!-- Only log level WARN and above -->  
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
      <level>DEBUG</level>  
    </filter>  
  </appender>  
  
  
  <!-- Enable FILE and STDOUT appenders for all log messages.  
       By default, only log at level INFO and above. -->  
  <root level="INFO">  
    <appender-ref ref="FILE" />  
    <appender-ref ref="STDOUT" />  
  </root>  
  
  <!-- For loggers in the these namespaces, log at all levels. -->  
  <logger name="pedestal" level="ALL" />  
  <logger name="hammock-cafe" level="ALL" />  
  <logger name="user" level="ALL" />  
</configuration>  

2,在HelloWorld.java中,添加log輸出:

package springboot;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Controller
@EnableAutoConfiguration
@SpringBootApplication
public class HelloWorld {

	private static Logger logger = Logger.getLogger(HelloWorld.class.getName()); 
	
	public static void main(String[] args) {
		//第一個簡單的應用,  
        SpringApplication.run(HelloWorld.class,args);
	}

	@RequestMapping("/hello")  
    @ResponseBody  
    public String hello(){
		logger.log(Level.INFO, "just test the log. (/hello)");
        return "Hello World";  
    }  

	@RequestMapping("/rundemo")  
    @ResponseBody  
    public UserInfo demo(){
		logger.log(Level.INFO, "just test the log. (/rundemo)");
		UserInfo u = new UserInfo();
		u.setUserCode("h001");
		u.setUserName("u name");
		u.setDeptCode("ab001");
        return u;  
    }  

	@RequestMapping(value="/trequest", method = RequestMethod.POST)  
	@ResponseBody
    public UserInfo trequest(@RequestBody UserInfo pu){
		logger.log(Level.INFO, "just test the log. (/trequest)");

		UserInfo u = new UserInfo();
		u.setUserCode(pu.getUserCode());
		u.setUserName(pu.getUserName());
		u.setDeptCode(pu.getDeptCode());

		return u;
    }


}

在控制檯上就看到log輸出了。

不過log研究只是個半拉子,還有很多東西沒搞透。先解決有無問題,後面再深入研究。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章