aspose file tools*
The moose likes Java in General and the fly likes log4j  vs  system.out.Println() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "log4j  vs  system.out.Println()" Watch "log4j  vs  system.out.Println()" New topic
Author

log4j vs system.out.Println()

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 29, 2005
Posts: 2308
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


Rahul Bhattacharjee
LinkedIn - Blog
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
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:



http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Do you mean open source for logging ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
v ray
Ranch Hand

Joined: Mar 15, 2007
Posts: 223
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: log4j vs system.out.Println()