cf-java-logging-support icon indicating copy to clipboard operation
cf-java-logging-support copied to clipboard

Hide Request Logs for health 200 requests

Open RobertKinda opened this issue 10 months ago • 3 comments

Hi,

when we run our application on cf each request gets logged through the GenerateRequestLogFilter. Is there a way to configure it, so that successful (200) health calls are ignored and not logged?

Best Regards, Robert

RobertKinda avatar Feb 28 '25 14:02 RobertKinda

Hi, thanks for providing this issue. There is no implementation of limiting the creation of request logs by the status code. If there was such a feature, it would need to be configurable for groups of status codes: 1xx, 2xx, 3xx, 4xx, 5xx at the very least.

I can see two ways to implement this functionality:

  1. Add the status code check to the GenerateRequestLogFilter. https://github.com/SAP/cf-java-logging-support/blob/2becc28d028b4340b693330d90f924b50f22e67c/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/GenerateRequestLogFilter.java#L84-L86 Since cf-java-logging-support allows to customize the servlet filters with own implementations, you could take the current implementation and provide your own class with the filtering condition to your liking.

  2. Implement a filter for your logging backend (logback or log4j2) that inspects each log messages and rejects all request logs according to the specified rules. Again, you could do this yourself. You can follow the approaches in the CustomLoggingTurboFilter (logback) or the DynamicLevelPrefixLoggerFilter (log4j2). In both cases you would scan the params for a RequestRecord instance. The getFields() map will contain the status code to check.

Due to the effort in scanning all log messages, I think the first approach is superior.

@RobertKinda: Is this information enough for you to solve your problem? Or do you want to see the filtering as a new feature in this library? In that case, I would need to think about, how to best allow configuration of the filtering.

KarstenSchnitter avatar Feb 28 '25 21:02 KarstenSchnitter

Hi @KarstenSchnitter,

thanks for introducing me to the touchpoint where I could implement my own logic. I think generally this should be a feature that should be taken into consideration. To give you an example why we would want that: We generally have a battery on health checks (/health /health/serviceA /health/serviceB) all monitored from another component. That produces of course a lot of clutter - Which we'd like to avoid.

RobertKinda avatar Mar 03 '25 11:03 RobertKinda

@RobertKinda servlet filters are registered to a path. You can leave out your health endpoints from that registration. Follow this documentation https://github.com/SAP/cf-java-logging-support/wiki/Instrumenting-Servlets#registering-the-servlet-filter and adjust the url patterns accordingly.

KarstenSchnitter avatar Mar 03 '25 13:03 KarstenSchnitter