Search in sources :

Example 1 with LogConfigurationException

use of org.apache.commons.logging.LogConfigurationException in project hadoop by apache.

the class HttpRequestLog method getRequestLog.

public static RequestLog getRequestLog(String name) {
    String lookup = serverToComponent.get(name);
    if (lookup != null) {
        name = lookup;
    }
    String loggerName = "http.requests." + name;
    String appenderName = name + "requestlog";
    Log logger = LogFactory.getLog(loggerName);
    boolean isLog4JLogger;
    ;
    try {
        isLog4JLogger = logger instanceof Log4JLogger;
    } catch (NoClassDefFoundError err) {
        // In some dependent projects, log4j may not even be on the classpath at
        // runtime, in which case the above instanceof check will throw
        // NoClassDefFoundError.
        LOG.debug("Could not load Log4JLogger class", err);
        isLog4JLogger = false;
    }
    if (isLog4JLogger) {
        Log4JLogger httpLog4JLog = (Log4JLogger) logger;
        Logger httpLogger = httpLog4JLog.getLogger();
        Appender appender = null;
        try {
            appender = httpLogger.getAppender(appenderName);
        } catch (LogConfigurationException e) {
            LOG.warn("Http request log for " + loggerName + " could not be created");
            throw e;
        }
        if (appender == null) {
            LOG.info("Http request log for " + loggerName + " is not defined");
            return null;
        }
        if (appender instanceof HttpRequestLogAppender) {
            HttpRequestLogAppender requestLogAppender = (HttpRequestLogAppender) appender;
            NCSARequestLog requestLog = new NCSARequestLog();
            requestLog.setFilename(requestLogAppender.getFilename());
            requestLog.setRetainDays(requestLogAppender.getRetainDays());
            return requestLog;
        } else {
            LOG.warn("Jetty request log for " + loggerName + " was of the wrong class");
            return null;
        }
    } else {
        LOG.warn("Jetty request log can only be enabled using Log4j");
        return null;
    }
}
Also used : Appender(org.apache.log4j.Appender) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) RequestLog(org.eclipse.jetty.server.RequestLog) Log(org.apache.commons.logging.Log) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) LogConfigurationException(org.apache.commons.logging.LogConfigurationException) Logger(org.apache.log4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger)

Example 2 with LogConfigurationException

use of org.apache.commons.logging.LogConfigurationException in project hbase by apache.

the class HttpRequestLog method getRequestLog.

public static RequestLog getRequestLog(String name) {
    String lookup = serverToComponent.get(name);
    if (lookup != null) {
        name = lookup;
    }
    String loggerName = "http.requests." + name;
    String appenderName = name + "requestlog";
    Log logger = LogFactory.getLog(loggerName);
    if (logger instanceof Log4JLogger) {
        Log4JLogger httpLog4JLog = (Log4JLogger) logger;
        Logger httpLogger = httpLog4JLog.getLogger();
        Appender appender = null;
        try {
            appender = httpLogger.getAppender(appenderName);
        } catch (LogConfigurationException e) {
            LOG.warn("Http request log for " + loggerName + " could not be created");
            throw e;
        }
        if (appender == null) {
            LOG.info("Http request log for " + loggerName + " is not defined");
            return null;
        }
        if (appender instanceof HttpRequestLogAppender) {
            HttpRequestLogAppender requestLogAppender = (HttpRequestLogAppender) appender;
            NCSARequestLog requestLog = new NCSARequestLog();
            requestLog.setFilename(requestLogAppender.getFilename());
            requestLog.setRetainDays(requestLogAppender.getRetainDays());
            return requestLog;
        } else {
            LOG.warn("Jetty request log for " + loggerName + " was of the wrong class");
            return null;
        }
    } else {
        LOG.warn("Jetty request log can only be enabled using Log4j");
        return null;
    }
}
Also used : Appender(org.apache.log4j.Appender) RequestLog(org.eclipse.jetty.server.RequestLog) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) Log(org.apache.commons.logging.Log) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) LogConfigurationException(org.apache.commons.logging.LogConfigurationException) Logger(org.apache.log4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger)

Example 3 with LogConfigurationException

use of org.apache.commons.logging.LogConfigurationException in project XobotOS by xamarin.

the class LogFactoryImpl method createLogFromClass.

/**
     * Attempts to load the given class, find a suitable constructor,
     * and instantiate an instance of Log.
     * 
     * @param logAdapterClassName classname of the Log implementation
     * 
     * @param logCategory  argument to pass to the Log implementation's
     * constructor
     * 
     * @param affectState  <code>true</code> if this object's state should
     * be affected by this method call, <code>false</code> otherwise.
     * 
     * @return  an instance of the given class, or null if the logging
     * library associated with the specified adapter is not available.
     *                          
     * @throws LogConfigurationException if there was a serious error with
     * configuration and the handleFlawedDiscovery method decided this
     * problem was fatal.
     */
private Log createLogFromClass(String logAdapterClassName, String logCategory, boolean affectState) throws LogConfigurationException {
    if (isDiagnosticsEnabled()) {
        logDiagnostic("Attempting to instantiate '" + logAdapterClassName + "'");
    }
    Object[] params = { logCategory };
    Log logAdapter = null;
    Constructor constructor = null;
    Class logAdapterClass = null;
    ClassLoader currentCL = getBaseClassLoader();
    for (; ; ) {
        // Loop through the classloader hierarchy trying to find
        // a viable classloader.
        logDiagnostic("Trying to load '" + logAdapterClassName + "' from classloader " + objectId(currentCL));
        try {
            if (isDiagnosticsEnabled()) {
                // Show the location of the first occurrence of the .class file
                // in the classpath. This is the location that ClassLoader.loadClass
                // will load the class from -- unless the classloader is doing
                // something weird. 
                URL url;
                String resourceName = logAdapterClassName.replace('.', '/') + ".class";
                if (currentCL != null) {
                    url = currentCL.getResource(resourceName);
                } else {
                    url = ClassLoader.getSystemResource(resourceName + ".class");
                }
                if (url == null) {
                    logDiagnostic("Class '" + logAdapterClassName + "' [" + resourceName + "] cannot be found.");
                } else {
                    logDiagnostic("Class '" + logAdapterClassName + "' was found at '" + url + "'");
                }
            }
            Class c = null;
            try {
                c = Class.forName(logAdapterClassName, true, currentCL);
            } catch (ClassNotFoundException originalClassNotFoundException) {
                // The current classloader was unable to find the log adapter 
                // in this or any ancestor classloader. There's no point in
                // trying higher up in the hierarchy in this case..
                String msg = "" + originalClassNotFoundException.getMessage();
                logDiagnostic("The log adapter '" + logAdapterClassName + "' is not available via classloader " + objectId(currentCL) + ": " + msg.trim());
                try {
                    // Try the class classloader.
                    // This may work in cases where the TCCL
                    // does not contain the code executed or JCL.
                    // This behaviour indicates that the application 
                    // classloading strategy is not consistent with the
                    // Java 1.2 classloading guidelines but JCL can
                    // and so should handle this case.
                    c = Class.forName(logAdapterClassName);
                } catch (ClassNotFoundException secondaryClassNotFoundException) {
                    // no point continuing: this adapter isn't available
                    msg = "" + secondaryClassNotFoundException.getMessage();
                    logDiagnostic("The log adapter '" + logAdapterClassName + "' is not available via the LogFactoryImpl class classloader: " + msg.trim());
                    break;
                }
            }
            constructor = c.getConstructor(logConstructorSignature);
            Object o = constructor.newInstance(params);
            // adapter couldn't be instantiated anyway.
            if (o instanceof Log) {
                logAdapterClass = c;
                logAdapter = (Log) o;
                break;
            }
            // Oops, we have a potential problem here. An adapter class
            // has been found and its underlying lib is present too, but
            // there are multiple Log interface classes available making it
            // impossible to cast to the type the caller wanted. We 
            // certainly can't use this logger, but we need to know whether
            // to keep on discovering or terminate now.
            //
            // The handleFlawedHierarchy method will throw 
            // LogConfigurationException if it regards this problem as
            // fatal, and just return if not.
            handleFlawedHierarchy(currentCL, c);
        } catch (NoClassDefFoundError e) {
            // We were able to load the adapter but it had references to
            // other classes that could not be found. This simply means that
            // the underlying logger library is not present in this or any
            // ancestor classloader. There's no point in trying higher up
            // in the hierarchy in this case..
            String msg = "" + e.getMessage();
            logDiagnostic("The log adapter '" + logAdapterClassName + "' is missing dependencies when loaded via classloader " + objectId(currentCL) + ": " + msg.trim());
            break;
        } catch (ExceptionInInitializerError e) {
            // A static initializer block or the initializer code associated 
            // with a static variable on the log adapter class has thrown
            // an exception.
            //
            // We treat this as meaning the adapter's underlying logging
            // library could not be found.
            String msg = "" + e.getMessage();
            logDiagnostic("The log adapter '" + logAdapterClassName + "' is unable to initialize itself when loaded via classloader " + objectId(currentCL) + ": " + msg.trim());
            break;
        } catch (LogConfigurationException e) {
            // a LogConfigurationException, so just throw it on                
            throw e;
        } catch (Throwable t) {
            // handleFlawedDiscovery will determine whether this is a fatal
            // problem or not. If it is fatal, then a LogConfigurationException
            // will be thrown.
            handleFlawedDiscovery(logAdapterClassName, currentCL, t);
        }
        if (currentCL == null) {
            break;
        }
        // try the parent classloader
        currentCL = currentCL.getParent();
    }
    if ((logAdapter != null) && affectState) {
        // We've succeeded, so set instance fields
        this.logClassName = logAdapterClassName;
        this.logConstructor = constructor;
        // Identify the <code>setLogFactory</code> method (if there is one)
        try {
            this.logMethod = logAdapterClass.getMethod("setLogFactory", logMethodSignature);
            logDiagnostic("Found method setLogFactory(LogFactory) in '" + logAdapterClassName + "'");
        } catch (Throwable t) {
            this.logMethod = null;
            logDiagnostic("[INFO] '" + logAdapterClassName + "' from classloader " + objectId(currentCL) + " does not declare optional method " + "setLogFactory(LogFactory)");
        }
        logDiagnostic("Log adapter '" + logAdapterClassName + "' from classloader " + objectId(logAdapterClass.getClassLoader()) + " has been selected for use.");
    }
    return logAdapter;
}
Also used : Log(org.apache.commons.logging.Log) Constructor(java.lang.reflect.Constructor) URL(java.net.URL) LogConfigurationException(org.apache.commons.logging.LogConfigurationException)

Example 4 with LogConfigurationException

use of org.apache.commons.logging.LogConfigurationException in project XobotOS by xamarin.

the class LogFactoryImpl method newInstance.

/**
     * Create and return a new {@link org.apache.commons.logging.Log}
     * instance for the specified name.
     *
     * @param name Name of the new logger
     *
     * @exception LogConfigurationException if a new instance cannot
     *  be created
     */
protected Log newInstance(String name) throws LogConfigurationException {
    Log instance = null;
    try {
        if (logConstructor == null) {
            instance = discoverLogImplementation(name);
        } else {
            Object[] params = { name };
            instance = (Log) logConstructor.newInstance(params);
        }
        if (logMethod != null) {
            Object[] params = { this };
            logMethod.invoke(instance, params);
        }
        return (instance);
    } catch (LogConfigurationException lce) {
        // just pass it on
        throw (LogConfigurationException) lce;
    } catch (InvocationTargetException e) {
        // A problem occurred invoking the Constructor or Method 
        // previously discovered
        Throwable c = e.getTargetException();
        if (c != null) {
            throw new LogConfigurationException(c);
        } else {
            throw new LogConfigurationException(e);
        }
    } catch (Throwable t) {
        // previously discovered
        throw new LogConfigurationException(t);
    }
}
Also used : Log(org.apache.commons.logging.Log) LogConfigurationException(org.apache.commons.logging.LogConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with LogConfigurationException

use of org.apache.commons.logging.LogConfigurationException in project ACS by ACS-Community.

the class CommonsLoggingFactory method newInstance.

@Override
protected synchronized Log newInstance(String name) throws LogConfigurationException {
    //		System.out.println("CommonsLoggingFactoryForACS#newInstance called for name=" + name);
    // Check which framework is requesting the apache commons logger
    String loggerNameBase = null;
    StackTraceElement[] stackTrace = (new Exception()).getStackTrace();
    for (StackTraceElement stackTraceElement : stackTrace) {
        if (stackTraceElement.getClassName().contains("org.apache.commons.scxml")) {
            loggerNameBase = SCXML_LOGGER_NAME_PREFIX;
            break;
        }
    // TODO: add check for other frameworks that use apache logging commons, once we have those
    }
    // Give up if we did not recognize the client framework
    if (loggerNameBase == null) {
        return super.newInstance(name);
    }
    // Check if we already have a logger for the client framework, and create it if needed.
    Log myLog = loggerMap.get(loggerNameBase);
    if (myLog == null) {
        final String loggerNameBaseFinal = loggerNameBase;
        myLog = new Jdk14Logger(loggerNameBase) {

            @Override
            public Logger getLogger() {
                // to some known framework loggers, in the absence of other specific log configuration.
                return ClientLogManager.getAcsLogManager().getLoggerForCorba(loggerNameBaseFinal, true);
            }
        };
        loggerMap.put(loggerNameBase, myLog);
    }
    return myLog;
}
Also used : Jdk14Logger(org.apache.commons.logging.impl.Jdk14Logger) Log(org.apache.commons.logging.Log) Logger(java.util.logging.Logger) Jdk14Logger(org.apache.commons.logging.impl.Jdk14Logger) LogConfigurationException(org.apache.commons.logging.LogConfigurationException)

Aggregations

LogConfigurationException (org.apache.commons.logging.LogConfigurationException)13 Log (org.apache.commons.logging.Log)12 Constructor (java.lang.reflect.Constructor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 URL (java.net.URL)3 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)2 Appender (org.apache.log4j.Appender)2 Logger (org.apache.log4j.Logger)2 NCSARequestLog (org.eclipse.jetty.server.NCSARequestLog)2 RequestLog (org.eclipse.jetty.server.RequestLog)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)1 JoranException (ch.qos.logback.core.joran.spi.JoranException)1 File (java.io.File)1 Files (java.nio.file.Files)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Arrays (java.util.Arrays)1 List (java.util.List)1