<commit_before>package org.apache.velocity.runtime.log;

/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.util.ClassUtils;

/**
 * <p>
 * This class is responsible for instantiating the correct LogChute
 * </p>
 *
 * <p>
 * The approach is :
 * </p>
 * <ul>
 * <li> 
 *      First try to see if the user is passing in a living object
 *      that is a LogChute, allowing the app to give its living
 *      custom loggers.
 *  </li>
 *  <li> 
 *       Next, run through the (possible) list of classes specified
 *       specified as loggers, taking the first one that appears to 
 *       work.  This is how we support finding logkit, log4j or
 *       jdk logging, whichever is in the classpath and found first, 
 *       as all three are listed as defaults.
 *  </li>
 *  <li>
 *      Finally, we turn to the System.out stream and print log messages
 *      to it if nothing else works.
 *  </li>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME>vens</a>
 * @author <a href="mailto:<EMAIL>"><NAME>.</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class LogManager
{
    /**
     *  Creates a new logging system or returns an existing one
     *  specified by the application.
     */
    public static LogChute createLogChute(RuntimeServices rsvc) throws Exception
    {
        /*
         *  if a logSystem was set as a configuation value, use that. 
         *  This is any class the user specifies.
         */
        Object o = rsvc.getProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM );

        if (o != null)
        {
            // first check for a LogChute
            if (o instanceof LogChute)
            {
                ((LogChute)o).init(rsvc);
                return (LogChute)o;
            }
            // then check for a LogSystem
            if (o instanceof LogSystem)
            {
                // wrap the LogSystem into a chute.
                LogChute chute = new LogChuteSystem((LogSystem)o);
                chute.init(rsvc);
                // warn the user about the deprecation
                chute.log(LogChute.WARN_ID, 
                          "LogSystem has been deprecated. Please use a LogChute implementation.");
                return chute;
            }
        }
  
        /* otherwise, see if a class was specified.  You can put multiple 
         * classes, and we use the first one we find.
         *
         * Note that the default value of this property contains the
         * AvalonLogChute, the Log4JLogChute, and the JdkLogChute for 
         * convenience - so we use whichever we find first.
         */
        List classes = new ArrayList();
        Object obj = rsvc.getProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS );

        /*
         *  we might have a list, or not - so check
         */
        if ( obj instanceof List)
        {
            classes = (List) obj;
        }
        else if ( obj instanceof String)
        { 
            classes.add( obj );
        }

        Log log = rsvc.getLog();

        /*
         *  now run through the list, trying each.  It's ok to 
         *  fail with a class not found, as we do this to also
         *  search out a default simple file logger
         */
        for( Iterator ii = classes.iterator(); ii.hasNext(); )
        {
            String claz = (String) ii.next();
            if (claz != null && claz.length() > 0 )
            {
                log.info("Trying to use logger class " + claz );
                try
                {
                    o = ClassUtils.getNewInstance( claz );
                    if (o instanceof LogChute)
                    {
                        ((LogChute)o).init(rsvc);
                        log.info("Using logger class " + claz);
                        return (LogChute)o;
                    }
                    else if (o instanceof LogSystem)
                    {
                        LogChute chute = new LogChuteSystem((LogSystem)o);
                        chute.init(rsvc);
                        // warn the user about the deprecation
                        chute.log(LogChute.WARN_ID, 
                                  "LogSystem has been deprecated. Please use a LogChute implementation.");
                        return chute;
                    }
                    else
                    {
                        log.error("The specifid logger class " + claz +
                                  " isn't a valid LogChute");
                    }
                }
                catch( NoClassDefFoundError ncdfe )
                {
                    log.debug("Couldn't find class " + claz 
                              + " or necessary supporting classes in "
                              + "classpath. Exception : " + ncdfe);
                }
            }
        }
      
        /* If the above failed, that means either the user specified a 
         * logging class that we can't find, there weren't the necessary
         * dependencies in the classpath for it, or there were the same
         * problems for the default loggers, log4j and Java1.4+.
         * Since we really don't know and we want to be sure the user knows
         * that something went wrong with the logging, let's fall back to the 
         * surefire StandardOutLogChute. No panicking or failing to log!!
         */
        LogChute sls = new StandardOutLogChute();
        sls.init(rsvc);
        log.info("Using StandardOutLogChute.");
        return sls;
    }

    /**
     * Update the Log instance with the appropriate LogChute and other
     * settings determined by the RuntimeServices.
     */
    public static void updateLog(Log log, RuntimeServices rsvc) throws Exception
    {
        // create a new LogChute using the RuntimeServices
        LogChute newLogChute = createLogChute(rsvc);
        LogChute oldLogChute = log.getLogChute();

        // If the old LogChute was a PrimordialLogChute,
        // dump its messages into the new system first.
        if (oldLogChute instanceof HoldingLogChute)
        {
            HoldingLogChute hlc = (HoldingLogChute)oldLogChute;
            hlc.transferTo(newLogChute);
        }

        // check whether or not we should print the stack traces of
        // Throwables passed *as* messages (rather than as Throwables).
        boolean showStacks = <commit_msg>make sure that a non LogSystem/non LogChute object passed into the LogManager is not just silently ignored but logged at warn level (might even be error).<commit_after>package org.apache.velocity.runtime.log;

/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.util.ClassUtils;

/**
 * <p>
 * This class is responsible for instantiating the correct LogChute
 * </p>
 *
 * <p>
 * The approach is :
 * </p>
 * <ul>
 * <li> 
 *      First try to see if the user is passing in a living object
 *      that is a LogChute, allowing the app to give its living
 *      custom loggers.
 *  </li>
 *  <li> 
 *       Next, run through the (possible) list of classes specified
 *       specified as loggers, taking the first one that appears to 
 *       work.  This is how we support finding logkit, log4j or
 *       jdk logging, whichever is in the classpath and found first, 
 *       as all three are listed as defaults.
 *  </li>
 *  <li>
 *      Finally, we turn to the System.out stream and print log messages
 *      to it if nothing else works.
 *  </li>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME>.</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class LogManager
{
    /**
     *  Creates a new logging system or returns an existing one
     *  specified by the application.
     */
    public static LogChute createLogChute(RuntimeServices rsvc) throws Exception
    {
        Log log = rsvc.getLog();

        /*
         *  if a logSystem was set as a configuation value, use that. 
         *  This is any class the user specifies.
         */
        Object o = rsvc.getProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM );

        if (o != null)
        {
            // first check for a LogChute
            if (o instanceof LogChute)
            {
                ((LogChute)o).init(rsvc);
                return (LogChute)o;
            }
            // then check for a LogSystem
            else if (o instanceof LogSystem)
            {
                // wrap the LogSystem into a chute.
                LogChute chute = new LogChuteSystem((LogSystem)o);
                chute.init(rsvc);
                // warn the user about the deprecation
                chute.log(LogChute.WARN_ID, 
                          "LogSystem has been deprecated. Please use a LogChute implementation.");
                return chute;
            }
            else
            {
                log.warn(o.getClass().getName() + " object passed in as log implementation which is not supported.");
            }
        }
  
        /* otherwise, see if a class was specified.  You can put multiple 
         * classes, and we use the first one we find.
         *
         * Note that the default value of this property contains the
         * AvalonLogChute, the Log4JLogChute, and the JdkLogChute for 
         * convenience - so we use whichever we find first.
         */
        List classes = new ArrayList();
        Object obj = rsvc.getProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS );

        /*
         *  we might have a list, or not - so check
         */
        if ( obj instanceof List)
        {
            classes = (List) obj;
        }
        else if ( obj instanceof String)
        { 
            classes.add( obj );
        }

        /*
         *  now run through the list, trying each.  It's ok to 
         *  fail with a class not found, as we do this to also
         *  search out a default simple file logger
         */
        for( Iterator ii = classes.iterator(); ii.hasNext(); )
        {
            String claz = (String) ii.next();
            if (claz != null && claz.length() > 0 )
            {
                log.info("Trying to use logger class " + claz );
                try
                {
                    o = ClassUtils.getNewInstance( claz );
                    if (o instanceof LogChute)
                    {
                        ((LogChute)o).init(rsvc);
                        log.info("Using logger class " + claz);
                        return (LogChute)o;
                    }
                    else if (o instanceof LogSystem)
                    {
                        LogChute chute = new LogChuteSystem((LogSystem)o);
                        chute.init(rsvc);
                        // warn the user about the deprecation
                        chute.log(LogChute.WARN_ID, 
                                  "LogSystem has been deprecated. Please use a LogChute implementation.");
                        return chute;
                    }
                    else
                    {
                        log.error("The specifid logger class " + claz +
                                  " isn't a valid LogChute");
                    }
                }
                catch( NoClassDefFoundError ncdfe )
                {
                    log.debug("Couldn't find class " + claz 
                              + " or necessary supporting classes in "
                              + "classpath. Exception : " + ncdfe);
                }
            }
        }
      
        /* If the above failed, that means either the user specified a 
         * logging class that we can't find, there weren't the necessary
         * dependencies in the classpath for it, or there were the same
         * problems for the default loggers, log4j and Java1.4+.
         * Since we really don't know and we want to be sure the user knows
         * that something went wrong with the logging, let's fall back to the 
         * surefire StandardOutLogChute. No panicking or failing to log!!
         */
        LogChute sls = new StandardOutLogChute();
        sls.init(rsvc);
        log.info("Using StandardOutLogChute.");
        return sls;
    }

    /**
     * Update the Log instance with the appropriate LogChute and other
     * settings determined by the RuntimeServices.
     */
    public static void updateLog(Log log, RuntimeServices rsvc) throws Exception
    {
        // create a new LogChute using the RuntimeServices
        LogChute newLogChute = createLogChute(rsvc);
        LogChute oldLogChute = log.getLogChute();

        // If the old LogChute was the pre-Init logger,
        // dump its messages into the new system first.
        if (oldLogChute instanceof HoldingLogChute)
        {
            HoldingLogChute hlc = (HoldingLogChute)oldLogChute;
            hlc.transferTo(newLogChute);
        }

        // check whether or not we should print the stack traces of
        // Throwables passed *as* messages (rather than as Throwables).
        boolean showStacks = 
{'fraction_non_alphanumeric': 0.08052001089028042, 'fraction_numerical': 0.002450313095562211, 'mean_word_length': 1.6574425755109423, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7388510', 'n_tokens_mistral': 3944, 'n_tokens_neox': 3775, 'n_words': 1405}