Hi.

On Thu, 21 Apr 2022 17:07:27 -0400
Chris Cheshire <yahoono...@gmail.com> wrote:

> Tomcat 9.
> 
> I wanted to separate out access logs for external api calls to log different
> info than the standard access log line. For example, the api key used which
> is set as a request header.
> 
> Adding that to the pattern was easy. 
> 
> However the conditional logging was clunky. I found the ‘conditionIf’ and
> ‘conditionUnless’ attributes for the access log valve, but these only work on
> request attributes, not headers (at least that’s what the documentation says).
> 
> I have created a filter that copies the values from the request headers to
> equivalent  attributes, just so the condition can work. This is where it
> feels  clunky, especially since the access log valve has replacement
> parameters for logging request headers.

I have solved a similar issue with https://logback.qos.ch/access.html and
https://logback.qos.ch/manual/loggingSeparation.html

That's the relevant snipplet for logback-access.xml. It uses the powerful
filter feature from logback https://logback.qos.ch/manual/filters.html .

I attach the configmap definition which shows the whole config.

```
      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!--
          Don't log the probs request from OpenShift
        -->
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
          <evaluator name="DontLogHealth">
            <expression>
              if(event.getRequestURI().contains("/health"))
                return true;
              return false;
            </expression>
          </evaluator>
          <onMatch>DENY</onMatch>
        </filter>
```

The log back access filter can be used to filter based on some AccessEvent like
a request header.

https://logback.qos.ch/manual/filters.html#access_EvalutorFilter
https://logback.qos.ch/xref/ch/qos/logback/access/spi/AccessEvent.html

I think that you can also define based on the request header which logger
should be used but I never had such a requirement .

Just replace the tomcat access valve with the logback one.

```
<Valve className="ch.qos.logback.access.tomcat.LogbackValve"
            quiet="false" filename="conf/logback-access.xml"/>
```

It was required to add this lib to the ext dir of tomcat.
Maybe there are some newer versions available.

```
ls -1 /opt/apache/tomcat/base/lib/ext/
commons-compiler-3.1.3.jar
jackson-annotations-2.9.9.jar
jackson-core-2.9.9.jar
jackson-databind-2.9.9.3.jar
janino-3.1.3.jar
logback-access-1.2.3.jar
logback-core-1.2.3.jar
logstash-logback-encoder-6.6.jar

```

> Is there a technical reason why the condition checking can’t work on request
> headers in the valve? If not, can this be considered as a feature request
> please?
> 
> Thanks


Regards
Alex

Attachment: logback-access-xml-cm.yaml
Description: application/yaml

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to