Is there any way I can configure spring boot logging to multiple files/console based on the configuration? i.e Some of the log statements should write into an audit file and normal log statements should go to console/normal log file.
Below is the code I have tried on spring boot example application.
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>
Below is the application.properties entries
logging.level.org.springframework.web=INFO
logging.file=logs/spring-boot-logging.log
Below the 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!";
}
}
Can someone please help? Did anyone face similar situation?
Thanks a lot
Just add another logger and appender in your configuration file and create logger object using the name of Logger defined in the configuration. For more explanation check Spring boot multiple log files
Spring boot logging into multiple files and we can create log into selected class also. I used the following
logback.xml
Dependency :
Standard logback example, two files with different packages going to different files :