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

Use java logger when loader logger api isn't available #159

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ legacy-fabric-gamerule-api-v1.version = 1.0.0
legacy-fabric-item-groups-v1.version = 2.0.0
legacy-fabric-keybindings-api-v1.version = 1.0.1
legacy-fabric-lifecycle-events-v1.version = 1.0.1
legacy-fabric-logger-api-v1.version = 1.0.4
legacy-fabric-logger-api-v1.version = 1.1.0
legacy-fabric-networking-api-v1.version = 2.0.1
legacy-fabric-permissions-api-v1.version = 1.0.1
legacy-fabric-registry-sync-api-v1.version = 2.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;
import net.legacyfabric.fabric.api.util.Location;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class CommandManagerImpl implements CommandManager {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Command Manager");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Command Manager");
private final Object lock = new Object();
private final SimpleDispatcher dispatcher;
private final List<CommandMapping> mappings = Lists.newArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;
import net.legacyfabric.fabric.api.util.Location;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class CommandManagerImpl implements CommandManager {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Command Manager");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Command Manager");
private final Object lock = new Object();
private final SimpleDispatcher dispatcher;
private final List<CommandMapping> mappings = Lists.newArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;
import net.legacyfabric.fabric.api.util.Location;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class CommandManagerImpl implements CommandManager {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Command Manager");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Command Manager");
private final Object lock = new Object();
private final SimpleDispatcher dispatcher;
private final List<CommandMapping> mappings = Lists.newArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class EntityEventsTest implements ModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "EntityEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "EntityEvents");

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class EntityEventsTest implements ModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "EntityEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "EntityEvents");

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import net.legacyfabric.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.legacyfabric.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class EntityEventsTest implements ModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "EntityEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "EntityEvents");

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import net.legacyfabric.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class ClientLifecycleEventsTest implements ClientModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "ClientLifecycleEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "ClientLifecycleEvents");

@Override
public void onInitializeClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import net.legacyfabric.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class ClientLifecycleEventsTest implements ClientModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "ClientLifecycleEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "ClientLifecycleEvents");

@Override
public void onInitializeClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import net.legacyfabric.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class ClientLifecycleEventsTest implements ClientModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "ClientLifecycleEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "ClientLifecycleEvents");

@Override
public void onInitializeClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import net.legacyfabric.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.legacyfabric.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class ServerLifecycleEventsTest implements ModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "ServerLifecycleEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "ServerLifecycleEvents");

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import net.legacyfabric.fabric.api.client.event.lifecycle.v1.ClientChunkEvents;
import net.legacyfabric.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public class ClientLifecycleEventsCommonTest implements ClientModInitializer {
private static final Logger LOGGER = Logger.get(LoggerImpl.API, "Test", "ClientLifecycleEvents");
private static final Logger LOGGER = Logger.get(LoggerHelper.API, "Test", "ClientLifecycleEvents");

@Override
public void onInitializeClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package net.legacyfabric.fabric.api.logger.v1;

import net.legacyfabric.fabric.impl.logger.LoggerImpl;
import net.legacyfabric.fabric.impl.logger.LoggerHelper;

public interface Logger {
/**
Expand All @@ -32,7 +32,7 @@ public interface Logger {
* @return a Logger implementation
*/
static Logger get(String context, String... subs) {
return new LoggerImpl(context, subs);
return LoggerHelper.getLogger(context, subs);
}

void info(String format);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2020 - 2024 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* 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
*
* 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 net.legacyfabric.fabric.impl.logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class JavaLoggerImpl implements net.legacyfabric.fabric.api.logger.v1.Logger {
private final Logger instance;

protected JavaLoggerImpl(String context, String... subs) {
List<String> parts = new ArrayList<>();
parts.add(context);
parts.addAll(Arrays.asList(subs));

instance = Logger.getLogger(parts.stream().collect(Collectors.joining("/")));
}

@Override
public void info(String format) {
instance.log(Level.INFO, format);
}

@Override
public void info(String format, Object... args) {
instance.log(Level.INFO, format, args);
}

@Override
public void info(String format, Throwable exc) {
instance.log(Level.INFO, format, exc);
}

@Override
public void error(String format) {
instance.log(Level.SEVERE, format);
}

@Override
public void error(String format, Object... args) {
instance.log(Level.SEVERE, format, args);
}

@Override
public void error(String format, Throwable exc) {
instance.log(Level.SEVERE, format, exc);
}

@Override
public void warn(String format) {
instance.log(Level.WARNING, format);
}

@Override
public void warn(String format, Object... args) {
instance.log(Level.WARNING, format, args);
}

@Override
public void warn(String format, Throwable exc) {
instance.log(Level.WARNING, format, exc);
}

@Override
public void debug(String format) {
instance.log(Level.FINE, format);
}

@Override
public void debug(String format, Object... args) {
instance.log(Level.FINE, format, args);
}

@Override
public void debug(String format, Throwable exc) {
instance.log(Level.FINE, format, exc);
}

@Override
public void trace(String format) {
instance.log(Level.FINEST, format);
}

@Override
public void trace(String format, Object... args) {
instance.log(Level.FINEST, format, args);
}

@Override
public void trace(String format, Throwable exc) {
instance.log(Level.FINEST, format, exc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2020 - 2024 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* 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
*
* 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 net.legacyfabric.fabric.impl.logger;

import java.util.function.BiFunction;

import net.legacyfabric.fabric.api.logger.v1.Logger;
import net.legacyfabric.fabric.impl.logger.utils.TriState;

public class LoggerHelper {
public static final String API = "LegacyFabricAPI";

private static TriState exists = TriState.DEFAULT;
private static final BiFunction<String, String[], Logger> javaLogger = JavaLoggerImpl::new;
private static final BiFunction<String, String[], Logger> nativeLogger = NativeLoggerImpl::new;

public static Logger getLogger(String context, String... subs) {
if (exists == TriState.DEFAULT) {
try {
Class.forName("net.fabricmc.loader.impl.util.log.LogCategory");
Class.forName("net.fabricmc.loader.impl.util.log.Log");
exists = TriState.TRUE;
} catch (ClassNotFoundException ignored) {
exists = TriState.FALSE;
}
}

return exists.get() ? nativeLogger.apply(context, subs) : javaLogger.apply(context, subs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,15 @@

package net.legacyfabric.fabric.impl.logger;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.fabricmc.loader.impl.util.log.Log;
import net.fabricmc.loader.impl.util.log.LogCategory;

import net.legacyfabric.fabric.api.logger.v1.Logger;

public class LoggerImpl implements Logger {
public static final String API = "LegacyFabricAPI";
private LogCategory category;

public LoggerImpl(String context, String... subs) {
try { // Loader 0.14.3+
tryCreatingLogger(context, subs);
} catch (NoSuchMethodError e) { // Loader 0.13+
List<String> parts = new ArrayList<>();
parts.add(context);
Collections.addAll(parts, subs);

try {
this.category = LogCategory.class.getDeclaredConstructor(String[].class).newInstance(parts.toArray(new String[0]));
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
}
public class NativeLoggerImpl implements Logger {
private final LogCategory category;

private void tryCreatingLogger(String context, String... subs) throws NoSuchMethodError {
public NativeLoggerImpl(String context, String... subs) {
this.category = LogCategory.createCustom(context, subs);
}

Expand Down
Loading
Loading