001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.log;
018    
019    import java.util.Locale;
020    import java.util.Map;
021    
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.LoggingLevel;
024    import org.apache.camel.Processor;
025    import org.apache.camel.impl.DefaultComponent;
026    import org.apache.camel.processor.CamelLogProcessor;
027    import org.apache.camel.processor.ThroughputLogger;
028    import org.apache.camel.util.CamelLogger;
029    import org.apache.camel.util.IntrospectionSupport;
030    
031    /**
032     * The <a href="http://camel.apache.org/log.html">Log Component</a>
033     * to log message exchanges to the underlying logging mechanism.
034     *
035     * @version 
036     */
037    public class LogComponent extends DefaultComponent {
038    
039        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
040            LoggingLevel level = getLoggingLevel(parameters);
041            String marker = getAndRemoveParameter(parameters, "marker", String.class);
042            Integer groupSize = getAndRemoveParameter(parameters, "groupSize", Integer.class);
043            Long groupInterval = getAndRemoveParameter(parameters, "groupInterval", Long.class);
044    
045            CamelLogger camelLogger = new CamelLogger(remaining, level, marker);
046            Processor logger;
047            if (groupSize != null) {
048                logger = new ThroughputLogger(camelLogger, groupSize);
049            } else if (groupInterval != null) {
050                Boolean groupActiveOnly = getAndRemoveParameter(parameters, "groupActiveOnly", Boolean.class, Boolean.TRUE);
051                Long groupDelay = getAndRemoveParameter(parameters, "groupDelay", Long.class);
052                logger = new ThroughputLogger(camelLogger, this.getCamelContext(), groupInterval, groupDelay, groupActiveOnly);
053            } else {
054                LogFormatter formatter = new LogFormatter();
055                IntrospectionSupport.setProperties(formatter, parameters);
056    
057                logger = new CamelLogProcessor(camelLogger, formatter);
058            }
059    
060            LogEndpoint endpoint = new LogEndpoint(uri, this);
061            setProperties(endpoint, parameters);
062            return new LogEndpoint(uri, this, logger);
063        }
064    
065        /**
066         * Gets the logging level, will default to use INFO if no level parameter provided.
067         */
068        protected LoggingLevel getLoggingLevel(Map<String, Object> parameters) {
069            String levelText = getAndRemoveParameter(parameters, "level", String.class, "INFO");
070            return LoggingLevel.valueOf(levelText.toUpperCase(Locale.ENGLISH));
071        }
072    
073    }