aspose file tools*
The moose likes Java in General and the fly likes log4j and apache.commons.logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "log4j and apache.commons.logging" Watch "log4j and apache.commons.logging" New topic
Author

log4j and apache.commons.logging

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
what's the relation between log4j and apache.commons.logging ? which one is more popular and better ?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I guess you mean java.util.logging vs org.apache.log4j.

Both are logging APIs, java.util.logging is part of the JDK since version 1.4. Log4J from Apache.

That been said, I have to say that log4j offers some nice features that the java.util.logging does not.

Read this article Log4j vs JDKLogging

Also, read this interesting discussion forum at The Server Side

I hope it helps!
[ April 18, 2006: Message edited by: Edwin Dalorzo ]
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Originally posted by Edwin Dalorzo:
I guess you mean java.util.logging vs org.apache.log4j.

Both are logging APIs, java.util.logging is part of the JDK since version 1.4. Log4J from Apache.

That been said, I have to say that log4j offers some nice features that the java.util.logging does not.

Read this article Log4j vs JDKLogging


No, I meant what's difference b/w org.apache.log4j and org.apache.commons.logging
Also, read this interesting discussion forum at The Server Side

I hope it helps!

[ April 18, 2006: Message edited by: Edwin Dalorzo ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

log4j is a logging framework, i.e. it provides the code to log messages. Commons-logging is an abstraction layer for logging frameworks, it doesn't log anything itself. For example if I write code using commons logging and deploy it on JBoss, the logging is done by log4j, but if I deploy it on WebSphere logging is done by WebSphere's own logging implementation. If I run the same code as a stand alone application it Java's own logging that is used.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Originally posted by Paul Sturrock:
log4j is a logging framework, i.e. it provides the code to log messages. Commons-logging is an abstraction layer for logging frameworks, it doesn't log anything itself. For example if I write code using commons logging and deploy it on JBoss, the logging is done by log4j, but if I deploy it on WebSphere logging is done by WebSphere's own logging implementation. If I run the same code as a stand alone application it Java's own logging that is used.


bear with me as I am new tto this subject. when I checked org.apache.commons.logging API, there is a "Log" interface and it has some methods like "debug", "error". And "LogFactory" class has a method "getLog" returns this type. Anyway, so if I implements this "Log" interface and do "error" and "debug", doesn't it log anything ? just confused.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The Log interface is the abstraction in front of any logger implementation. If you implemented Log as you suggest then yes it would log something. However rather than you yourself implementing this interface you defer to an existing implementation, be that log4j or whatever.

Commons Logging has a default LogFactory which follows these steps to get a Log implementation:
  • looks for a property called org.apache.commons.logging.Log. If it exists, tries to use that Log implementation
  • If that doesn't exist, tries to find log4j on the classpath. If it is there, uses that implementation.
  • If log4j is not present, and the JDK is 1.4+, uses Java's own logging implementation
  • If its pre 1.4 JDK, redirects all output to System.err.

  • ben oliver
    Ranch Hand

    Joined: Mar 28, 2006
    Posts: 375
    Originally posted by Paul Sturrock:
    The Log interface is the abstraction in front of any logger implementation. If you implemented Log as you suggest then yes it would log something. However rather than you yourself implementing this interface you defer to an existing implementation, be that log4j or whatever.

    Commons Logging has a default LogFactory which follows these steps to get a Log implementation:
  • looks for a property called org.apache.commons.logging.Log. If it exists, tries to use that Log implementation
  • If that doesn't exist, tries to find log4j on the classpath. If it is there, uses that implementation.
  • If log4j is not present, and the JDK is 1.4+, uses Java's own logging implementation
  • If its pre 1.4 JDK, redirects all output to System.err.


  • Thanks. I guess I should NOT implement "Log' interface by myself ! Now, please help me check if my understanding is correct ---

    1. I will include log4j.jar in my classpath
    2. I can just import org.apache.commons.logging stuff, and then I use
    *****
    Log myLog = (Log)Logfactory.getLog(..);
    myLog.error(...);
    myLog.debug(..);
    *********

    without worrying about implementing the Log interface. Does this make sense ? Well, Still feel unsafe when I do "myLog.error(..)" since myLog is just an interface...

    Please help. thanks.
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336


    Well, Still feel unsafe when I do "myLog.error(..)" since myLog is just an interface...

    No need to worry - writing to an interface is not only common in the Java world, it is a good idea. If you code to an interface then it doesn't matter how the class that implements that interface changes your code will still work.

    Here's how I would declare a commons logging logger:

    No need to cast the type returned from LogFactory's getLog() method.
    ben oliver
    Ranch Hand

    Joined: Mar 28, 2006
    Posts: 375
    Originally posted by Paul Sturrock:

    No need to cast the type returned from LogFactory's getLog() method.


    thanks. just want to be 100% clear with it -- Do you mean the "logger" in your code is a instantiated object of some class that implements "Log" interface ? It must be, or how can we call a method directly on an interface type ?
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Right.


    "I'm not back." - Bill Harding, Twister
    ben oliver
    Ranch Hand

    Joined: Mar 28, 2006
    Posts: 375
    A follow-up question for this ---

    Where does these logging information written to ? Suppose the application is run today and then tomorrow, does the log go to the same file ? Do I need to hardcaode the output log file and indicate its location ?
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    That depends entirely on the logging framework you use with your application (i.e. which framework commons logging will delegate logging to). If you choose log4j you configure log4j's properties to define where the logging output goes (in log4j-speak: you define appenders). For example a log4j.properties file might look like this:

    Which is defining two appenders, "stdout" (i.e. the console) and "File" which is a log file C:\mylog.log.

    Make sense?
    [ April 24, 2006: Message edited by: Paul Sturrock ]
    Vihar Korlapati
    Greenhorn

    Joined: Aug 18, 2010
    Posts: 1
    if i want to write the log details to a file when i use "org.apache.commons.logging", should i need to use log4j.properties file? if so...how? and where i need to say to read the log4j.properties in java?

    if i am using "org.apache.commons.logging" what is the use of giving log4j.properties?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: log4j and apache.commons.logging