简体   繁体   中英

How do I get logging output in IntelliJ using slf4j in my Gradle project?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM