Skip to content

Improve LoggingCacheErrorHandler#28648

Closed
vpavic wants to merge 2 commits into
spring-projects:mainfrom
vpavic:improve-logging-cache-error-handler
Closed

Improve LoggingCacheErrorHandler#28648
vpavic wants to merge 2 commits into
spring-projects:mainfrom
vpavic:improve-logging-cache-error-handler

Conversation

@vpavic

@vpavic vpavic commented Jun 17, 2022

Copy link
Copy Markdown
Contributor

This PR contains 2 commits with some improvements to LoggingCacheErrorHandler:

  1. Simplify creation of LoggingCacheErrorHandler with logged stacktrace:
    At present, creating LoggingCacheErrorHandler that logs stacktrace also requires supplying the logger to be used. This might be inconvenient for some users, as it requires usage of Commons Logging API. This commit simplifies creation of such as LoggingCacheErrorHandler instance by adding a constructor that only accepts a boolean flag indicating whether to log stacktrace.

  2. Defer log message creation in LoggingCacheErrorHandler:
    At present, LoggingCacheErrorHandler creates log message eagerly and does not verify whether warn level is enabled at all for the used logger. This commit ensures that log message creation is deferred until it is about to be logged.

The first commit could maybe be update to deprecate the existing constructor that takes org.apache.commons.logging.Log and replace it with the one that takes String representing logger name as that way Commons Logging dependency wouldn't leak out at all. But I'd leave that decision to whoever reviews this PR.

At present, creating LoggingCacheErrorHandler that logs stacktrace also requires supplying the logger to be used. This might be inconvenient for some users, as it requires usage of Commons Logging API.

This commit simplifies creation of such as LoggingCacheErrorHandler instance by adding a constructor that only accepts a boolean flag indicating whether to log stacktrace.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 17, 2022
At present, LoggingCacheErrorHandler creates log message eagerly and does not verify whether warn level is enabled at all for the used logger.

This commit ensures that log message creation is deferred until it is about to be logged.
@vpavic vpavic force-pushed the improve-logging-cache-error-handler branch from e76f6f7 to 5da2dec Compare June 17, 2022 15:12
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Jun 17, 2022
@sbrannen

Copy link
Copy Markdown
Member

I think it's a good idea to check if the WARN log level is enabled, and switching to a Supplier is also a good improvement.

The only real concern I have with the proposed changes is that the logCacheError() method is protected. Thus, that is technically a breaking change in case somebody has subclassed LoggingCacheErrorHandler.

On the other hand, LoggingCacheErrorHandler was introduced in 5.3.16, so perhaps there is little risk in changing the signature of the protected method at this point.

@snicoll, thoughts?

@sbrannen sbrannen added this to the Triage Queue milestone Jun 17, 2022
@vpavic

vpavic commented Jun 18, 2022

Copy link
Copy Markdown
Contributor Author

Good point @sbrannen, I overlooked that #logCacheError is protected.

I guess that for the 5.3.x branch it shouldn't be an issue to ensure backwards compatibility using something like this:

--- a/spring-context/src/main/java/org/springframework/cache/interceptor/LoggingCacheErrorHandler.java
+++ b/spring-context/src/main/java/org/springframework/cache/interceptor/LoggingCacheErrorHandler.java
@@ -113,4 +113,15 @@ public class LoggingCacheErrorHandler implements CacheErrorHandler {
                }
        }
 
+       /**
+        * Log the specified message.
+        * @param logger the logger
+        * @param message the message
+        * @param ex the exception
+        * @deprecated as of 5.3.22 in favor of {@link #logCacheError(Log, Supplier, RuntimeException)}
+        */
+       @Deprecated
+       protected void logCacheError(Log logger, String message, RuntimeException ex) {
+               logCacheError(logger, () -> message, ex);
+       }
+
 }

@sbrannen

Copy link
Copy Markdown
Member

I guess that for the 5.3.x branch it shouldn't be an issue to ensure backwards compatibility using something like this:

Right. I also thought about doing that, but it would only cover half of the issue.

Keeping the existing logCacheError() method would work for people who have extended LoggingCacheErrorHandler, overridden one of the handle*() methods, and invoked logCacheError().

But it would not work for anyone who has overridden logCacheError(Log, String, RuntimeException) since the new code (in this PR) never invokes logCacheError(Log, String, RuntimeException). In other words, any existing customization in an overridden logCacheError(Log, String, RuntimeException) method would be ignored.

@sbrannen

Copy link
Copy Markdown
Member

@vpavic, can you please submit the Simplify creation of LoggingCacheErrorHandler with logged stacktrace commit in a separate PR?

We can then get that merged in on its own since it's noncontroversial.

And we can focus this PR on the discussion of the other proposed changes.

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Jun 21, 2022
@vpavic

vpavic commented Jun 21, 2022

Copy link
Copy Markdown
Contributor Author

Sure, I can do that:

Though note there's one remark in the PR description that impacts the commit that's now subject of a separate PR:

The first commit could maybe be update to deprecate the existing constructor that takes org.apache.commons.logging.Log and replace it with the one that takes String representing logger name as that way Commons Logging dependency wouldn't leak out at all. But I'd leave that decision to whoever reviews this PR.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 21, 2022
sbrannen pushed a commit to sbrannen/spring-framework that referenced this pull request Jun 21, 2022
@sbrannen sbrannen added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Jun 21, 2022
@sbrannen

sbrannen commented Jun 21, 2022

Copy link
Copy Markdown
Member

Thanks for the fruitful discussions, @vpavic!

This PR has now been superseded by #28670 and #28672.

@sbrannen sbrannen closed this Jun 21, 2022
@sbrannen sbrannen removed this from the Triage Queue milestone Jun 21, 2022
@vpavic vpavic deleted the improve-logging-cache-error-handler branch June 21, 2022 12:51
sbrannen added a commit to sbrannen/spring-framework that referenced this pull request Jun 21, 2022
Since LoggingCacheErrorHandler was only recently introduced in 5.3.16,
we have decided to completely revise its internals (protected API) in
5.3.x while retaining the current public API.

Specifically, this commit:

- introduces protected getLogger() and isLogStackTraces() methods to
  improve extensibility

- revises logCacheError() to accept a Supplier<String> for lazy
  resolution of error messages

Closes spring-projectsgh-28672
See spring-projectsgh-28670, spring-projectsgh-28648
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants