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的文件

 

上一篇:springboot超级详细的日志配置(基于logback)


下一篇:Logback自定义过滤器