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研究只是个半拉子,还有很多东西没搞透。先解决有无问题,后面再深入研究。