12

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

1
  • " it seems the configuration file logback-access.xml is not read." Can you be more specific ? Can you post the relevant part of the output when Spring Boot starts ? Commented Jan 3, 2017 at 17:04

2 Answers 2

8

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");

Solution

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>

Edit - Other solution for logback 1.1.6+

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");

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked. I have enabled accesslog.enabled to true in app.properties, then added logback-access.xml and now added the dependency.
For info, I just updated the link to spring-boot-ext-logback-access and the maven dependency: it has been updated since I posted this answer
Wanted to add that spring-boot-ext-logback-access does a lot more than just make your access logs readable. It allows it to work with spring profiles like the regular logback-spring.xml. So it's still worth using instead of just manually adding log-access.xml to the logback valve.
3

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:

  1. Add logback-access dependency
    implementation group: 'ch.qos.logback', name: 'logback-access', version: '1.2.3'
  1. Inject the TomcatServletWebServerFactory with added instance value of LogbackValve.
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addContextValves(new LogbackValve());
        return tomcat;
    }
  1. Add the below 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""

7 Comments

For Tomcat you need to set access logger to a DEBUG level in logback-spring.xml to see the REST logs <logger name="org.apache.catalina.valves.AccessLogValve" level="DEBUG"/>
registering LogbackValve into Tomcat Container context as tomcat.addContextValves(new LogbackValve()); is more than enough to get the traces of incoming API calls via the servlets in accesslogs. The above answer working for me long time in my applications
Can you share your related pom dependencies please. It seems like Springboot autoconfigures everything finding some libraries in the path and neither of the configurations above has any effect in my case.
I always use Gradle but this doesn't impact anything, make sure you have logback-access.xml file inside resources\conf directory. For verification please extract the jar and check the file in the classpath if it is present or not with the above mentioned directory structure.
I have added everything from your Answer, but it doesn't help. Although I already have the access-log entry printed, I can't change the format. If your variant would have worked - it could be helpful. Can you share logging related dependencies from your pom.xml please
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.