Reputation: 151
Can anyone please tell me in web applications which is the better way to store log information. If we use tomcat server internally the log information will be stored in log file. What is the better approach to log the application data console for trouble shooting either by using system.out.println(" log information here") or log4j log file to store log. which one gives better performance when we are dealing the large application?
Upvotes: 0
Views: 1689
Reputation: 2158
Logger is much better because you can configure appenders, size of the log file, log lever, rolling log files and many other things. Don't use System.out.println for any regular project. You can use it only if you have test project to verify if something in Java works like you think it should.
Upvotes: 2
Reputation: 4736
Using a logger is probably the better approach for a number of reasons:
Turn off certain log levels
You can turn off logs at the debug level when developement is complete, if you used s.o.p, you would have to comment out/remove them
Log anywhere
You can easily choose to log to a file/email/sms without changing your code, you only need to change configuration.
Also, from the log4j(1.2) home page, some performance info is given:
On an AMD Duron clocked at 800Mhz running JDK 1.3.1, it costs about 5 nanoseconds to determine if a logging statement should be logged or not. Actual logging is also quite fast, ranging from 21 microseconds using the SimpleLayout, 37 microseconds using the TTCCLayout. The performance of the PatternLayout is almost as good as the dedicated layouts, except that it is much more flexible.
Upvotes: 1
Reputation: 88707
Which one gives better performance when we are dealing the large application?
Besides the obvious reasons for using a logger (customizable output, filtering, file handling etc.), a logger normally yields better performance, beause of the following:
the logger can provide information on the log level, i.e. if you have to build some expensive message for debugging, you can skip that if debug level is not available:
if( log.isDebugEnabled() ) { log.debug( buildSomeExpensiveOutput() ); }
This would only call buildSomeExpensiveOutput()
if debug level logging is enabled and would save that cost if it isn't (e.g. in most production systems).
writing to the console might slow down the application because execution might be synchronized, whereas most loggers write their logs asynchronously
Upvotes: 2
Reputation: 9474
It is much better to use logging framework (logback, log4j2) because you can fine tune log level without recompilation. For exmaple you find out that you do not need some information - decrease log level from info to error. Or you face issue - increase it to debug or trace.
The performance cost of using log framework is small. You must be careful about concatenation of strings, especially if log level is disabled:
if (log.isDebugEnabled()) {
log.debug("Value = " + value);
}
Upvotes: 1