-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added validations and waiting mechanism * Added tests and replaced IllegalArgumentException with LogCaptorException * Added another test * Added another test * Attempt to resolve security issue * Attempt to resolve security hotspot * Applied sonar recommendations * Improved performance
- Loading branch information
Showing
6 changed files
with
236 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
27 changes: 27 additions & 0 deletions
27
src/main/java/nl/altindag/log/exception/LogCaptorException.java
This file contains 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,27 @@ | ||
/* | ||
* Copyright 2019 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.log.exception; | ||
|
||
public final class LogCaptorException extends RuntimeException { | ||
|
||
public LogCaptorException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public LogCaptorException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains 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,91 @@ | ||
/* | ||
* Copyright 2019 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.log.util; | ||
|
||
import nl.altindag.log.exception.LogCaptorException; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.helpers.SubstituteLogger; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.IntSupplier; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* <strong>NOTE:</strong> | ||
* Please don't use this class directly as it is part of the internal API. Class name and methods can be changed any time. | ||
* | ||
* @author Hakan Altindag | ||
*/ | ||
public final class LogbackUtils { | ||
|
||
private static final int DEFAULT_POLL_COUNTER_LIMIT = 10; | ||
private static final int DEFAULT_POLL_DELAY_MILLISECONDS = 100; | ||
private static final Pattern IS_NUMBER_PATTERN = Pattern.compile("^\\d+$"); | ||
|
||
private static final IntSupplier POLL_COUNTER_LIMIT = () -> Optional.ofNullable(System.getProperty("logcaptor.poll-counter-limit")) | ||
.map(String::trim) | ||
.filter(value -> !value.isEmpty()) | ||
.filter(value -> IS_NUMBER_PATTERN.matcher(value).matches()) | ||
.map(Integer::parseInt) | ||
.orElse(DEFAULT_POLL_COUNTER_LIMIT); | ||
private static final IntSupplier POLL_DELAY_MILLISECONDS = () -> Optional.ofNullable(System.getProperty("logcaptor.poll-delay-milliseconds")) | ||
.map(String::trim) | ||
.filter(value -> !value.isEmpty()) | ||
.filter(value -> IS_NUMBER_PATTERN.matcher(value).matches()) | ||
.map(Integer::parseInt) | ||
.orElse(DEFAULT_POLL_DELAY_MILLISECONDS); | ||
|
||
private LogbackUtils() {} | ||
|
||
public static ch.qos.logback.classic.Logger getLogger(String loggerName) { | ||
org.slf4j.Logger slf4jLogger = getSlf4jLogger(loggerName); | ||
ValidationUtils.requireLoggerOfType(slf4jLogger, ch.qos.logback.classic.Logger.class); | ||
return (ch.qos.logback.classic.Logger) slf4jLogger; | ||
} | ||
|
||
/** | ||
* Attempts to get the {@link org.slf4j.Logger}. | ||
* It might occur the {@link LoggerFactory} returns a {@link SubstituteLogger}. In that | ||
* case it will retry to get the correct logger within the given poll limit and poll delay. | ||
* SLF4J will provide the {@link SubstituteLogger} temporally when the underlying logger is not ready yet. | ||
* This will most likely happen when running the tests in parallel. | ||
*/ | ||
private static org.slf4j.Logger getSlf4jLogger(String loggerName) { | ||
int retryCounter = 0; | ||
org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(loggerName); | ||
|
||
if (!(slf4jLogger instanceof SubstituteLogger)) { | ||
return slf4jLogger; | ||
} | ||
|
||
int pollCounterLimit = POLL_COUNTER_LIMIT.getAsInt(); | ||
int pollDelayMilliseconds = POLL_DELAY_MILLISECONDS.getAsInt(); | ||
|
||
while (slf4jLogger instanceof SubstituteLogger && retryCounter++ < pollCounterLimit) { | ||
try { | ||
TimeUnit.MILLISECONDS.sleep(pollDelayMilliseconds); | ||
slf4jLogger = LoggerFactory.getLogger(loggerName); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new LogCaptorException(e); | ||
} | ||
} | ||
|
||
return slf4jLogger; | ||
} | ||
|
||
} |
This file contains 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 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
109 changes: 109 additions & 0 deletions
109
src/test/java/nl/altindag/log/util/LogbackUtilsShould.java
This file contains 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,109 @@ | ||
/* | ||
* Copyright 2019 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.log.util; | ||
|
||
import ch.qos.logback.classic.Logger; | ||
import nl.altindag.log.exception.LogCaptorException; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.helpers.SubstituteLogger; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.times; | ||
|
||
class LogbackUtilsShould { | ||
|
||
@Test | ||
void getLoggerEvenWhenItHasInitializedLogger() { | ||
SubstituteLogger substituteLogger = mock(SubstituteLogger.class); | ||
Logger logbackLogger = mock(Logger.class); | ||
|
||
try (MockedStatic<LoggerFactory> mockedStatic = mockStatic(LoggerFactory.class)) { | ||
mockedStatic.when(() -> LoggerFactory.getLogger("magic-logger")) | ||
.thenReturn(substituteLogger) | ||
.thenReturn(logbackLogger); | ||
|
||
Logger logger = LogbackUtils.getLogger("magic-logger"); | ||
assertThat(logger).isEqualTo(logbackLogger); | ||
} | ||
} | ||
|
||
@Test | ||
void failToGetLoggerWhenTheUnderlyingLoggerIsNotInitializedYet() { | ||
SubstituteLogger substituteLogger = mock(SubstituteLogger.class); | ||
System.setProperty("logcaptor.poll-counter-limit", "1"); | ||
|
||
try (MockedStatic<LoggerFactory> mockedStatic = mockStatic(LoggerFactory.class)) { | ||
mockedStatic.when(() -> LoggerFactory.getLogger("magic-logger")) | ||
.thenReturn(substituteLogger); | ||
|
||
assertThatThrownBy(() -> LogbackUtils.getLogger("magic-logger")) | ||
.isInstanceOf(LogCaptorException.class) | ||
.hasMessage("SLF4J Logger implementation should be of the type [ch.qos.logback.classic.Logger] but found [org.slf4j.helpers.SubstituteLogger]."); | ||
} finally { | ||
System.clearProperty("logcaptor.poll-counter-limit"); | ||
} | ||
} | ||
|
||
@Test | ||
void failToGetLoggerWhenTheUnderlyingLoggerIsNotInitializedAndExceedsRetryMechanism() { | ||
System.setProperty("logcaptor.poll-counter-limit", "5"); | ||
System.setProperty("logcaptor.poll-delay-milliseconds", "10"); | ||
|
||
SubstituteLogger substituteLogger = mock(SubstituteLogger.class); | ||
|
||
try (MockedStatic<LoggerFactory> mockedStatic = mockStatic(LoggerFactory.class)) { | ||
mockedStatic.when(() -> LoggerFactory.getLogger("magic-logger")) | ||
.thenReturn(substituteLogger); | ||
|
||
assertThatThrownBy(() -> LogbackUtils.getLogger("magic-logger")) | ||
.isInstanceOf(LogCaptorException.class) | ||
.hasMessage("SLF4J Logger implementation should be of the type [ch.qos.logback.classic.Logger] but found [org.slf4j.helpers.SubstituteLogger]."); | ||
|
||
mockedStatic.verify(() -> LoggerFactory.getLogger("magic-logger"), times(6)); | ||
} finally { | ||
System.clearProperty("logcaptor.poll-counter-limit"); | ||
System.clearProperty("logcaptor.poll-delay-milliseconds"); | ||
} | ||
} | ||
|
||
@Test | ||
void useDefaultRetryMechanismWhenSystemPropertiesAreInvalid() { | ||
System.setProperty("logcaptor.poll-counter-limit", " "); | ||
System.setProperty("logcaptor.poll-delay-milliseconds", " "); | ||
|
||
SubstituteLogger substituteLogger = mock(SubstituteLogger.class); | ||
|
||
try (MockedStatic<LoggerFactory> mockedStatic = mockStatic(LoggerFactory.class)) { | ||
mockedStatic.when(() -> LoggerFactory.getLogger("magic-logger")) | ||
.thenReturn(substituteLogger); | ||
|
||
assertThatThrownBy(() -> LogbackUtils.getLogger("magic-logger")) | ||
.isInstanceOf(LogCaptorException.class) | ||
.hasMessage("SLF4J Logger implementation should be of the type [ch.qos.logback.classic.Logger] but found [org.slf4j.helpers.SubstituteLogger]."); | ||
|
||
mockedStatic.verify(() -> LoggerFactory.getLogger("magic-logger"), times(11)); | ||
} finally { | ||
System.clearProperty("logcaptor.poll-counter-limit"); | ||
System.clearProperty("logcaptor.poll-delay-milliseconds"); | ||
} | ||
} | ||
|
||
} |