-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix StatusLogger
time-zone issues and stack overflow
#2322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
918190e
Add time-zone support to `StatusLogger`
vy 78e2f14
Make sure `StatusLogger#logMessage()` failures don't cause stack over…
vy 00cf39d
Fix build failures
vy 89417c7
Add changelog entries
vy db30243
Harden invalid argument fallbacks
vy ee4d520
Support environment variables
vy d65611a
Use the new property naming style
vy 9c52bfe
Simplify `StatusLoggerPropertiesUtilDoubleTest` test cases
vy 6fd3ff1
Merge remote-tracking branch 'origin/2.x' into 2.x-StatusLogger-fix
vy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
log4j-api-test/src/test/java/org/apache/logging/log4j/status/StatusLoggerDateTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.status; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.function.Supplier; | ||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.message.ParameterizedNoReferenceMessageFactory; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.mockito.Mockito; | ||
import org.mockito.stubbing.Answer; | ||
|
||
class StatusLoggerDateTest { | ||
|
||
private static final String INSTANT_YEAR = "1970"; | ||
|
||
private static final String INSTANT_MONTH = "12"; | ||
|
||
private static final String INSTANT_DAY = "27"; | ||
|
||
private static final String INSTANT_HOUR = "12"; | ||
|
||
private static final String INSTANT_MINUTE = "34"; | ||
|
||
private static final String INSTANT_SECOND = "56"; | ||
|
||
private static final String INSTANT_FRACTION = "789"; | ||
|
||
private static final Instant INSTANT = Instant.parse(INSTANT_YEAR | ||
+ '-' | ||
+ INSTANT_MONTH | ||
+ '-' | ||
+ INSTANT_DAY | ||
+ 'T' | ||
+ INSTANT_HOUR | ||
+ ':' | ||
+ INSTANT_MINUTE | ||
+ ':' | ||
+ INSTANT_SECOND | ||
+ '.' | ||
+ INSTANT_FRACTION | ||
+ 'Z'); | ||
|
||
private static final Supplier<Instant> CLOCK = () -> INSTANT; | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"yyyy-MM-dd," + (INSTANT_YEAR + '-' + INSTANT_MONTH + '-' + INSTANT_DAY), | ||
"HH:mm:ss," + (INSTANT_HOUR + ':' + INSTANT_MINUTE + ':' + INSTANT_SECOND), | ||
"HH:mm:ss.SSS," + (INSTANT_HOUR + ':' + INSTANT_MINUTE + ':' + INSTANT_SECOND + '.' + INSTANT_FRACTION) | ||
}) | ||
void common_date_patterns_should_work(final String instantPattern, final String formattedInstant) { | ||
|
||
// Create a `StatusLogger` configuration | ||
final Properties statusLoggerConfigProperties = new Properties(); | ||
statusLoggerConfigProperties.put(StatusLogger.STATUS_DATE_FORMAT, instantPattern); | ||
statusLoggerConfigProperties.put(StatusLogger.STATUS_DATE_FORMAT_ZONE, "UTC"); | ||
final StatusLogger.Config statusLoggerConfig = new StatusLogger.Config(statusLoggerConfigProperties); | ||
|
||
// Create a `StatusConsoleListener` recording `StatusData` | ||
final StatusConsoleListener statusConsoleListener = mock(StatusConsoleListener.class); | ||
when(statusConsoleListener.getStatusLevel()).thenReturn(Level.ALL); | ||
final List<StatusData> loggedStatusData = new ArrayList<>(); | ||
doAnswer((Answer<Void>) invocation -> { | ||
final StatusData statusData = invocation.getArgument(0, StatusData.class); | ||
loggedStatusData.add(statusData); | ||
return null; | ||
}) | ||
.when(statusConsoleListener) | ||
.log(Mockito.any()); | ||
|
||
// Create the `StatusLogger` | ||
final StatusLogger logger = new StatusLogger( | ||
StatusLoggerDateTest.class.getSimpleName(), | ||
ParameterizedNoReferenceMessageFactory.INSTANCE, | ||
statusLoggerConfig, | ||
CLOCK, | ||
statusConsoleListener); | ||
|
||
// Log a message | ||
final String message = "test message"; | ||
final Level level = Level.ERROR; | ||
final Throwable throwable = new RuntimeException("test failure"); | ||
logger.log(level, message, throwable); | ||
|
||
// Verify the logging | ||
assertThat(loggedStatusData).hasSize(1); | ||
final StatusData statusData = loggedStatusData.get(0); | ||
assertThat(statusData.getLevel()).isEqualTo(level); | ||
assertThat(statusData.getThrowable()).isSameAs(throwable); | ||
assertThat(statusData.getFormattedStatus()) | ||
.matches("(?s)^" + formattedInstant + " .+ " + level + ' ' + message + ".*" + throwable.getMessage() | ||
+ ".*"); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...i-test/src/test/java/org/apache/logging/log4j/status/StatusLoggerFailingListenerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.status; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.parallel.ResourceLock; | ||
import uk.org.webcompere.systemstubs.SystemStubs; | ||
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; | ||
|
||
@ExtendWith(SystemStubsExtension.class) | ||
@ResourceLock("log4j2.StatusLogger") | ||
class StatusLoggerFailingListenerTest { | ||
|
||
public static final StatusLogger STATUS_LOGGER = StatusLogger.getLogger(); | ||
|
||
private StatusListener listener; | ||
|
||
@BeforeEach | ||
void createAndRegisterListener() { | ||
listener = mock(StatusListener.class); | ||
STATUS_LOGGER.registerListener(listener); | ||
} | ||
|
||
@AfterEach | ||
void unregisterListener() { | ||
STATUS_LOGGER.removeListener(listener); | ||
} | ||
|
||
@Test | ||
void logging_with_failing_listener_should_not_cause_stack_overflow() throws Exception { | ||
|
||
// Set up a failing listener on `log(StatusData)` | ||
when(listener.getStatusLevel()).thenReturn(Level.ALL); | ||
final Exception listenerFailure = new RuntimeException("test failure " + Math.random()); | ||
doThrow(listenerFailure).when(listener).log(any()); | ||
|
||
// Log something and verify exception dump | ||
final String stderr = SystemStubs.tapSystemErr(() -> STATUS_LOGGER.error("foo")); | ||
final String listenerFailureClassName = listenerFailure.getClass().getCanonicalName(); | ||
assertThat(stderr).contains(listenerFailureClassName + ": " + listenerFailure.getMessage()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.