diff --git a/README.md b/README.md index 8c6a014..2809512 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Or, for Spring Boot 3: no.idporten.logging idporten-access-log-spring-boot-3-starter - 2.0.2 + 2.3.4 ``` @@ -77,20 +77,24 @@ spring: environment: current-running-environment ``` -If you want to disable the Tomcat access logging completely, e.g. for local development, you can set the following property: +The library uses the standard tomcat accesslog property for enabling or disabling logging: ```yaml -tomcat: - accesslog: disabled +server: + tomcat: + accesslog: + enabled: true # default is true if not set ``` -Set this property to 'enabled' or remove completely to enable Tomcat access logging. Use your own logback-access.xml file or configure debug-logging: ```yaml digdir: access: logging: - debug-level: request + debug-level: request # [request|response], default config if not set or null config-file: my-logback.xml # will override debug setting + filtering: + static-resources: true # filters out static resources. default is true + paths: /config.json, /.well-known # comma-separated list of paths to filter out. Matches paths using .startsWith(). Default is empty. ``` USE EITHER `debug-level` OR `config-file`, not both. Valid values for debug-level are: diff --git a/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/AccessLogsConfiguration.java b/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/AccessLogsConfiguration.java index e13989a..c84bc49 100644 --- a/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/AccessLogsConfiguration.java +++ b/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/AccessLogsConfiguration.java @@ -10,6 +10,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.List; + @Configuration @EnableConfigurationProperties(AccessLogsProperties.class) public class AccessLogsConfiguration { @@ -32,9 +34,18 @@ public static AccessLogsProperties getProperties() { @Value("${digdir.access.logging.debug-level:}") String debugLevel; + @Value("${digdir.access.logging.filtering.static-resources:true}") + boolean filterStaticResources; + + @Value("${digdir.access.logging.filtering.paths:}") + List filterPaths; + + @Value("${tomcat.accesslog:}") + String deprecatedTomcatAccessLogProperty; + @Bean - @ConditionalOnProperty(prefix = "tomcat", name = "accesslog", havingValue = "enabled", matchIfMissing = true) + @ConditionalOnProperty(name = "server.tomcat.accesslog.enabled", havingValue = "true", matchIfMissing = true) public WebServerFactoryCustomizer accessLogsCustomizer(AccessLogsProperties props) { if (properties == null) { properties = props; @@ -43,10 +54,16 @@ public WebServerFactoryCustomizer accessLogsCusto LoggerFactory.getLogger(AccessLogsConfiguration.class).info("Initialize accessLogsCustomizer for Tomcat Access Logging as JSON. Use config-file: " + logbackConfigFile); + if(deprecatedTomcatAccessLogProperty != null && !deprecatedTomcatAccessLogProperty.equals("enabled")) { // deprecated property is set, and set to something else than 'enabled' + LoggerFactory.getLogger(AccessLogsConfiguration.class).warn("Property 'tomcat.accesslog' is deprecated. Use 'server.tomcat.accesslog.enabled=false' instead."); + } + return factory -> { var logbackValve = new LogbackValve(); logbackValve.setFilename(logbackConfigFile); logbackValve.setAsyncSupported(true); + logbackValve.setFilterStaticResources(filterStaticResources); + logbackValve.setFilterPaths(filterPaths); factory.addContextValves(logbackValve); }; } diff --git a/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/tomcat/LogbackValve.java b/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/tomcat/LogbackValve.java index ee9e8d9..46f9df9 100644 --- a/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/tomcat/LogbackValve.java +++ b/idporten-access-log-spring-boot-3-starter/src/main/java/no/idporten/logging/access/tomcat/LogbackValve.java @@ -36,6 +36,8 @@ import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.valves.ValveBase; +import org.springframework.web.servlet.HandlerMapping; +import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import java.io.File; import java.io.IOException; @@ -96,6 +98,17 @@ public class LogbackValve extends ValveBase private ScheduledExecutorService scheduledExecutorService; + private boolean filterStaticResources = true; + public void setFilterStaticResources(boolean filterStaticResources) { + this.filterStaticResources = filterStaticResources; + } + + private List filterPaths = null; + public void setFilterPaths(List filterPaths) { + this.filterPaths = filterPaths; + } + + public LogbackValve() { putObject(CoreConstants.EVALUATOR_MAP, new HashMap>()); } @@ -242,6 +255,24 @@ public void invoke(Request request, Response response) throws IOException, Servl getNext().invoke(request, response); + if (response.getStatus() < 400) { // only filter out successful requests + if (filterStaticResources) { + Object handlerObject = request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE); + if (handlerObject instanceof ResourceHttpRequestHandler) { + return; // skip logging static resources + } + } + + if (filterPaths != null) { + String requestURI = request.getRequestURI(); + for (String filterPath : filterPaths) { + if (requestURI.startsWith(filterPath)) { + return; // skip logging paths that are filtered + } + } + } + } + TomcatServerAdapter adapter = new TomcatServerAdapter(request, response); IAccessEvent accessEvent = new AccessEvent(this, request, response, adapter); diff --git a/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-full.xml b/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-full.xml index 71d3f7c..dd126e3 100644 --- a/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-full.xml +++ b/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-full.xml @@ -3,27 +3,6 @@ - - - - String uri = event.getRequestURI(); - if((event.getStatusCode() == 200 || event.getStatusCode() == 304) - && ((uri.startsWith("/css")) - || (uri.startsWith("/js")) - || (uri.startsWith("/webfonts")) - || (uri.startsWith("/img")) - || (uri.startsWith("/favicon")))){ - return true; - } - else if(uri.startsWith("/apple")){ - return true; - } - return false; - - - NEUTRAL - DENY - diff --git a/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-resp-full.xml b/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-resp-full.xml index bb80689..c55e780 100644 --- a/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-resp-full.xml +++ b/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access-req-resp-full.xml @@ -3,27 +3,6 @@ - - - - String uri = event.getRequestURI(); - if((event.getStatusCode() == 200 || event.getStatusCode() == 304) - && ((uri.startsWith("/css")) - || (uri.startsWith("/js")) - || (uri.startsWith("/webfonts")) - || (uri.startsWith("/img")) - || (uri.startsWith("/favicon")))){ - return true; - } - else if(uri.startsWith("/apple")){ - return true; - } - return false; - - - NEUTRAL - DENY - diff --git a/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access.xml b/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access.xml index cdbf51f..c73ba77 100644 --- a/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access.xml +++ b/idporten-access-log-spring-boot-3-starter/src/main/resources/logback-access.xml @@ -3,27 +3,6 @@ - - - - String uri = event.getRequestURI(); - if((event.getStatusCode() == 200 || event.getStatusCode() == 304) - && ((uri.startsWith("/css")) - || (uri.startsWith("/js")) - || (uri.startsWith("/webfonts")) - || (uri.startsWith("/img")) - || (uri.startsWith("/favicon")))){ - return true; - } - else if(uri.startsWith("/apple")){ - return true; - } - return false; - - - NEUTRAL - DENY - diff --git a/pom.xml b/pom.xml index 79e0607..72d2348 100644 --- a/pom.xml +++ b/pom.xml @@ -28,8 +28,8 @@ 3.13.0 3.3.1 + 3.3.1 3.1.2 - 3.3.0