Bug 39690 - Initialization fail in J2EE Environment
Summary: Initialization fail in J2EE Environment
Status: NEEDINFO
Alias: None
Product: Log4j - Now in Jira
Classification: Unclassified
Component: Appender (show other bugs)
Version: 1.3alpha
Hardware: Other Windows XP
: P2 critical
Target Milestone: ---
Assignee: log4j-dev
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-05-31 10:09 UTC by Jan Novotn
Modified: 2008-08-02 10:33 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Novotn 2006-05-31 10:09:41 UTC
I had problems while using DBAppender in J2EE environment - to be conrete while
using Oracle OC4J 9.0.4.0.0. I have log4j-all-1.3alpha-8.jar in applib folder
along with log4j.xml configuration.

Problems occurs when container starts and initializes my EAR - because in some
of my libraries and as I found out either in third party libraries, there are
declarations such as:

private static Logger = LogFactory.getLog("SOMETHING");

This means, that initialization of logger is done while class is loading into
memory and this can occur (and occurs) in initalization stage of EAR in container.
First access to LogFactory triggers loading and initializing of Log4J library
and configured appenders too. DBAppender then tries to find its datasource which
lies in JNDI. But because it is in initialization stage, container refuses to
supply the datasource, because it is not available in JNDI tree yet.
It end with exception and DBAppender does try to initialize no more. Logging to
this appender is then disabled and nothing is logged.

I solve this problem with extending your classes. I append my solution to this
bug - but tell me please whether I havent't understand aim of this appender wrong.

As I know I'm not only one, who has problems with thad. Same problems occur
while using JMS appender (and I think from the same reason).

############## Extended Log4J DBAppender ################

package cz.corpus.f1.commons.log;

import org.apache.log4j.db.DBAppender;
import org.apache.log4j.db.ConnectionSource;
import org.apache.log4j.spi.LoggingEvent;

/**
 * Modifies unwanted behaviour of DBAppenderu while initialization stage of Oracle.
 *
 * @author Jan Novotný
 */
public class Log4JDBAppender extends DBAppender {
    private boolean initializedProperly = false;

    public void activateOptions() {
        try {
            //try to initalize appender with caution
            System.out.println("Log4J - activating appender ... ");
            super.activateOptions();
            initializedProperly = true;
        } catch (Throwable e) {
            //ups .... we have to try in next occasion
            System.out.println("Log4J - error in activating appender (this could
be ok): " + e.getLocalizedMessage());
        } finally {
            // all nice and dandy on the eastern front
            this.active = true;
        }
    }

    protected void append(LoggingEvent event) {
        ConnectionSource connectionSource = getConnectionSource();
        //if the dialect is unknown it means, that initialization didn't
finished well - so we have to try again
        if (!initializedProperly && connectionSource.getSQLDialectCode() !=
ConnectionSource.UNKNOWN_DIALECT) {
            if (connectionSource instanceof Log4JJNDIConnectionSource) {
                System.out.println("Log4J - reconfiguring connection source ... ");
                connectionSource.activateOptions();
            }
            //we try to initialize appender too
            System.out.println("Log4J - reconfiguring appender ... ");
            activateOptions();
        }
        //if initalization finished well we could normally log - otherwise do
nothing
        if (initializedProperly) {
            super.append(event);
        } else System.out.println("Log4J - appender not initialized - event not
logged ...");
    }

}

############## Extended Log4J JNDI ConnectionSource ################

package cz.corpus.f1.commons.log;

import org.apache.log4j.db.JNDIConnectionSource;

/**
 * Modifies unwanted behaviour of DBAppenderu while initialization stage of Oracle.
 *
 * @author Jan Novotný
 */
public class Log4JJNDIConnectionSource extends JNDIConnectionSource {

    /**
     * @see org.apache.log4j.spi.OptionHandler#activateOptions()
     */
    public void activateOptions() {
        try {
            //try to initalize JNDI Connection Source with caution
            System.out.println("LOG4J - Activate options - connection source ... ");
            super.activateOptions();
        } catch (Throwable e) {
            //Ups ... it is possible that JNDI is not ready yet ... postopone
the initialization
            System.out.println("LOG4J - Activate options - connection source ...
failed");
            e.printStackTrace(System.out);
        }
    }

}
Comment 1 Thorbjørn Ravn Andersen 2008-06-30 12:50:21 UTC
This is reported against 1.3alpha, but should also be the same problem in 1.2.

Would a reasonable solution be that you rename the log4j configuration file to something not loaded automatically and then invoke the log4j configuration explicitly in your own code when things are set up correctly?

Comment 2 Thorbjørn Ravn Andersen 2008-08-02 10:33:09 UTC
Marking as NEEDINFO.