You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by bu...@apache.org on 2006/05/31 12:09:42 UTC

DO NOT REPLY [Bug 39690] New: - Initialization fail in J2EE Environment

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=39690>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=39690

           Summary: Initialization fail in J2EE Environment
           Product: Log4j
           Version: 1.3alpha
          Platform: Other
        OS/Version: Windows XP
            Status: NEW
          Severity: critical
          Priority: P2
         Component: Appender
        AssignedTo: log4j-dev@logging.apache.org
        ReportedBy: jan.novotny@corpus.cz


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);
        }
    }

}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org