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

SLF4J Logger rewrite part2 #492

Merged
merged 11 commits into from Nov 13, 2017
Merged

SLF4J Logger rewrite part2 #492

merged 11 commits into from Nov 13, 2017

Conversation

kantenkugel
Copy link
Collaborator

@kantenkugel kantenkugel commented Oct 1, 2017

Completely removed SimpleLog in favor of a SLF4J Logger.

  • Now using the Logger from slf4j-simple as fallback.
  • new class JDALogger acts as JDA's Logger factory. It either returns the Logger via LoggerFactory if slf4j binding is present or creates a SimpleLogger instance (and cachinges it).
  • Replaced all String concatenations of loggers with the lazy construction using parametrized logger methods and {}
  • Added JDALogger#getLazyString(LAMBDATERM) to lazily construct String from expression as slf4j doesn't support that natively yet

@kantenkugel
Copy link
Collaborator Author

Would suggest waiting with merge until next version bump

@kantenkugel kantenkugel added status: completed has been completed but is not yet released requires testing labels Oct 1, 2017
@kantenkugel kantenkugel added this to the Upcoming Release milestone Oct 1, 2017
@MinnDevelopment MinnDevelopment modified the milestones: Upcoming Release, Release 3.4 Oct 3, 2017
@schnapster
Copy link
Contributor

May I suggest to take advantage of slf4js methods and instead of separately logging stack traces, append them to the message they belong to?

The output should look exactly the same in terms of information content, with the benefit of making life easier on log aggregators which may get confused by the current behaviour of doing two log calls for a single issue (concrete example of when things go wrong: using the logback appender for sentry)

Example:
https://github.com/DV8FromTheWorld/JDA/blob/30174cfa66e1952d1e98b7ddc5eb7803f74c535f/src/main/java/net/dv8tion/jda/core/requests/WebSocketClient.java#L975-L985

Instead do:

        catch (JSONException ex)
        {
            LOG.warn("Got an unexpected Json-parse error. Please redirect following message to the devs:\n\t{}\n\t{} -> {}",
                ex.getMessage(), type, content, ex);
        }
        catch (Exception ex)
        {
            LOG.error("Got an unexpected error. Please redirect following message to the devs:\n\t{} -> {}", type, content, ex);
        }

@Shredder121
Copy link
Contributor

The only issue is that if you use the Throwable method, the format arguments cannot be used.

@kantenkugel
Copy link
Collaborator Author

And that (together with the fact that i want the json data seperated) is why i did it the way i did

@schnapster
Copy link
Contributor

The only issue is that if you use the Throwable method, the format arguments cannot be used.

That's not true.
https://www.slf4j.org/faq.html#paramException

@schnapster
Copy link
Contributor

the fact that i want the json data seperated

What are the benefits of doing that? There is no information lost by attaching the stacktrace to the source of it.

@Shredder121
Copy link
Contributor

If you want it separated-but-together I can also recommend MDC to bind variables. (which you can then immediately remove if needed)
Or use String.format before it.
You can do a lot if you put everything in an if (logger.is(Warn/Error)Enabled()) statement.
I do vote in favor of keeping the calls together, as "just" the exception logged is a shame for log aggregation tools, or continuity when viewing log files
WOAH TIL! Thanks.
(keeping draft for posterity)

@@ -514,7 +515,7 @@ else if (sample < Short.MIN_VALUE)
}
catch (Exception e)
{
LOG.fatal(e);
LOG.error("Something happened!", e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the comments in this file a bit more descriptive like "Unexpected Exception occurred while X"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) Yea i didn't know what exactly happened there

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its funny how you didn't mention the other 2 exceptions above :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the plural "comments" for a reason 👀

@@ -313,6 +313,7 @@ public Role getPublicRole()
}

@Override
@Deprecated
public TextChannel getPublicChannel()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is going to be in 3.4 we will be removing this method anyway FYI

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just added it because gradle build complained about implementing a deprecated method

+ "\nRoute: " + bucket.getRoute()
+ "\nHeaders: " + headers);
Requester.LOG.debug("Encountered issue with headers when updating a bucket\nRoute: {}\nHeaders: {}",
bucket.getRoute(), headers);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok if i remove the isDebugEnabled part?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, doubt that check is needed.

@@ -938,7 +938,7 @@ protected void handleEvent(JSONObject raw)
// }

JSONObject content = raw.getJSONObject("d");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this into the try catch block below? I just got a JSONException on 286 from this line, missing a print of the faulty JSON.

[2017-10-18 12:30:31,889] [ DEBUG] [JDA RateLimit-Queue Pool - Thread 2] n.d.jda.core.requests.Requester: Received response with following cf-rays: [3afad2a07e74646f-FRA]
[2017-10-18 12:30:47,770] [ ERROR] [JDA [1 / 2] MainWS-ReadThread] n.d.j.core.requests.WebSocketClient: Encountered an Exception 
org.json.JSONException: JSONObject["d"] is not a JSONObject.
        at org.json.JSONObject.getJSONObject(JSONObject.java:641)
        at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:947)
        at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:666)
        at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
        at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:260)
        at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:238)
        at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:963)
        at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:746)
        at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
        at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
        at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
[2017-10-18 12:31:21,579] [ DEBUG] [fredboat.FredBoat.main()] n.d.jda.core.requests.Requester: Received response with following cf-rays: [3afad3d719096361-FRA]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the right PR to do that in

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seing as this is already fixed in another pr, not gonna implement here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That cannot be moved into the try/catch block because the json is used within the catch

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aight. My intent on suggesting that was less concerned with the current bug, more of a general idea to improve future logging.
And Minn is correct, the change is more complicated than moving the two lines into the try catch, I'm sure whoever decides to add this in here or in another PR will figure out something appropriate. Maybe look at other similar places for possible uncaught JSONExceptions and have PR for that after this is merged.

@MinnDevelopment MinnDevelopment added status: freezer this is currently put on hold and removed status: completed has been completed but is not yet released labels Nov 5, 2017
@MinnDevelopment MinnDevelopment removed the status: freezer this is currently put on hold label Nov 12, 2017
Copy link
Member

@MinnDevelopment MinnDevelopment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Collaborator

@Almighty-Alpaca Almighty-Alpaca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than those two minor thing this pr looks good to me, too.

@@ -18,7 +18,7 @@
* Package which contains all utilities for the JDA library.
* These are used by JDA itself and can also be useful for the library consumer!
*
* <p>This contains the JDA logger implementation {@link net.dv8tion.jda.core.utils.SimpleLog SimpleLog}
* <p>This contains the JDA-specific Logger factory {@link net.dv8tion.jda.core.utils.JDALogger JDALogger}
* <br>Which is currently planned to be rewritten to be more end-user friendly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the rewrite isn't planned but finished.

String getString() throws Exception;
}

private JDALogger() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the inner class be at the end of the file?

@kantenkugel kantenkugel merged commit e9399b7 into master Nov 13, 2017
@MinnDevelopment MinnDevelopment deleted the jdalogger branch November 13, 2017 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants