Skip to content
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

feat: setup JPMS module #133

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.api;

import com.djaytan.bukkit.slf4j.internal.BukkitLoggerAdapter;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;

Expand All @@ -38,6 +41,11 @@ public final class BukkitLoggerFactory implements ILoggerFactory {

private static java.util.logging.Logger staticLogger;

@Internal
public BukkitLoggerFactory() {
// Bukkit logger must be provided by the library consumer
}

/**
* Provides the Bukkit logger to be returned by any subsequent call to {@link
* org.slf4j.LoggerFactory#getLogger(String)}.
Expand All @@ -55,11 +63,8 @@ public static void provideBukkitLogger(@NotNull java.util.logging.Logger jdk14Lo
staticLogger = jdk14Logger;
}

/**
* Resets the Bukkit logger.
*
* <p>For testing purposes only.
*/
/** Resets the Bukkit logger. */
@TestOnly
static void resetBukkitLogger() {
staticLogger = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.internal;

import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -33,11 +33,11 @@
* This class is responsible for adapting the Bukkit logger instance against the SLF4J {@link
* org.slf4j.Logger} interface.
*/
final class BukkitLoggerAdapter extends LegacyAbstractLogger {
public final class BukkitLoggerAdapter extends LegacyAbstractLogger {

private final transient Logger logger;

BukkitLoggerAdapter(@NotNull Logger logger, @NotNull String name) {
public BukkitLoggerAdapter(@NotNull Logger logger, @NotNull String name) {
super.name = name;
this.logger = logger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.internal;

import com.djaytan.bukkit.slf4j.api.BukkitLoggerFactory;
import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.helpers.BasicMDCAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.internal;

import java.util.ResourceBundle;
import java.util.logging.LogRecord;
Expand Down Expand Up @@ -93,8 +93,8 @@ final class JulLogRecordFactory {
* instead since that's the official and supported way for answering our specific need here. If we
* want to improve the implementation, it seems to be the way to go. The JUL implementation has
* gone into this direction, for example, since a recent version of JDK higher than 8 (see the
* {@link LogRecord#inferCaller()} method for details). Furthermore, it will make the code more
* testable (that's not fully the case with the current implementation because of <code>
* {@link LogRecord}{@code #inferCaller()} method for details). Furthermore, it will make the code
* more testable (that's not fully the case with the current implementation because of <code>
* new Throwable()</code> call).
*
* @param adapterOrSubstituteCallerFqcn The adapter or substitute SLF4J logger fully qualified
Expand All @@ -104,7 +104,7 @@ final class JulLogRecordFactory {
private static @Nullable CallerLocation inferCallerLocation(
@NotNull String adapterOrSubstituteCallerFqcn) {
// The first element is the top-most call on the execution stack i.e. always the following line:
// com.djaytan.bukkit.slf4j/com.djaytan.bukkit.slf4j.JulLogRecordFactory.inferCallerLocation(JulLogRecordFactory.java:<line_number>)
// com.djaytan.bukkit.slf4j/com.djaytan.bukkit.slf4j.internal.JulLogRecordFactory.inferCallerLocation(JulLogRecordFactory.java:<line_number>)
StackTraceElement[] stackTraceElements = new Throwable().getStackTrace();

// First, search for a method in a logger implementation class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.internal;

import java.util.Arrays;
import java.util.Objects;
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* The MIT License
* Copyright © 2023 Loïc DUBOIS-TERMOZ (alias Djaytan)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import com.djaytan.bukkit.slf4j.internal.BukkitLoggerServiceProvider;
import org.slf4j.spi.SLF4JServiceProvider;

module bukkit.slf4j {
requires java.logging;
requires org.jetbrains.annotations;
requires org.slf4j;

exports com.djaytan.bukkit.slf4j.api;

provides SLF4JServiceProvider with
BukkitLoggerServiceProvider;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.djaytan.bukkit.slf4j.BukkitLoggerServiceProvider
com.djaytan.bukkit.slf4j.internal.BukkitLoggerServiceProvider
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.api;

import static com.djaytan.bukkit.slf4j.TestHelper.executeLogging;
import static com.djaytan.bukkit.slf4j.TestHelper.testLoggingExecutionOutputExpectedMessage;
import static com.djaytan.bukkit.slf4j.api.TestHelper.executeLogging;
import static com.djaytan.bukkit.slf4j.api.TestHelper.testLoggingExecutionOutputExpectedMessage;
import static org.assertj.core.api.Assertions.assertThat;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -89,11 +89,13 @@ void consumer_can_log_lots_of_arguments() {
}

@Test
@SuppressWarnings({"java:S3457", "LoggingPlaceholderCountMatchesArgumentCount"})
void exceeding_arguments_shall_be_ignored() {
testLoggingExecutionOutputExpectedMessage(slf4jLogger -> slf4jLogger.info("{}", 14, 15), "14");
}

@Test
@SuppressWarnings({"java:S3457", "LoggingPlaceholderCountMatchesArgumentCount"})
void templates_must_remain_for_missing_argument() {
testLoggingExecutionOutputExpectedMessage(
slf4jLogger -> slf4jLogger.info("{}, {}", 14), "14, {}");
Expand All @@ -118,6 +120,7 @@ void last_argument_is_treated_specially_if_it_is_a_throwable() {
}

@Test
@SuppressWarnings({"java:S3457", "LoggingPlaceholderCountMatchesArgumentCount"})
void last_argument_is_treated_specially_if_it_is_a_throwable_even_with_missing_argument() {
// Arrange
var throwable = new IllegalStateException("test invalid state");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.api;

import static com.djaytan.bukkit.slf4j.TestHelper.SAMPLE_LOGGER_NAME;
import static com.djaytan.bukkit.slf4j.TestHelper.executeLogging;
import static com.djaytan.bukkit.slf4j.TestHelper.flushHandlers;
import static com.djaytan.bukkit.slf4j.TestHelper.getJulStreamLogger;
import static com.djaytan.bukkit.slf4j.TestHelper.setJulLoggerLevel;
import static com.djaytan.bukkit.slf4j.api.TestHelper.SAMPLE_LOGGER_NAME;
import static com.djaytan.bukkit.slf4j.api.TestHelper.executeLogging;
import static com.djaytan.bukkit.slf4j.api.TestHelper.flushHandlers;
import static com.djaytan.bukkit.slf4j.api.TestHelper.getJulStreamLogger;
import static com.djaytan.bukkit.slf4j.api.TestHelper.setJulLoggerLevel;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.params.provider.Arguments.arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.api;

import static org.assertj.core.api.Assertions.assertThat;

import com.djaytan.bukkit.slf4j.internal.BukkitLoggerServiceProvider;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.djaytan.bukkit.slf4j;
package com.djaytan.bukkit.slf4j.internal;

import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.slf4j.event.Level;

final class Slf4jLogRecordTest {

@Test
void equalsAndHashcodeVerification() {
EqualsVerifier.forClass(Slf4jLogRecord.class).verify();
EqualsVerifier.forClass(Slf4jLogRecord.class)
.withPrefabValues(Level.class, Level.INFO, Level.DEBUG)
.withPrefabValues(
Marker.class, MarkerFactory.getMarker("marker"), MarkerFactory.getMarker("marker2"))
.verify();
}

@Test
Expand Down