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.io.InputStream;
020
021 import org.apache.camel.Exchange;
022 import org.apache.camel.Message;
023 import org.apache.camel.converter.stream.StreamCache;
024 import org.apache.camel.processor.interceptor.ExchangeFormatter;
025 import org.apache.camel.util.ObjectHelper;
026
027 /**
028 * Log formatter to format the logging output.
029 */
030 public class LogFormatter implements ExchangeFormatter {
031
032 private boolean showExchangeId;
033 private boolean showProperties;
034 private boolean showHeaders;
035 private boolean showBodyType = true;
036 private boolean showBody = true;
037 private boolean showOut;
038 private boolean showAll;
039 private boolean multiline;
040
041 public Object format(Exchange exchange) {
042 Message in = exchange.getIn();
043
044 StringBuilder sb = new StringBuilder("");
045 if (showAll || showExchangeId) {
046 if (multiline) {
047 sb.append('\n');
048 }
049 sb.append(", Id:").append(exchange.getExchangeId());
050 }
051 if (showAll || showProperties) {
052 if (multiline) {
053 sb.append('\n');
054 }
055 sb.append(", Properties:").append(exchange.getProperties());
056 }
057 if (showAll || showHeaders) {
058 if (multiline) {
059 sb.append('\n');
060 }
061 sb.append(", Headers:").append(in.getHeaders());
062 }
063 if (showAll || showBodyType) {
064 if (multiline) {
065 sb.append('\n');
066 }
067 sb.append(", BodyType:").append(getBodyTypeAsString(in));
068 }
069 if (showAll || showBody) {
070 if (multiline) {
071 sb.append('\n');
072 }
073 sb.append(", Body:").append(getBodyAsString(in));
074 }
075
076 Message out = exchange.getOut(false);
077 if (showAll || showOut) {
078 if (out != null) {
079 if (showAll || showHeaders) {
080 if (multiline) {
081 sb.append('\n');
082 }
083 sb.append(", OutHeaders:").append(out.getHeaders());
084 }
085 if (showAll || showBodyType) {
086 if (multiline) {
087 sb.append('\n');
088 }
089 sb.append(", OutBodyType:").append(getBodyTypeAsString(out));
090 }
091 if (showAll || showBody) {
092 if (multiline) {
093 sb.append('\n');
094 }
095 sb.append(", OutBody:").append(getBodyAsString(out));
096 }
097 } else {
098 if (multiline) {
099 sb.append('\n');
100 }
101 sb.append(", Out: null");
102 }
103 }
104
105 // get rid of the leading space comma if needed
106 return "Exchange[" + (multiline ? sb.append(']').toString() : sb.toString().substring(2) + "]");
107 }
108
109 public boolean isShowExchangeId() {
110 return showExchangeId;
111 }
112
113 public void setShowExchangeId(boolean showExchangeId) {
114 this.showExchangeId = showExchangeId;
115 }
116
117 public boolean isShowProperties() {
118 return showProperties;
119 }
120
121 public void setShowProperties(boolean showProperties) {
122 this.showProperties = showProperties;
123 }
124
125 public boolean isShowHeaders() {
126 return showHeaders;
127 }
128
129 public void setShowHeaders(boolean showHeaders) {
130 this.showHeaders = showHeaders;
131 }
132
133 public boolean isShowBodyType() {
134 return showBodyType;
135 }
136
137 public void setShowBodyType(boolean showBodyType) {
138 this.showBodyType = showBodyType;
139 }
140
141 public boolean isShowBody() {
142 return showBody;
143 }
144
145 public void setShowBody(boolean showBody) {
146 this.showBody = showBody;
147 }
148
149 public boolean isShowOut() {
150 return showOut;
151 }
152
153 public void setShowOut(boolean showOut) {
154 this.showOut = showOut;
155 }
156
157 public boolean isShowAll() {
158 return showAll;
159 }
160
161 public void setShowAll(boolean showAll) {
162 this.showAll = showAll;
163 }
164
165 public boolean isMultiline() {
166 return multiline;
167 }
168
169 /**
170 * If enabled then each information is outputted on a newline.
171 */
172 public void setMultiline(boolean multiline) {
173 this.multiline = multiline;
174 }
175
176 // Implementation methods
177 //-------------------------------------------------------------------------
178 protected Object getBodyAsString(Message message) {
179
180 StreamCache newBody = message.getBody(StreamCache.class);
181 if (newBody != null) {
182 message.setBody(newBody);
183 }
184 Object answer = message.getBody(String.class);
185 if (answer == null) {
186 answer = message.getBody();
187 }
188 if (newBody != null) {
189 // Reset the StreamCache
190 newBody.reset();
191 }
192 return answer;
193 }
194
195 protected Object getBodyTypeAsString(Message message) {
196 String answer = ObjectHelper.classCanonicalName(message.getBody());
197 if (answer != null && answer.startsWith("java.lang.")) {
198 return answer.substring(10);
199 }
200 return answer;
201 }
202
203 }