spring集成logback

springboot自带logback所需的maven文件,所以pom.xml中不用引入。
日志不用启动spring也是可以用的,非常方便。还可以使用占位符。代码:

logger.info("希望你 {},{}",666,999);

java 代码如下:

@Controller
class IndexController {
	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("name","hello pillar");
        logger.error("参数验证失败");
        return "index";
    }
}

logback-spring.xml文件内容:



<configuration>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
            pattern>
        layout>
    appender>

    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>DENYonMatch>
            <onMismatch>ACCEPTonMismatch>
        filter>
        <encoder>
            <pattern>
                %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>D:/log/logbackInfo.%d.logfileNamePattern>
        rollingPolicy>
    appender>

    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>D:/log/logbackError.%d.logfileNamePattern>
        rollingPolicy>
    appender>
    <root level="INFO">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    root>
configuration>

有几个appender日志就会输出到几个地方。
该配置文件有日志会输出到3个地方。console,filelog,errorlog。

你可能感兴趣的:(spring)