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.UriEndpointComponent;
026    import org.apache.camel.processor.CamelLogProcessor;
027    import org.apache.camel.processor.DefaultExchangeFormatter;
028    import org.apache.camel.processor.ThroughputLogger;
029    import org.apache.camel.spi.ExchangeFormatter;
030    import org.apache.camel.util.CamelLogger;
031    
032    /**
033     * The <a href="http://camel.apache.org/log.html">Log Component</a>
034     * to log message exchanges to the underlying logging mechanism.
035     *
036     * @version 
037     */
038    public class LogComponent extends UriEndpointComponent {
039    
040        private ExchangeFormatter exchangeFormatter;
041    
042        public LogComponent() {
043            super(LogEndpoint.class);
044        }
045        
046        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
047            LoggingLevel level = getLoggingLevel(parameters);
048    
049            LogEndpoint endpoint = new LogEndpoint(uri, this);
050            endpoint.setLevel(level.name());
051            setProperties(endpoint, parameters);
052    
053            CamelLogger camelLogger = new CamelLogger(remaining, level, endpoint.getMarker());
054            Processor logger;
055            if (endpoint.getGroupSize() != null) {
056                logger = new ThroughputLogger(camelLogger, endpoint.getGroupSize());
057            } else if (endpoint.getGroupInterval() != null) {
058                Boolean groupActiveOnly = endpoint.getGroupActiveOnly() != null ? endpoint.getGroupActiveOnly() : Boolean.TRUE;
059                Long groupDelay = endpoint.getGroupDelay();
060                logger = new ThroughputLogger(camelLogger, this.getCamelContext(), endpoint.getGroupInterval(), groupDelay, groupActiveOnly);
061            } else {
062                // first, try to use the user-specified formatter (or the one picked up from the Registry and transferred to 
063                // the property by a previous endpoint initialisation); if null, try to pick it up from the Registry now
064                ExchangeFormatter localFormatter = exchangeFormatter;
065                if (localFormatter == null) {
066                    localFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter", ExchangeFormatter.class);
067                    if (localFormatter != null) {
068                        exchangeFormatter = localFormatter;
069                        setProperties(exchangeFormatter, parameters);
070                    }
071                }
072                // if no formatter is available in the Registry, create a local one of the default type, for a single use
073                if (localFormatter == null) {
074                    localFormatter = new DefaultExchangeFormatter();
075                    setProperties(localFormatter, parameters);
076                }
077                logger = new CamelLogProcessor(camelLogger, localFormatter);
078            }
079    
080            endpoint.setLogger(logger);
081            return endpoint;
082        }
083    
084        /**
085         * Gets the logging level, will default to use INFO if no level parameter provided.
086         */
087        protected LoggingLevel getLoggingLevel(Map<String, Object> parameters) {
088            String levelText = getAndRemoveParameter(parameters, "level", String.class, "INFO");
089            return LoggingLevel.valueOf(levelText.toUpperCase(Locale.ENGLISH));
090        }
091    
092        public ExchangeFormatter getExchangeFormatter() {
093            return exchangeFormatter;
094        }
095    
096        /**
097         * Sets a custom {@link ExchangeFormatter} to convert the Exchange to a String suitable for logging.
098         * <p />
099         * If not specified, we default to {@link DefaultExchangeFormatter}.
100         */
101        public void setExchangeFormatter(ExchangeFormatter exchangeFormatter) {
102            this.exchangeFormatter = exchangeFormatter;
103        }
104    
105    }