You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "vy (via GitHub)" <gi...@apache.org> on 2024/03/04 10:24:35 UTC
[PR] Don't fail on insufficient parameters in `ParameterFormatter` (#2337) (logging-log4j2)
vy opened a new pull request, #2343:
URL: https://github.com/apache/logging-log4j2/pull/2343
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Don't fail on insufficient parameters in `ParameterFormatter` (#2337) (logging-log4j2)
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #2343:
URL: https://github.com/apache/logging-log4j2/pull/2343#discussion_r1510945299
##########
log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java:
##########
@@ -236,7 +240,10 @@ static void formatMessage(
final String message = String.format(
"found %d argument placeholders, but provided %d for pattern `%s`",
analysis.placeholderCount, args.length, pattern);
- throw new IllegalArgumentException(message);
Review Comment:
Personally I am pretty much in favor of throwing an exception here: [`java.util.Formatter`](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html) does it, so we can also throw.
On the other hand we should probably catch the exception in `ParameterizedMessage#getFormattedMessage`, as it happens in other kinds of messages:
https://github.com/apache/logging-log4j2/blob/28b70d1d7fe63417e8eaa1abe8bf992363060515/log4j-api/src/main/java/org/apache/logging/log4j/message/StringFormattedMessage.java#L120-L126
https://github.com/apache/logging-log4j2/blob/28b70d1d7fe63417e8eaa1abe8bf992363060515/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageFormatMessage.java#L113-L119
##########
log4j-api-test/src/test/java/org/apache/logging/log4j/message/ParameterFormatterTest.java:
##########
@@ -17,21 +17,27 @@
package org.apache.logging.log4j.message;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.logging.log4j.message.ParameterFormatter.MessagePatternAnalysis;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.test.junit.UsingStatusLoggerMock;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentCaptor;
/**
* Tests {@link ParameterFormatter}.
*/
-public class ParameterFormatterTest {
+@UsingStatusLoggerMock
+class ParameterFormatterTest {
Review Comment:
I am not a big fan of mocking the entire `StatusLogger`: this prevents the test to run in parallel with other tests.
I prefer the [`@UsingStatusListener`](https://github.com/apache/logging-log4j2/blob/2.x/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingStatusListener.java) extension to register a per-test status listener.
Of course neither of us is impartial in the choice of JUnit extension to use. ;-)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Don't fail on insufficient parameters in `ParameterFormatter` (#2337) (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy merged PR #2343:
URL: https://github.com/apache/logging-log4j2/pull/2343
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Don't fail on insufficient parameters in `ParameterFormatter` (#2337) (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #2343:
URL: https://github.com/apache/logging-log4j2/pull/2343#discussion_r1512757491
##########
log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java:
##########
@@ -236,7 +240,10 @@ static void formatMessage(
final String message = String.format(
"found %d argument placeholders, but provided %d for pattern `%s`",
analysis.placeholderCount, args.length, pattern);
- throw new IllegalArgumentException(message);
Review Comment:
I have updated it as you requested. Could you review the changes in 1084855467f554f2dabf34af4d860018fb098fd8, please?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Don't fail on insufficient parameters in `ParameterFormatter` (#2337) (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #2343:
URL: https://github.com/apache/logging-log4j2/pull/2343#discussion_r1513375283
##########
log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterizedMessage.java:
##########
@@ -274,7 +278,12 @@ public void formatTo(final StringBuilder buffer) {
buffer.append(formattedMessage);
} else {
final int argCount = args != null ? args.length : 0;
- ParameterFormatter.formatMessage(buffer, pattern, args, argCount, patternAnalysis);
+ try {
+ ParameterFormatter.formatMessage(buffer, pattern, args, argCount, patternAnalysis);
+ } catch (final Exception error) {
+ STATUS_LOGGER.error("message formatting failure", error);
Review Comment:
Fixed in a40714e415813433df099f6d15de5b511ea3f910.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Don't fail on insufficient parameters in `ParameterFormatter` (#2337) (logging-log4j2)
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #2343:
URL: https://github.com/apache/logging-log4j2/pull/2343#discussion_r1512770976
##########
log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterizedMessage.java:
##########
@@ -274,7 +278,12 @@ public void formatTo(final StringBuilder buffer) {
buffer.append(formattedMessage);
} else {
final int argCount = args != null ? args.length : 0;
- ParameterFormatter.formatMessage(buffer, pattern, args, argCount, patternAnalysis);
+ try {
+ ParameterFormatter.formatMessage(buffer, pattern, args, argCount, patternAnalysis);
+ } catch (final Exception error) {
+ STATUS_LOGGER.error("message formatting failure", error);
Review Comment:
Nit: the message is "Unable to format msg: {}" in the case of `StringFormattedMessage`. Can we use the same message everywhere?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Don't fail on insufficient parameters in `ParameterFormatter` (#2337) (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #2343:
URL: https://github.com/apache/logging-log4j2/pull/2343#discussion_r1513382158
##########
log4j-api-test/src/test/java/org/apache/logging/log4j/message/ParameterFormatterTest.java:
##########
@@ -17,21 +17,27 @@
package org.apache.logging.log4j.message;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.logging.log4j.message.ParameterFormatter.MessagePatternAnalysis;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.test.junit.UsingStatusLoggerMock;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentCaptor;
/**
* Tests {@link ParameterFormatter}.
*/
-public class ParameterFormatterTest {
+@UsingStatusLoggerMock
+class ParameterFormatterTest {
Review Comment:
Piotr, if I would have been a programmer who also happens to be a serial killer, you would not want me to see what you have done in `UsingStatusListener` et al. using `ThreadLocal`s. :sunglasses: I find it a liability with several fragile assumptions just to make tests needing `StatusLogger` work in parallel. I have so much dislike towards such design choices, but I will spare that for another time.
Just to show how much I like you, I have replaced `@UsingStatusLoggerMock` with `@UsingStatusListener` in 186c580d2cda1f301e7b5eab14ded8a15e5b54f5 as you requested. :stuck_out_tongue_closed_eyes:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org