1. 下面是我的logback.xml的配置
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>D:/log/proxy/server.out</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>D:/log/proxy/api_open_%d{yyyy-MM-dd}-%i.log</fileNamePattern> <!-- <fileNamePattern>/app/tomcat/open-Platform/api-open/log/api_open_%d{yyyy-MM-dd}-%i.log</fileNamePattern> --> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="FILE" /> <appender-ref ref="STDOUT" /> <!--这一行可以控制是否输出到控制台--> </root> </configuration>
2. 添加依赖
<dependency> <groupId>uk.org.lidalia</groupId> <artifactId>sysout-over-slf4j</artifactId> <version>1.0.2</version> </dependency>
3.在程序中尽可能早的地方(如程序入口)添加以下代码
SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
4.web.xml中添加listener(实测好像不添加也可以)
<listener> <listener-class>uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4JServletContextListener</listener-class> </listener>
5. 实际效果
抄袭自: https://blog.csdn.net/yckiven/article/details/86251688