Java – How to configure custom log file in java web project

javajboss5.xlog4j

I am developing a java web application using eclipse IDE and jboss server (version 5). I am trying to implement a custom log file for my application with the following code :

FileAppender fileAppender=new FileAppender(newPatternLayout(),Constant.LOGGER_PATH);
logger.addAppender(fileAppender);
BasicConfigurator.configure();
logger.setLevel(Level.ERROR);
logger.error(cause);

When I am trying to deploy my application to the jboss server it gives following exception :

    18:35:16,346 ERROR [STDERR] log4j:ERROR A "org.jboss.logging.util.OnlyOnceError
    andler" object is not assignable to a "org.apache.log4j.spi.ErrorHandler" varia
    le.
    18:35:16,346 ERROR [STDERR] log4j:ERROR The class "org.apache.log4j.spi.ErrorHa
    dler" was loaded by
    18:35:16,346 ERROR [STDERR] log4j:ERROR [WebappClassLoader
      delegate: false
      repositories:
        /WEB-INF/classes/
    ----------> Parent Classloader:
    java.net.FactoryURLClassLoader@893918
    ] whereas object of type
    18:35:16,346 ERROR [STDERR] log4j:ERROR "org.jboss.logging.util.OnlyOnceErrorHa
    dler" was loaded by [org.jboss.system.server.NoAnnotationURLClassLoader@a32b].
    18:35:16,377 ERROR [STDERR] log4j:ERROR Could not create an Appender. Reported
    rror follows.
    18:35:16,377 ERROR [STDERR] java.lang.ClassCastException: org.jboss.logging.app
    nder.DailyRollingFileAppender cannot be cast to org.apache.log4j.Appender

Best Answer

It looks like your application has its own copy of log4j.jar bundles into its lib directory. This will clash with JBoss's own copy.

Remove that JAR from the lib directory and try again, your app will use the JBoss copy.