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