2

I'm starting a java project using slf4j for logging, gradle for building, and intellij for the IDE. How do I have intellij show the logs in the IDE when I run the project?

I've been attempting to find out how to put slf4j-simple in the classpath without declaring it in build.gradle. I would think that outputting logs to System.err would make them show in intellij. Do you think this is on the right track?

If you're familiar with slf4j, you know that the point of slf4j is to avoid declaring compile-time dependencies on logging frameworks.

3
  • If you're writing an executable application, you do declare your dependency on your logging implementation. Commented Jul 18, 2016 at 3:28
  • @chrylis I have reason to want to delay that decision. I see your point though. If you think it's not possible, go ahead and submit an answer. Commented Jul 18, 2016 at 3:46
  • Why not configure a console appender, or whatever it may be called depending on the underlying implementation? Commented Jul 18, 2016 at 16:02

1 Answer 1

2

In order to show output, there needs to be both slf4j-api as well as exactly one slf4j implementation binding (such as slf4j-simple or logback or whatever) in the classpath. This means:

  • If you're writing some kind of library (either for internal use by other modules you maintain, or "for the public"), you should depend only on slf4j-api and not have a compile- or runtime-declared implementation binding dependency.
  • If you're writing some end application, you need to include the implementation binding dependency, though it can probably be runtime-scoped rather than compile-scoped.

If you're expecting to "see" something when you "run the project", then either:

  1. This is some kind of library, and you're doing some kind of unit or integration testing on your library, and so you should include an implementation binding dependency (like slf4j-simple) in your testing classpath (so that you see output while testing, but it's not used by whatever depends on your library). Or,
  2. This is some kind of end application, so there should be an implementation binding dependency defined in compile or runtime scopes. If you're not sure what you want to use eventually, that's fine, just pick something to use for now that does simple logging to the console, since SLF4J makes it easy to replace later. Many logging frameworks easily support having separate configurations for "production" as opposed to testing, and I think it's fairly common to have the testing configuration basically just log to console.

IntelliJ IDEA will automatically display anything printed to the console. Also, many run configuration types have a "Log" tab that lets one specify log files to scan and also display while an application is being run. So, if you do want a testing logging configuration that has logging to multiple files and the like, you can generally configure IDEA to be able to let you see those as well while your application is running.

My experience is much more with Maven than with Gradle, but I believe the concept of dependency scope of compile, runtime, and test translate fairly straightforwardly to Gradle. Please ask if you need more specific clarification.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.