• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

log4j vs system.out.Println()

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

what is the difference between printing

a content in console using log4j and

system.out.println().peoples use to say

that using system.out.println() is performance

loss

i think log4j is written in java so it may also

use system.out.println() to print the content in console


then why peoples preferring log4j ?

i think i have posted this topic in correct forum
i am not including any web related techonology


regards
amir
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all you're ever going to want to do is print a few messages to console, then System.out.println() is fine.

A proper logging system, like Log4J, adds all sorts of features that are very useful in real applications. You can direct the output to different places (console, file, network etc.). You can tell it to output only a subset of the messages, without having to recompile. You can get timestamp on each message etc.

If you're writing a small utility program for your own use, which will never expand into something more, use System.out.println(). If you're writing industrial-strength code, or a utility library for others, using a proper logging system is the professional thing to do.

Log4J is not the only one, by the way.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks perter

for your reply

iam scrutinize only in the performance

some bloggers have told that system.out.prrintln() will consume

to much of performance than log4j

i think log4j will consume more performance because it

have has to print more information

regards
amir
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
log4j's ConsoleAppender uses either System.out or System.err, so what you were told is not correct. And as log4j is wrapping the standard streams, it is a bit slower than using System directly.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amirtharaj, could you please avoid to separate each lines with a blank line. It makes it really difficult to read. Thank you
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Configuration is another factor for using log4j over SOP's.
Even while you are developing you application you can set the log4j level to DEBUG and at production you can turn that to ERROR or FATAL.

Logging too much statements might be a performance overhead .You should always follow the best practices for better performance.
Check out the last section of document.
http://logging.apache.org/log4j/docs/manual.html
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you use System.out.println() I would use a wrapper logging class that will later allow you to use log4j while only changing this class and not all of your apps logging methods. I don't have a good example of all the methods you should include, but something like this (as well as the methods that take an exception would be good i.e. error(message, exception))



Initiallly debug might look like this:

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks one and all i want to know what are all the similar opensource tools like log4j that can be used in java platform
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean open source for logging ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Java 1.4, the standard API itself contains a logging facility: see the API documentation of package java.util.logging.

However, a lot of people still seem to prefer Log4J above the standard logging API. Log4J is probably a bit more flexible than the standard logging API.
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, for normal small programs where performance or any other similar issue is never a problem, SOP's are fine, but when running a real time project, its best to use some kind of logger. log4j is great, but even the logger from the util package is better than SOPs...Another thing which intruiges me is assertions, need to use it more!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic