Skip to content

Commit

Permalink
SLF4J Logger rewrite part2 (#492)
Browse files Browse the repository at this point in the history
SLF4J stage 2 (final).
- Added SLF4J Logger (fallback) and JDA internal Factory.
- Added SLF4J to the README.
- Replaced logs using variables with lazily built logs using SLF4J variable injection.
- Combined multi-logs into a single log message (exceptions!)

Also did:
- Added try/catch in onTextMessage to print full JSON package when an error occurs outside of handling
- Added handling for CHANNEL_UPDATE for a not existing category
  • Loading branch information
kantenkugel committed Nov 13, 2017
1 parent 72d5a74 commit e9399b7
Show file tree
Hide file tree
Showing 50 changed files with 896 additions and 621 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -184,6 +184,22 @@ repositories {

The builds are distributed using JCenter through Bintray [JDA JCenter Bintray](https://bintray.com/dv8fromtheworld/maven/JDA/)

### Logging Framework - SLF4J
JDA is using [SLF4J](https://www.slf4j.org/) to log its messages.

That means you should add some SLF4J implementation to your build path in addition to JDA.
If no implementation is found, following message will be printed to the console on startup:
```
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
```

JDA currently provides a fallback Logger in case that no SLF4J implementation is present.
We strongly recommend to use one though, as that can improve speed and allows you to customize the Logger as well as log to files

The most popular implementations are [Log4j 2](https://logging.apache.org/log4j/2.x/) and [Logback](https://logback.qos.ch/)

## Documentation
Docs can be found on the [Jenkins](http://home.dv8tion.net:8080/) or directly [here](http://home.dv8tion.net:8080/job/JDA/javadoc/)
<br>A simple Wiki can also be found in this repository's [Wiki section](https://github.com/DV8FromTheWorld/JDA/wiki)
Expand Down Expand Up @@ -286,6 +302,10 @@ All dependencies are managed automatically by Gradle.
* Version: **3.0.3**
* [BitBucket](https://bitbucket.org/trove4j/trove)
* [JCenter Repository](https://bintray.com/bintray/jcenter/net.sf.trove4j%3Atrove4j/view)
* slf4j-api
* Version: **1.7.25**
* [Website](https://www.slf4j.org/)
* [JCenter Repository](https://bintray.com/bintray/jcenter/org.slf4j%3Aslf4j-api/view)

## Related Projects

Expand Down
Expand Up @@ -52,7 +52,7 @@ protected Long handleInternally(JSONObject content)
if (channel == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a CALL_CREATE for a Group/PrivateChannel that is not yet cached. JSON: " + content);
EventCache.LOG.debug("Received a CALL_CREATE for a Group/PrivateChannel that is not yet cached. JSON: {}", content);
return null;
}

Expand All @@ -64,7 +64,7 @@ protected Long handleInternally(JSONObject content)
{
GroupImpl group = (GroupImpl) channel;
if (group.getCurrentCall() != null)
WebSocketClient.LOG.fatal("Received a CALL_CREATE for a Group that already has an active call cached! JSON: " + content);
WebSocketClient.LOG.error("Received a CALL_CREATE for a Group that already has an active call cached! JSON: {}", content);
group.setCurrentCall(call);
group.getUserMap().forEachEntry((userId, user) ->
{
Expand All @@ -88,7 +88,7 @@ protected Long handleInternally(JSONObject content)
{
PrivateChannelImpl priv = (PrivateChannelImpl) channel;
if (priv.getCurrentCall() != null)
WebSocketClient.LOG.fatal("Received a CALL_CREATE for a PrivateChannel that already has an active call cached! JSON: " + content);
WebSocketClient.LOG.error("Received a CALL_CREATE for a PrivateChannel that already has an active call cached! JSON: {}", content);
priv.setCurrentCall(call);
callUsers.put(priv.getUser().getIdLong(), new CallUserImpl(call, priv.getUser()));
callUsers.put(api.getSelfUser().getIdLong(), new CallUserImpl(call, api.getSelfUser()));
Expand Down
Expand Up @@ -45,15 +45,15 @@ protected Long handleInternally(JSONObject content)
if (channel == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received CALL_DELETE for a Group/PrivateChannel that is not yet cached. JSON: " + content);
EventCache.LOG.debug("Received CALL_DELETE for a Group/PrivateChannel that is not yet cached. JSON: {}", content);
return null;
}

CallImpl call = (CallImpl) channel.getCurrentCall();
if (call == null)
{
api.getEventCache().cache(EventCache.Type.CALL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a CALL_DELETE for a Call that is not yet cached. JSON: " + content);
EventCache.LOG.debug("Received a CALL_DELETE for a Call that is not yet cached. JSON: {}", content);
return null;
}

Expand Down
Expand Up @@ -53,15 +53,15 @@ protected Long handleInternally(JSONObject content)
if (channel == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a CALL_UPDATE for a Group/PrivateChannel that has not yet been cached. JSON: " + content);
EventCache.LOG.debug("Received a CALL_UPDATE for a Group/PrivateChannel that has not yet been cached. JSON: {}", content);
return null;
}

CallImpl call = (CallImpl) channel.getCurrentCall();
if (call == null)
{
api.getEventCache().cache(EventCache.Type.CALL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a CALL_UPDATE for a Call that has not yet been cached. JSON: " + content);
EventCache.LOG.debug("Received a CALL_UPDATE for a Call that has not yet been cached. JSON: {}", content);
return null;
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ protected Long handleInternally(JSONObject content)
if (group == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, groupId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a CHANNEL_RECIPIENT_ADD for a group that is not yet cached! JSON: " + content);
EventCache.LOG.debug("Received a CHANNEL_RECIPIENT_ADD for a group that is not yet cached! JSON: {}", content);
return null;
}

Expand Down
Expand Up @@ -42,15 +42,15 @@ protected Long handleInternally(JSONObject content)
if (group == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, groupId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a CHANNEL_RECIPIENT_REMOVE for a group that is not yet cached! JSON: " + content);
EventCache.LOG.debug("Received a CHANNEL_RECIPIENT_REMOVE for a group that is not yet cached! JSON: {}", content);
return null;
}

User user = group.getUserMap().remove(userId);
if (user == null)
{
api.getEventCache().cache(EventCache.Type.USER, userId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a CHANNEL_RECIPIENT_REMOVE for a user that is not yet cached in the group! JSON: " + content);
EventCache.LOG.debug("Received a CHANNEL_RECIPIENT_REMOVE for a user that is not yet cached in the group! JSON: {}", content);
return null;
}

Expand Down
Expand Up @@ -41,7 +41,7 @@ protected Long handleInternally(JSONObject content)
Relationship relationship = api.getEntityBuilder().createRelationship(content);
if (relationship == null)
{
WebSocketClient.LOG.warn("Received a RELATIONSHIP_ADD with an unknown type! JSON: " + content);
WebSocketClient.LOG.warn("Received a RELATIONSHIP_ADD with an unknown type! JSON: {}", content);
return null;
}
switch (relationship.getType())
Expand Down Expand Up @@ -71,7 +71,7 @@ protected Long handleInternally(JSONObject content)
relationship));
break;
default:
WebSocketClient.LOG.warn("Received a RELATIONSHIP_ADD with an unknown type! JSON: " + content);
WebSocketClient.LOG.warn("Received a RELATIONSHIP_ADD with an unknown type! JSON: {}", content);
return null;
}
api.getEventCache().playbackCache(EventCache.Type.RELATIONSHIP, relationship.getUser().getIdLong());
Expand Down
Expand Up @@ -58,7 +58,7 @@ protected Long handleInternally(JSONObject content)
if (relationship == null)
{
api.getEventCache().cache(EventCache.Type.RELATIONSHIP, userId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a RELATIONSHIP_REMOVE for a relationship that was not yet cached! JSON: " + content);
EventCache.LOG.debug("Received a RELATIONSHIP_REMOVE for a relationship that was not yet cached! JSON: {}", content);
return null;
}
((JDAClientImpl) api.asClient()).getRelationshipMap().remove(userId);
Expand Down Expand Up @@ -136,7 +136,7 @@ protected Long handleInternally(JSONObject content)
relationship));
break;
default:
WebSocketClient.LOG.warn("Received a RELATIONSHIP_REMOVE with an unknown RelationshipType! JSON: " + content);
WebSocketClient.LOG.warn("Received a RELATIONSHIP_REMOVE with an unknown RelationshipType! JSON: {}", content);
return null;
}
api.getEventCache().clear(EventCache.Type.RELATIONSHIP, userId);
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/net/dv8tion/jda/core/audio/AudioConnection.java
Expand Up @@ -32,9 +32,10 @@
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.events.ExceptionEvent;
import net.dv8tion.jda.core.managers.impl.AudioManagerImpl;
import net.dv8tion.jda.core.utils.SimpleLog;
import net.dv8tion.jda.core.utils.JDALogger;
import net.dv8tion.jda.core.utils.tuple.Pair;
import org.json.JSONObject;
import org.slf4j.Logger;
import tomp2p.opuswrapper.Opus;

import java.net.DatagramPacket;
Expand All @@ -54,7 +55,7 @@

public class AudioConnection
{
public static final SimpleLog LOG = SimpleLog.getLog(AudioConnection.class);
public static final Logger LOG = JDALogger.getLog(AudioConnection.class);
public static final int OPUS_SAMPLE_RATE = 48000; //(Hz) We want to use the highest of qualities! All the bandwidth!
public static final int OPUS_FRAME_SIZE = 960; //An opus frame size of 960 at 48000hz represents 20 milliseconds of audio.
public static final int OPUS_FRAME_TIME_AMOUNT = 20;//This is 20 milliseconds. We are only dealing with 20ms opus packets.
Expand Down Expand Up @@ -116,7 +117,7 @@ public void ready()
}
catch (InterruptedException e)
{
LOG.fatal(e);
LOG.error("AudioConnection ready thread got interrupted while sleeping", e);
Thread.currentThread().interrupt();
}
}
Expand All @@ -134,7 +135,7 @@ public void ready()
});
readyThread.setUncaughtExceptionHandler((thread, throwable) ->
{
LOG.fatal(throwable);
LOG.error("Uncaught exception in Audio ready-thread", throwable);
JDAImpl api = (JDAImpl) getJDA();
api.getEventManager().handle(new ExceptionEvent(api, throwable, true));
});
Expand Down Expand Up @@ -207,8 +208,8 @@ protected void updateUserSSRC(int ssrc, long userId)
{
//Different User already existed with this ssrc. What should we do? Just replace? Probably should nuke the old opusDecoder.
//Log for now and see if any user report the error.
LOG.fatal("Yeah.. So.. JDA received a UserSSRC update for an ssrc that already had a User set. Inform DV8FromTheWorld.\n" +
"ChannelId: " + channel.getId() + " SSRC: " + ssrc + " oldId: " + previousId + " newId: " + userId);
LOG.error("Yeah.. So.. JDA received a UserSSRC update for an ssrc that already had a User set. Inform DV8FromTheWorld.\nChannelId: {} SSRC: {} oldId: {} newId: {}",
channel.getId(), ssrc, previousId, userId);
}
}
else
Expand Down Expand Up @@ -318,7 +319,7 @@ private synchronized void setupReceiveThread()
}
catch (SocketException e)
{
LOG.fatal(e);
LOG.error("Couldn't set SO_TIMEOUT for UDP socket", e);
}
while (!udpSocket.isClosed() && !Thread.currentThread().isInterrupted())
{
Expand Down Expand Up @@ -415,13 +416,13 @@ else if (couldReceive)
}
catch (Exception e)
{
LOG.fatal(e);
LOG.error("There was some random exception while waiting for udp packets", e);
}
}
});
receiveThread.setUncaughtExceptionHandler((thread, throwable) ->
{
LOG.fatal(throwable);
LOG.error("There was some uncaught exception in the audio receive thread", throwable);
JDAImpl api = (JDAImpl) getJDA();
api.getEventManager().handle(new ExceptionEvent(api, throwable, true));
});
Expand All @@ -446,7 +447,7 @@ private synchronized void setupCombinedExecutor()
t.setDaemon(true);
t.setUncaughtExceptionHandler((thread, throwable) ->
{
LOG.fatal(throwable);
LOG.error("I have no idea how, but there was an uncaught exception in the combinedAudioExecutor", throwable);
JDAImpl api = (JDAImpl) getJDA();
api.getEventManager().handle(new ExceptionEvent(api, throwable, true));
});
Expand Down Expand Up @@ -515,7 +516,7 @@ else if (sample < Short.MIN_VALUE)
}
catch (Exception e)
{
LOG.fatal(e);
LOG.error("There was some unexpected exception in the combinedAudioExecutor!", e);
}
}, 0, 20, TimeUnit.MILLISECONDS);
}
Expand Down Expand Up @@ -654,7 +655,7 @@ else if (speaking && changeTalking)
}
catch (Exception e)
{
LOG.fatal(e);
LOG.error("There was an error while getting next audio packet", e);
}

if (nextPacket != null)
Expand All @@ -666,7 +667,7 @@ else if (speaking && changeTalking)
@Override
public void onConnectionError(ConnectionStatus status)
{
LOG.warn("IAudioSendSystem reported a connection error of: " + status);
LOG.warn("IAudioSendSystem reported a connection error of: {}", status);
LOG.warn("Shutting down AudioConnection.");
webSocket.close(status);
}
Expand Down

0 comments on commit e9399b7

Please sign in to comment.