1. 程式人生 > >logback配置、指定類日誌輸出到指定資料夾

logback配置、指定類日誌輸出到指定資料夾

1、controller程式碼demo

package com.duke.api;

@Controller

public class demo {

private static final Logger logger = LoggerFactory.getLogger(demo.class);

    @RequestMapping("/test")

    public void testDemo(HttpServletRequest request, HttpServletResponse resp){

        String string = request.getParameter("demo");


        logger.info(string);

    }

}

2、logback.xml

指定demo類中的日誌資訊輸出到自定義資料夾中(/home/server/logs/)

<!--  定義日誌儲存的地址 -->

<property name="log_address" value="/home/server/logs" />

<!-- log info start -->
<appender name="demo-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log_address}/demo-info(自定義日誌儲存的資料夾)/demo(檔名).%d{yyyy-MM-dd}.log
</FileNamePattern>

</rollingPolicy>

          <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</layout>
</encoder>
<!--日誌檔案最大的大小 -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>

</appender>

<logger name="com.duke.api.Demo" level="DEBUG">
<appender-ref ref="demo-info" />

</logger>

這就是指定類輸出日誌到指定資料夾的全部配置,當訪問該類的時候

會在伺服器/home/server/logs/demo-info/下生成一個日誌檔案

類似demo.2000-01-01.log的檔案