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 */ 017package org.apache.camel.reifier; 018 019import java.util.Map; 020 021import org.apache.camel.Exchange; 022import org.apache.camel.Expression; 023import org.apache.camel.ExtendedCamelContext; 024import org.apache.camel.LoggingLevel; 025import org.apache.camel.Processor; 026import org.apache.camel.model.LogDefinition; 027import org.apache.camel.model.ProcessorDefinition; 028import org.apache.camel.processor.LogProcessor; 029import org.apache.camel.spi.CamelLogger; 030import org.apache.camel.spi.MaskingFormatter; 031import org.apache.camel.spi.RouteContext; 032import org.apache.camel.support.CamelContextHelper; 033import org.apache.camel.support.processor.DefaultMaskingFormatter; 034import org.apache.camel.util.ObjectHelper; 035import org.apache.camel.util.StringHelper; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039public class LogReifier extends ProcessorReifier<LogDefinition> { 040 041 public LogReifier(ProcessorDefinition<?> definition) { 042 super((LogDefinition)definition); 043 } 044 045 @Override 046 public Processor createProcessor(RouteContext routeContext) throws Exception { 047 StringHelper.notEmpty(definition.getMessage(), "message", this); 048 049 // use simple language for the message string to give it more power 050 Expression exp = routeContext.getCamelContext().resolveLanguage("simple").createExpression(definition.getMessage()); 051 052 // get logger explicitely set in the definition 053 Logger logger = definition.getLogger(); 054 055 // get logger which may be set in XML definition 056 if (logger == null && ObjectHelper.isNotEmpty(definition.getLoggerRef())) { 057 logger = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), definition.getLoggerRef(), Logger.class); 058 } 059 060 if (logger == null) { 061 // first - try to lookup single instance in the registry, just like 062 // LogComponent 063 Map<String, Logger> availableLoggers = routeContext.lookupByType(Logger.class); 064 if (availableLoggers.size() == 1) { 065 logger = availableLoggers.values().iterator().next(); 066 log.debug("Using custom Logger: {}", logger); 067 } else if (availableLoggers.size() > 1) { 068 // we should log about this somewhere... 069 log.debug("More than one {} instance found in the registry. Falling back to create logger by name.", Logger.class.getName()); 070 } 071 } 072 073 if (logger == null) { 074 String name = definition.getLogName(); 075 if (name == null) { 076 name = routeContext.getCamelContext().getGlobalOption(Exchange.LOG_EIP_NAME); 077 if (name != null) { 078 log.debug("Using logName from CamelContext properties: {}", name); 079 } 080 } 081 if (name == null) { 082 name = routeContext.getRouteId(); 083 log.debug("LogName is not configured, using route id as logName: {}", name); 084 } 085 logger = LoggerFactory.getLogger(name); 086 } 087 088 // should be INFO by default 089 LoggingLevel level = definition.getLoggingLevel() != null ? definition.getLoggingLevel() : LoggingLevel.INFO; 090 CamelLogger camelLogger = new CamelLogger(logger, level, definition.getMarker()); 091 092 return new LogProcessor(exp, camelLogger, getMaskingFormatter(routeContext), routeContext.getCamelContext().adapt(ExtendedCamelContext.class).getLogListeners()); 093 } 094 095 private MaskingFormatter getMaskingFormatter(RouteContext routeContext) { 096 if (routeContext.isLogMask()) { 097 MaskingFormatter formatter = routeContext.lookup(MaskingFormatter.CUSTOM_LOG_MASK_REF, MaskingFormatter.class); 098 if (formatter == null) { 099 formatter = new DefaultMaskingFormatter(); 100 } 101 return formatter; 102 } 103 return null; 104 } 105 106}