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 org.apache.camel.Exchange;
020 import org.apache.camel.Message;
021 import org.apache.camel.StreamCache;
022 import org.apache.camel.spi.ExchangeFormatter;
023 import org.apache.camel.util.ObjectHelper;
024
025 /**
026 * Log formatter to format the logging output.
027 */
028 public class LogFormatter implements ExchangeFormatter {
029
030 private boolean showExchangeId;
031 private boolean showProperties;
032 private boolean showHeaders;
033 private boolean showBodyType = true;
034 private boolean showBody = true;
035 private boolean showOut;
036 private boolean showException;
037 private boolean showAll;
038 private boolean multiline;
039 private int maxChars;
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 if (exchange.getException() != null && (showAll || showException)) {
076 if (multiline) {
077 sb.append('\n');
078 }
079 sb.append(", Exception:").append(exchange.getException().getMessage());
080 }
081
082 if (showAll || showOut) {
083 if (exchange.hasOut()) {
084 Message out = exchange.getOut();
085 if (showAll || showHeaders) {
086 if (multiline) {
087 sb.append('\n');
088 }
089 sb.append(", OutHeaders:").append(out.getHeaders());
090 }
091 if (showAll || showBodyType) {
092 if (multiline) {
093 sb.append('\n');
094 }
095 sb.append(", OutBodyType:").append(getBodyTypeAsString(out));
096 }
097 if (showAll || showBody) {
098 if (multiline) {
099 sb.append('\n');
100 }
101 sb.append(", OutBody:").append(getBodyAsString(out));
102 }
103 } else {
104 if (multiline) {
105 sb.append('\n');
106 }
107 sb.append(", Out: null");
108 }
109 }
110
111 if (maxChars > 0) {
112 StringBuilder answer = new StringBuilder();
113 for (String s : sb.toString().split("\n")) {
114 if (s != null) {
115 if (s.length() > maxChars) {
116 s = s.substring(0, maxChars);
117 answer.append(s).append("...");
118 } else {
119 answer.append(s);
120 }
121 if (multiline) {
122 answer.append("\n");
123 }
124 }
125 }
126
127 // get rid of the leading space comma if needed
128 return "Exchange[" + (multiline ? answer.append(']').toString() : answer.toString().substring(2) + "]");
129 }
130
131 // get rid of the leading space comma if needed
132 return "Exchange[" + (multiline ? sb.append(']').toString() : sb.toString().substring(2) + "]");
133 }
134
135 public boolean isShowExchangeId() {
136 return showExchangeId;
137 }
138
139 public void setShowExchangeId(boolean showExchangeId) {
140 this.showExchangeId = showExchangeId;
141 }
142
143 public boolean isShowProperties() {
144 return showProperties;
145 }
146
147 public void setShowProperties(boolean showProperties) {
148 this.showProperties = showProperties;
149 }
150
151 public boolean isShowHeaders() {
152 return showHeaders;
153 }
154
155 public void setShowHeaders(boolean showHeaders) {
156 this.showHeaders = showHeaders;
157 }
158
159 public boolean isShowBodyType() {
160 return showBodyType;
161 }
162
163 public void setShowBodyType(boolean showBodyType) {
164 this.showBodyType = showBodyType;
165 }
166
167 public boolean isShowBody() {
168 return showBody;
169 }
170
171 public void setShowBody(boolean showBody) {
172 this.showBody = showBody;
173 }
174
175 public boolean isShowOut() {
176 return showOut;
177 }
178
179 public void setShowOut(boolean showOut) {
180 this.showOut = showOut;
181 }
182
183 public boolean isShowAll() {
184 return showAll;
185 }
186
187 public void setShowAll(boolean showAll) {
188 this.showAll = showAll;
189 }
190
191 public boolean isShowException() {
192 return showException;
193 }
194
195 public void setShowException(boolean showException) {
196 this.showException = showException;
197 }
198
199 public boolean isMultiline() {
200 return multiline;
201 }
202
203 public int getMaxChars() {
204 return maxChars;
205 }
206
207 public void setMaxChars(int maxChars) {
208 this.maxChars = maxChars;
209 }
210
211 /**
212 * If enabled then each information is outputted on a newline.
213 */
214 public void setMultiline(boolean multiline) {
215 this.multiline = multiline;
216 }
217
218 // Implementation methods
219 //-------------------------------------------------------------------------
220 protected Object getBodyAsString(Message message) {
221 StreamCache newBody = message.getBody(StreamCache.class);
222 if (newBody != null) {
223 message.setBody(newBody);
224 }
225
226 Object answer = message.getBody(String.class);
227 if (answer == null) {
228 answer = message.getBody();
229 }
230
231 if (newBody != null) {
232 // Reset the StreamCache
233 newBody.reset();
234 }
235 return answer;
236 }
237
238 protected Object getBodyTypeAsString(Message message) {
239 String answer = ObjectHelper.classCanonicalName(message.getBody());
240 if (answer != null && answer.startsWith("java.lang.")) {
241 return answer.substring(10);
242 }
243 return answer;
244 }
245
246 }