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

fix: switch from log4j to SLF4J #610

Merged
merged 6 commits into from Jan 29, 2021

Conversation

sullis
Copy link
Contributor

@sullis sullis commented Dec 6, 2020

Checklist

  • I acknowledge that all my contributions will be made under the project's license

@eshanholtz
Copy link
Contributor

@sullis We recently added log4j as our logging dependency. Any reason why we should be using SLF4J instead?

@eshanholtz eshanholtz added the status: waiting for feedback waiting for feedback from the submitter label Dec 8, 2020
@lex-erik
Copy link

Hi @eshanholtz, below I've written up a few reasons for using Slf4j.

Log4j is an implementation, Slf4j is a logging abstraction and not an actual implementation. This is important, as it allows consumers of your library to choose their own flavor of logging implementation.

By forcing Log4j unto your consumers,

  • the Twilio SDK is possibly breaking applications by including an additional logging implementation
  • when consumers use a different logging implementation, then logs generated by Twilio do not follow the rules and may not be shipped to ElasticSearch or Splunk unless I create a log4j version of the logging configuration.

To hit it home further, I'm currently running into one of these problems. The Twilio SDK is currently conflicting with server frameworks such as Spring and Micronaut that rely on classpath scanning and detect which logging library is on the classpath. See for example here where Micronaut detects the logging implementation, however, when it finds multiple Logging implementations it will refuse to start (as it should). Spring works in a similar fashion.

I believe it's considered a rule of thumb that as SDK developers you must use Slf4j and leave logging implementation choices to your consumer. SDK developers can chose to include log4j as runtime dependency when they develop, but it should never become a compile time dependency. Slf4j is exactly created to solve these type of problems.

My $.02.

@lex-erik
Copy link

As FYI, for whoever is running into similar issues, Twilio 7.3.0 is the latest release before log4j became a compile dependency. Reverting back to Twilio SDK 7.3.0 worked for me.

Copy link
Contributor

@thinkingserious thinkingserious left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @sullis!

Please take a look at my comments and let us know what you think. Thanks again!

pom.xml Show resolved Hide resolved
README.md Outdated
Twilio.setLoggerConfiguration("path/to/log4j2.xml");
```

This library uses SFL4J for logging. Consult the SFL4J documentation
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to update the example (perhaps java.util.logging, logback or log4j).

pom.xml Show resolved Hide resolved
src/main/java/com/twilio/Twilio.java Show resolved Hide resolved
This is a proof of concept. It demonstrates how SLF4J could be used
as the Logging API.

SFL4J is the standard logging API for open source Java libraries.
@thinkingserious thinkingserious added the type: community enhancement feature request not on Twilio's roadmap label Dec 28, 2020
@eshanholtz eshanholtz changed the title SLF4J logging api fix: SLF4J logging api Jan 5, 2021
@eshanholtz
Copy link
Contributor

@sullis Your changes look good. Can you just address the last few comments from @thinkingserious and we can get this PR merged. Thanks!

@thinkingserious
Copy link
Contributor

Hello @sullis,

Would you like us to take this PR from here and get this PR closed out? Please let me know what you think. Thanks again!

With best regards,

Elmer

@sullis
Copy link
Contributor Author

sullis commented Jan 26, 2021

Would you like us to take this PR from here and get this PR closed out? Please let me know what you think. Thanks again!

Please take ownership of this PR and/or open a replacement PR. I think it is best for you to own it.

@thinkingserious
Copy link
Contributor

Thank you @sullis! We appreciate your contribution to help us get this update merged!

@thinkingserious
Copy link
Contributor

thinkingserious commented Jan 28, 2021

Here is how I tested this, from this branch:

  1. make install (from twilio-java root)
  2. touch LogExample.java
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

public class LogExample {
    public static void main(String[] args) {
        String accountSid = System.getenv("TWILIO_ACCOUNT_SID");
		String authToken = System.getenv("TWILIO_AUTH_TOKEN");
        Twilio.init(accountSid, authToken);
        Message message = Message.creator(
        new PhoneNumber(System.getenv("TWILIO_TO_NUMBER");),
        new PhoneNumber(System.getenv("TWILIO_FROM_NUMBER");),
            "Hello logging world!"
        ).create();

        System.out.println(message.getSid());
    }
}
  1. touch log4j.properties
log4j.rootLogger=DEBUG,stdout
log4j.logger.com.endeca=INFO
log4j.logger.com.endeca.itl.web.metrics=INFO

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n
  1. Download the following jars: slf4j-log4j12-1.7.28.jar, log4j-1.2.17.jar, slf4j-api-1.7.28.jar
  2. javac -classpath ./slf4j-log4j12-1.7.28.jar:./log4j-1.2.17.jar:./slf4j-api-1.7.28.jar:./target/twilio-8.6.0-jar-with-dependencies.jar:. LogExample.java && java -classpath ./slf4j-log4j12-1.7.28.jar:./log4j-1.2.17.jar:./slf4j-api-1.7.28.jar:./target/twilio-8.6.0-jar-with-dependencies.jar:. LogExample

Then, you should see something like:

...
DEBUG	2021-01-27 21:15:06,759	784	com.twilio.http.TwilioRestClient	[main]	responseHeader: X-Shenanigans: none
...

@thinkingserious thinkingserious marked this pull request as ready for review January 28, 2021 05:36
@@ -59,11 +58,14 @@ public Response request(final Request request) {

logRequest(request);
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to add a check either within this function or before this function is called to make sure logger.isDebugEnabled(). Within the logRequest() function can we add some additional logic to make sure headerParams and queryParams are not null before we log them?

Copy link
Contributor

Choose a reason for hiding this comment

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

Great catch! isEmpty() does not catch null values.

src/test/resources/log4j2.xml Show resolved Hide resolved
Copy link
Contributor

@JenniferMah JenniferMah left a comment

Choose a reason for hiding this comment

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

Did you test this with Java 9 or later to make sure that we don't see any issues with the build artifact?

@thinkingserious
Copy link
Contributor

I tested with Java 11 and did not observe any issues.

Copy link
Contributor

@JenniferMah JenniferMah left a comment

Choose a reason for hiding this comment

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

🎆

@thinkingserious thinkingserious changed the title fix: SLF4J logging api fix: switch to SLF4J Jan 29, 2021
@thinkingserious thinkingserious changed the title fix: switch to SLF4J fix: switch from log4j to SLF4J Jan 29, 2021
@thinkingserious thinkingserious merged commit 69e449f into twilio:main Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for feedback waiting for feedback from the submitter type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants