有什么办法可以根据配置将spring boot日志记录配置到多个文件/控制台?即某些日志语句应写入审计文件,而正常的日志语句应写入控制台/普通日志文件。
以下是我在spring boot示例应用程序中尝试过的代码。
的logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="file" level="DEBUG" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="org.hello" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
</configuration>
以下是application.properties条目
logging.level.org.springframework.web=INFO
logging.file=logs/spring-boot-logging.log
HelloController下面
@RestController
public class HelloController {
Logger logger = LoggerFactory.getLogger(HelloController.class);
Logger logger1 = LoggerFactory.getLogger("file");
@RequestMapping("/")
public String index() {
logger.info("My Log test");
logger1.info("My Audit test");
return "Greetings from Spring Boot!";
}
}
有人可以帮忙吗?有没有人面临类似的情况?
非常感谢
答案 0 :(得分:2)
标准的logback示例,两个包含不同包的文件转到不同的文件:
<configuration>
<appender name="FILE1" class="ch.qos.logback.core.FileAppender">
<file>myApp1.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE2" class="ch.qos.logback.core.FileAppender">
<file>myApp1.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<logger name="com.package1.foo" level="DEBUG">
<appender-ref ref="FILE1" />
</logger>
<logger name="com.package2.bar" level="DEBUG">
<appender-ref ref="FILE2" />
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
答案 1 :(得分:0)
只需在配置文件中添加另一个logger和appender,并使用配置中定义的Logger名称创建logger对象。有关更多说明 校验 Spring boot multiple log files
答案 2 :(得分:0)
Spring boot登录到多个文件,我们还可以创建登录到选定类的日志。
我使用了以下logback.xml
<configuration>
<appender name="logOutPut" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/WebServe-logback.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/WebServe-log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="errorlab" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/error-logback.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/error-log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="jdbc.sqltiming.appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/WebServe-sql-timing.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/WebServe-sql-timing-log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.tasks.MailNotificationTask">
<level value="ALL" />
<appender-ref ref="errorlab" />
</logger>
<logger name="jdbc.sqltiming">
<appender-ref ref="jdbc.sqltiming.appender" />
</logger>
<root level="debug">
<appender-ref ref="logOutPut" />
</root>
依赖性:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>