Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write log file using org.apache.commons.logging

I'm writing an application where I need to write log to a file using org.apache.commons.logging library, but i don't know how to start.

Can anyone help me?

Thanks & best regards.

like image 984
KhanhQuach Avatar asked Aug 05 '12 10:08

KhanhQuach


People also ask

What is org Apache Commons Logging?

Apache Commons Logging (previously known as Jakarta Commons Logging or JCL) is a Java-based logging utility and a programming model for logging and for other toolkits. It provides APIs, log implementations, and wrapper implementations over some other tools.

What is the use of Org Apache Commons Logging LogFactory?

The Logfactory class uses the Factory Design Pattern to select a logger class. The Apache Commons Logging library decouples the code from the underlying logging library we use. The Logger class can be any one from these: org.

Does Org Apache Commons use log4j?

Apache Commons Logging is an abstraction for the concrete implementation. It uses log4j, if present and configured.

Does SLF4J use Commons Logging?

SLF4J based implementation of commons-logging wrapper APIs.


2 Answers

Try this sample, first you need two properties files likes this;

commons-logging.properties that put in your application's classpath. The contents of this file should look like:

    org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger

You can also use Log4j logger besides Jdk14Logger.And need second custom properties file.For example log-config.properties looks like this:

    # The following creates two handlers
    handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
    # Set the default logging level for the root logger
    .level=SEVERE
    # log level for the "com.example" package
    sample.logging.level=FINE
    # Set the default logging level
    java.util.logging.ConsoleHandler.level=ALL
    java.util.logging.FileHandler.level=FINE
    # Set the default formatter
    java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
    java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
    # Specify the location and name of the log file
    java.util.logging.FileHandler.pattern=D:/temp/log/test.log

This is sample test class

     public class TestLog {

     private static Log log = LogFactory.getLog(TestLog.class);
     public static void main(String[] args) {
          log.info("Testing Info Message.");
              if (log.isDebugEnabled()) {
                  log.debug("Testing Debug Message.");
          }
        }
     }

This is sample package structure using eclipse;

enter image description here

And add TestLog class's Edit Configuration under VM arguments likes this;

  -Djava.util.logging.config.file=/D:/dev/workspace/LoggingTest/bin/log-config.properties(your properties file path)

enter image description here

And run then you can find your log file under D:/temp/log/test.log

like image 190
Sai Ye Yan Naing Aye Avatar answered Sep 19 '22 05:09

Sai Ye Yan Naing Aye


I hope this helps... this is how we have done in our project...

A. Include the jar in your project.

B. Define log4j.xml for loggers definition something like this...

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

    <appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
    <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
    <param name="File" value="${jboss.server.log.dir}/server.log"/>
    <param name="Append" value="false"/>
    <param name="MaxFileSize" value="1048576KB"/>
    <param name="MaxBackupIndex" value="3"/>

 <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
 </layout>      
</appender>

<root>
   <appender-ref ref="CONSOLE"/>
   <appender-ref ref="FILE"/>
</root>

</log4j:configuration>

C. Use the logger in the class:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Class YourClass{
    private static Log log = LogFactory.getLog(YourClass.class);

    public void yourMethod(){
        log.info("Your Message");
    }
}

EDIT: D. Since we have a JBoss AS environment so the application is configured to read log4j.xml like following (You would need an equivalent config):

<mbean code="org.jboss.logging.Log4jService"
  name="jboss.system:type=Log4jService,service=Logging"
  xmbean-dd="resource:xmdesc/Log4jService-xmbean.xml">
  <attribute name="ConfigurationURL">resource:jboss-log4j.xml</attribute>
  <!-- Set the org.apache.log4j.helpers.LogLog.setQuiteMode. As of log4j1.2.8
  this needs to be set to avoid a possible deadlock on exception at the
  appender level. See bug#696819.
  -->
  <attribute name="Log4jQuietMode">true</attribute>
  <!-- How frequently in seconds the ConfigurationURL is checked for changes -->
  <attribute name="RefreshPeriod">60</attribute>
</mbean>
like image 32
Bharat Sinha Avatar answered Sep 21 '22 05:09

Bharat Sinha