My application.yml is :
server:
tomcat:
accesslog:
enabled: true
basedir: my-tomcat
We use spring boot 1.4.3.RELEASE and I would like to configure a logback-access.xml ( under src/main/resources) with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- always a good activate OnConsoleStatusListener -->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%h %l %u %user %date "%r" %s %b</pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
I can see an access_log.2017-01-03.log file under my-tomcat folder with the right access logs but noting on my concole, it seems the configuration file logback-access.xml is not read.
Any idea ?
Eric
Am I mistaken or is this not supported natively by Spring Boot ?
Source: https://github.com/spring-projects/spring-boot/issues/2609:
Hey, I'm trying to get logback-access + tomcat working with spring boot. Has anyone been able to get this working out-of-the-box? Or is there some necessary plumbing to set up?
...
As a workaround, you can copy the access xml from the class path to the filesystem and run it there as part of your configuration class Files.copy(this.getClass().getResourceAsStream("/logback-access.xml"),Paths.get("log-access.xml"),StandardCopyOption.REPLACE_EXISTING); logbackValve.setFilename("log-access.xml");
Use spring-boot-ext-logback-access:
Simply adding the dependency should do it:
<dependency>
<groupId>net.rakugakibox.spring.boot</groupId>
<artifactId>logback-access-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
On the spring-boot issue mentioned above, someone posted this:
Since logback 1.1.6 there is no need of any workarounds in order to load the logback-access configuration file as a resource. Reference: http://jira.qos.ch/browse/LOGBACK-1069
All you have to do is:
logbackValve.setFilename("log-access.xml");
Though I am late to the party posting my simple working code for posterity.
The logback
's ACCESS log can be printed in the console log by the following steps:
logback-access
dependency implementation group: 'ch.qos.logback', name: 'logback-access', version: '1.2.3'
TomcatServletWebServerFactory
with added instance value of LogbackValve
. @Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addContextValves(new LogbackValve());
return tomcat;
}
logback-access.xml
into the classpath resources\conf
directory.<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>combined</Pattern>
<Pattern>[ACCESS] %h %l %u %t{yyyy-MM-dd HH:mm:ss.SSS} %s %b %D ms</Pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
The access log would be printed in the console as
"[ACCESS] <host> <date> "<httpmethod> <httpuri> HTTP/1.1" <httpstatus> "<timetaken in millisecond> ms""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With