What is considered better practice ? Will I get all of Log4J2 benefits if I code to SLF4J ? Will it effect performance ? Is all of the above questions get the same answer for JCL as well ?
Asked
Active
Viewed 3,593 times
2
-
Possible duplicate of [How to use log4j 2.0 and slf4j and Commons Logging together](http://stackoverflow.com/questions/15095877/how-to-use-log4j-2-0-and-slf4j-and-commons-logging-together) – Thilo Oct 26 '15 at 09:41
-
@Thilo - that's partially answering my question. Will I get different performance based on my API layer ? – Nati Oct 26 '15 at 09:44
-
1Actually, the up-voted answer over there talks about log4j 1.2 (not 2.0). Maybe ignore that thread... I wonder if someone has put a dollar amount on the cost of the confusion between the many Java logging frameworks. – Thilo Oct 26 '15 at 09:50
-
1In short, SLF4J is the prefered choice. The performance is almost entirely dependent on the configuration which will still be done with LOG4J2 xml files. Both SLF4J and LOG4J2 support parameterized logging which should be used to increase performance. – alan7678 Oct 26 '15 at 19:20
-
1The only real drawback of using SLF4J could also be considered a bonus. The api is simpler, which allows less flexibility(ie. no custom log levels, all objects must be parameterized, throwables must be the last arg) etc. Speaking from experience working with a very large logging project, although you lose some benefit of the flexibility with the log4j2 api, you gain it back by having a simpler implementation which is easier to refactor in the future. – alan7678 Oct 26 '15 at 19:24
1 Answers
8
As a developer on Log4j 2 I am, of course, biased. If you use Markers and filter on them I would recommend Log4j over SLF4J. SLF4J has some concurrency problems with Markers. If you want to take advantage of Log4j's support for logging Messages instead of Strings then, of course, you have to use Log4j's API. If you want to log using a printf style API you will have to use the Log4j API. If you want to use Java 8 lambda expressions you will have to use the Log4j API.
OTOH, if you you are perfectly happy with the SLF4J API and don't want any of the additional features the performance cost of using it should be minimal.
I should also note that it is possible to write to the Log4j 2 API and have that routed into SLF4J.

rgoers
- 8,696
- 1
- 22
- 24
-
what's the point of routing from LOG4J2 to SLF4J ? using slf4j simple isn't appealing in any way... – Nati Oct 28 '15 at 11:53
-
2The point is that even if your application uses the Log4j 2 API, you are not necessarily bound to the Log4j 2 implementation: you can still switch to another logging implementation without changing your application code. – Remko Popma Oct 30 '15 at 04:00