Skip to content

Commit

Permalink
Replace SimpleLogger with FallbackLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed May 4, 2024
1 parent d01ea0b commit 1cc2df3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 449 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.utils.JDALogger;

public class GuildBanHandler extends SocketHandler
{
Expand All @@ -45,7 +44,7 @@ protected Long handleInternally(DataObject content)
if (guild == null)
{
getJDA().getEventCache().cache(EventCache.Type.GUILD, id, responseNumber, allContent, this::handle);
EventCache.LOG.debug("Received Guild Member {} event for a Guild not yet cached.", JDALogger.getLazyString(() -> banned ? "Ban" : "Unban"));
EventCache.LOG.debug("Received Guild Member {} event for a Guild not yet cached.", banned ? "Ban" : "Unban");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import net.dv8tion.jda.internal.entities.MemberImpl;
import net.dv8tion.jda.internal.entities.channel.concrete.PrivateChannelImpl;
import net.dv8tion.jda.internal.requests.WebSocketClient;
import net.dv8tion.jda.internal.utils.JDALogger;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -73,8 +72,7 @@ protected Long handleInternally(DataObject content)

if (emojiId == null && emojiName == null)
{
WebSocketClient.LOG.debug("Received a reaction {} with no name nor id. json: {}",
JDALogger.getLazyString(() -> add ? "add" : "remove"), content);
WebSocketClient.LOG.debug("Received a reaction {} with no name nor id. json: {}", add ? "add" : "remove", content);
return null;
}
final long guildId = content.getUnsignedLong("guild_id", 0);
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/utils/FallbackLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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.dv8tion.jda.internal.utils;

import org.slf4j.Marker;
import org.slf4j.event.Level;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.LegacyAbstractLogger;
import org.slf4j.helpers.MessageFormatter;

import java.time.LocalDateTime;

class FallbackLogger extends LegacyAbstractLogger
{
private final String name;

public FallbackLogger(String name)
{
this.name = name;
}

@Override
protected String getFullyQualifiedCallerName()
{
return null;
}

@Override
protected void handleNormalizedLoggingCall(Level level, Marker marker, String messagePattern, Object[] arguments, Throwable throwable)
{
LocalDateTime now = LocalDateTime.now();
FormattingTuple result = MessageFormatter.arrayFormat(messagePattern, arguments);
System.err.printf("%1$tF %1$tT [%2$s] [%3$s] %4$s%n", now, name, level, result.getMessage());
if (throwable != null)
throwable.printStackTrace(System.err);
}

@Override
public boolean isTraceEnabled()
{
return false;
}

@Override
public boolean isDebugEnabled()
{
return false;
}

@Override
public boolean isInfoEnabled()
{
return true;
}

@Override
public boolean isWarnEnabled()
{
return true;
}

@Override
public boolean isErrorEnabled()
{
return true;
}
}
12 changes: 6 additions & 6 deletions src/main/java/net/dv8tion/jda/internal/utils/JDALogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* This class serves as a LoggerFactory for JDA's internals.
* <br>It will either return a Logger from a SLF4J implementation via {@link org.slf4j.LoggerFactory} if present,
* or an instance of a custom {@link SimpleLogger} (From slf4j-simple).
* or an instance of a custom {@link FallbackLogger}.
* <p>
* It also has the utility method {@link #getLazyString(LazyEvaluation)} which is used to lazily construct Strings for Logging.
*/
Expand Down Expand Up @@ -83,7 +83,7 @@ private JDALogger() {}
* Will get the {@link org.slf4j.Logger} with the given log-name
* or create and cache a fallback logger if there is no SLF4J implementation present.
* <p>
* The fallback logger will be an instance of a slightly modified version of SLF4Js SimpleLogger.
* The fallback logger uses a constant logging configuration and prints directly to {@link System#err}.
*
* @param name
* The name of the Logger
Expand All @@ -96,15 +96,15 @@ public static Logger getLog(String name)
{
if (SLF4J_ENABLED)
return LoggerFactory.getLogger(name);
return LOGS.computeIfAbsent(name, SimpleLogger::new);
return LOGS.computeIfAbsent(name, FallbackLogger::new);
}
}

/**
* Will get the {@link org.slf4j.Logger} for the given Class
* or create and cache a fallback logger if there is no SLF4J implementation present.
* <p>
* The fallback logger will be an instance of a slightly modified version of SLF4Js SimpleLogger.
* The fallback logger uses a constant logging configuration and prints directly to {@link System#err}.
*
* @param clazz
* The class used for the Logger name
Expand All @@ -117,7 +117,7 @@ public static Logger getLog(Class<?> clazz)
{
if (SLF4J_ENABLED)
return LoggerFactory.getLogger(clazz);
return LOGS.computeIfAbsent(clazz.getName(), (n) -> new SimpleLogger(clazz.getSimpleName()));
return LOGS.computeIfAbsent(clazz.getName(), (n) -> new FallbackLogger(clazz.getSimpleName()));
}
}

Expand All @@ -144,7 +144,7 @@ public String toString()
{
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
return "Error while evaluating lazy String... " + sw.toString();
return "Error while evaluating lazy String... " + sw;
}
}
};
Expand Down
Loading

0 comments on commit 1cc2df3

Please sign in to comment.