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.model;
018
019 import javax.xml.bind.annotation.XmlAccessType;
020 import javax.xml.bind.annotation.XmlAccessorType;
021 import javax.xml.bind.annotation.XmlAttribute;
022 import javax.xml.bind.annotation.XmlRootElement;
023
024 import org.apache.camel.Expression;
025 import org.apache.camel.LoggingLevel;
026 import org.apache.camel.Processor;
027 import org.apache.camel.processor.LogProcessor;
028 import org.apache.camel.spi.RouteContext;
029 import org.apache.camel.util.CamelLogger;
030 import org.apache.camel.util.ObjectHelper;
031
032 /**
033 * Represents an XML <log/> element
034 *
035 * @version
036 */
037 @XmlRootElement(name = "log")
038 @XmlAccessorType(XmlAccessType.FIELD)
039 public class LogDefinition extends NoOutputDefinition<LogDefinition> {
040 @XmlAttribute(required = true)
041 private String message;
042 @XmlAttribute
043 private LoggingLevel loggingLevel;
044 @XmlAttribute
045 private String logName;
046 @XmlAttribute
047 private String marker;
048
049 public LogDefinition() {
050 }
051
052 public LogDefinition(String message) {
053 this.message = message;
054 }
055
056 @Override
057 public String toString() {
058 return "Log[" + message + "]";
059 }
060
061 @Override
062 public String getLabel() {
063 return "log";
064 }
065
066 @Override
067 public String getShortName() {
068 return "log";
069 }
070
071 @Override
072 public Processor createProcessor(RouteContext routeContext) throws Exception {
073 ObjectHelper.notEmpty(message, "message", this);
074
075 // use simple language for the message string to give it more power
076 Expression exp = routeContext.getCamelContext().resolveLanguage("simple").createExpression(message);
077
078 String name = getLogName();
079 if (name == null) {
080 name = routeContext.getRoute().getId();
081 }
082 // should be INFO by default
083 LoggingLevel level = getLoggingLevel() != null ? getLoggingLevel() : LoggingLevel.INFO;
084 CamelLogger logger = new CamelLogger(name, level, getMarker());
085
086 return new LogProcessor(exp, logger);
087 }
088
089 @Override
090 public void addOutput(ProcessorDefinition<?> output) {
091 // add outputs on parent as this log does not support outputs
092 getParent().addOutput(output);
093 }
094
095 public LoggingLevel getLoggingLevel() {
096 return loggingLevel;
097 }
098
099 public void setLoggingLevel(LoggingLevel loggingLevel) {
100 this.loggingLevel = loggingLevel;
101 }
102
103 public String getMessage() {
104 return message;
105 }
106
107 public void setMessage(String message) {
108 this.message = message;
109 }
110
111 public String getLogName() {
112 return logName;
113 }
114
115 public void setLogName(String logName) {
116 this.logName = logName;
117 }
118
119 public String getMarker() {
120 return marker;
121 }
122
123 public void setMarker(String marker) {
124 this.marker = marker;
125 }
126 }