JasCav
JasCav

Reputation: 34632

Understanding Logging in Java System

I recently began working on a project in IntelliJ (Java 8) in which I am writing (essentially) a little upload/download client. It works fine, but I completely do not understand how logging is working within my application. (Please bear with me, this is a myriad of problems, and while I can get things to sort of work, it is not making sense to me and I want to understand what is happening.)

It appears that one of the libraries I am using is leveraging SLF4J with underlying Log4J. Figuring I would continue to use this in my own application. However, when I attempted to add:

compile 'org.slf4j:slf4j-log4j12:1.7.7'

to my Gradle build dependencies and run my application, I receive the exception:

SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. 
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
Exception in thread "main" java.lang.ExceptionInInitializerError

From my research, I understand what the issue is (creating a kind of recursive reference), though I don't understand why another library's use of a library is affecting my use. Seems strange.

In any case, I then switched over to using slf4j-simple. This fixed the above issue, and, when I run my application in IntelliJ, everything works great. I see the logging in the console (which is what I want for now), and that's it. No other logging is displaying.

Where things get really weird (and where my question is going) is that when I build and deploy my app to my test system, and then proceed to run it in on the test system via a small batch script in the Windows command prompt, I see a ton of output from every library...except my own. None of my logging is registering anywhere.

This completely makes no sense to me, and I am wondering what it is about the logging structure that I am not understanding. Ultimately, I don't want to see other library logs, but I would like to see my own. (And, if I can use Log4J on top of all this, +bonus points, but that's not my main question.)

(Note that all logging is at INFO or higher level. But, even if I log everything as errors, I still don't see them when I run java -jar MyApp.jar from the Windows console.)

Upvotes: 0

Views: 245

Answers (1)

Daniel Stolz
Daniel Stolz

Reputation: 308

If you use log4j-over-slf4j.jar, your code should use the log4j API. Then, in addition to the slf4j apis, you will need a back end logging implementation (that is NOT log4j, apparently). slf4j is just a facade; it does not provide its own logging implemenation.

If you use slf4j-log4j12.jar, your code should use the slf4j API. You are then using log4j as the logging back end (so you would, for example, use log4j.properties to configure logging).

Based on the error you are receiving, I am assuming the project is using the first option. You could look at the other code to see which logging API is being used. If the project is using log4j APIs, you should as well.

Upvotes: 1

Related Questions