package vg.civcraft.mc.civmodcore.util;

import com.google.common.base.Strings;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.bukkit.plugin.java.PluginClassLoader;

public final class CivLogger extends Logger {

	private final String prefix;

	private CivLogger(final Logger logger, final String prefix) {
		super(logger.getName(), logger.getResourceBundleName());
		setParent(logger);
		this.prefix = prefix;
	}

	@Override
	public void log(final LogRecord record) {
		if (!Strings.isNullOrEmpty(this.prefix)) {
			record.setMessage("[" + this.prefix + "] " + record.getMessage());
		}
		super.log(record);
	}

	/**
	 * Creates a logger based on a given class. If the given class was loaded by a plugin, it will piggy back off that
	 * plugin's logger.
	 *
	 * @param clazz The class to base the logger on.
	 * @return Returns a new civ logger.
	 */
	public static CivLogger getLogger(final Class<?> clazz) {
		if (clazz == null) {
			return new CivLogger(Logger.getLogger(CivLogger.class.getSimpleName()), null);
		}
		final ClassLoader classLoader = clazz.getClassLoader();
		if (!(classLoader instanceof PluginClassLoader)) {
			return new CivLogger(Logger.getLogger(CivLogger.class.getSimpleName()), clazz.getSimpleName());
		}
		final var plugin = ((PluginClassLoader) classLoader).getPlugin();
		return new CivLogger(plugin.getLogger(), clazz.getSimpleName());
	}

}

package vg.civcraft.mc.civmodcore.util;

import com.google.common.base.Strings;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.bukkit.plugin.java.PluginClassLoader;

public final class CivLogger extends Logger {

	private final String prefix;

	private CivLogger(final Logger logger, final String prefix) {
		super(logger.getName(), logger.getResourceBundleName());
		setParent(logger);
		this.prefix = prefix;
	}

	@Override
	public void log(final LogRecord record) {
		if (!Strings.isNullOrEmpty(this.prefix)) {
			record.setMessage("[" + this.prefix + "] " + record.getMessage());
		}
		super.log(record);
	}

	/**
	 * Creates a logger based on a given class. If the given class was loaded by a plugin, it will piggy back off that
	 * plugin's logger.
	 *
	 * @param clazz The class to base the logger on.
	 * @return Returns a new civ logger.
	 */
	public static CivLogger getLogger(@Nonnull final Class<?> clazz) {
		final ClassLoader classLoader = clazz.getClassLoader();
		if (classLoader instanceof PluginClassLoader) {
			final var plugin = ((PluginClassLoader) classLoader).getPlugin();
			if (plugin != null) {
				return new CivLogger(plugin.getLogger(), clazz.getSimpleName());
			}
			// Plugin has been constructed but not initialised yet
			final var loaderLoggerField = FieldUtils.getDeclaredField(PluginClassLoader.class, "logger", true);
			try {
				final var loaderLogger = (Logger) loaderLoggerField.get(classLoader);
				return new CivLogger(loaderLogger, clazz.getSimpleName());
			}
			catch (final IllegalAccessException ignored) {}
		}
		return new CivLogger(Logger.getLogger(CivLogger.class.getSimpleName()), clazz.getSimpleName());
	}

}

Fix CivLogger issue if the logger detects a plugin but the plugin is not enabled yet