简体   繁体   中英

log4j over System.out.println

What is performance wise advantage of using log4j over System.out.Println ? FYI:I know log4j has multiple appenders,debug logging and other features which System.Out.println doesn't have and is applicable at class level also and is used in larger applications. But if I have a small application, say a file will log4j will provide better performance than System.Out.println . How internally log4j works?

Log4j isn't entitled to be more performant. It was created for having more abilities to decrease log amounts and specify the log output. Imagine a Tomcat server which logs amounts of hibernate database accesses. Eg with the log level you can stop straining the server through this info logs. But this is not a "native" performance advantage since you can simulate this with flag checking before sysos.

The only case I can think of in which you could perceive a performance advantage using log4j over System.out.println would be for example if doing this:

logger.debug("Logging something");

Instead of doing this:

System.out.println("Logging something");

And you then configure the logging level in such a way that it does not require logging debug messages. In that scenario, the first line will not actually log, but the second one will still write to the System.out.

In all other cases (I mean, in all cases where logging is actually done) I don't think it will be faster to use log4j performance wise .

Usually it's used for all the reasons you state, and because the performance is not that different (framework overhead is minimal compared to the time it actually takes to write things on files).

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