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.PrintWriter;
020    import java.io.StringWriter;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Message;
024    import org.apache.camel.StreamCache;
025    import org.apache.camel.spi.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 showException;
040        private boolean showCaughtException;
041        private boolean showStackTrace;
042        private boolean showAll;
043        private boolean multiline;
044        private int maxChars;
045    
046        public Object format(Exchange exchange) {
047            Message in = exchange.getIn();
048    
049            StringBuilder sb = new StringBuilder("");
050            if (showAll || showExchangeId) {
051                if (multiline) {
052                    sb.append('\n');
053                }
054                sb.append(", Id:").append(exchange.getExchangeId());
055            }
056            if (showAll || showProperties) {
057                if (multiline) {
058                    sb.append('\n');
059                }
060                sb.append(", Properties:").append(exchange.getProperties());
061            }
062            if (showAll || showHeaders) {
063                if (multiline) {
064                    sb.append('\n');
065                }
066                sb.append(", Headers:").append(in.getHeaders());
067            }
068            if (showAll || showBodyType) {
069                if (multiline) {
070                    sb.append('\n');
071                }
072                sb.append(", BodyType:").append(getBodyTypeAsString(in));
073            }
074            if (showAll || showBody) {
075                if (multiline) {
076                    sb.append('\n');
077                }
078                sb.append(", Body:").append(getBodyAsString(in));
079            }
080    
081            if (showAll || showException || showCaughtException) {
082    
083                // try exception on exchange first
084                Exception exception = exchange.getException();
085                boolean caught = false;
086                if (showCaughtException && exception == null) {
087                    // fallback to caught exception
088                    exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
089                    caught = true;
090                }
091    
092                if (exception != null) {
093                    if (multiline) {
094                        sb.append('\n');
095                    }
096                    if (caught) {
097                        sb.append(", CaughtExceptionType:").append(exception.getClass().getCanonicalName());
098                        sb.append(", CaughtExceptionMessage:").append(exception.getMessage());
099                    } else {
100                        sb.append(", ExceptionType:").append(exception.getClass().getCanonicalName());
101                        sb.append(", ExceptionMessage:").append(exception.getMessage());
102                    }
103                    if (showAll || showStackTrace) {
104                        StringWriter sw = new StringWriter();
105                        exception.printStackTrace(new PrintWriter(sw));
106                        sb.append(", StackTrace:").append(sw.toString());
107                    }
108                }
109            }
110    
111            if (showAll || showOut) {
112                if (exchange.hasOut()) {
113                    Message out = exchange.getOut();
114                    if (showAll || showHeaders) {
115                        if (multiline) {
116                            sb.append('\n');
117                        }
118                        sb.append(", OutHeaders:").append(out.getHeaders());
119                    }
120                    if (showAll || showBodyType) {
121                        if (multiline) {
122                            sb.append('\n');
123                        }
124                        sb.append(", OutBodyType:").append(getBodyTypeAsString(out));
125                    }
126                    if (showAll || showBody) {
127                        if (multiline) {
128                            sb.append('\n');
129                        }
130                        sb.append(", OutBody:").append(getBodyAsString(out));
131                    }
132                } else {
133                    if (multiline) {
134                        sb.append('\n');
135                    }
136                    sb.append(", Out: null");
137                }
138            }
139    
140            if (maxChars > 0) {
141                StringBuilder answer = new StringBuilder();
142                for (String s : sb.toString().split("\n")) {
143                    if (s != null) {
144                        if (s.length() > maxChars) {
145                            s = s.substring(0, maxChars);
146                            answer.append(s).append("...");
147                        } else {
148                            answer.append(s);
149                        }
150                        if (multiline) {
151                            answer.append("\n");
152                        }
153                    }
154                }
155    
156                // get rid of the leading space comma if needed
157                return "Exchange[" + (multiline ? answer.append(']').toString() : answer.toString().substring(2) + "]");
158            }
159    
160            // get rid of the leading space comma if needed
161            return "Exchange[" + (multiline ? sb.append(']').toString() : sb.toString().substring(2) + "]");
162        }
163    
164        public boolean isShowExchangeId() {
165            return showExchangeId;
166        }
167    
168        public void setShowExchangeId(boolean showExchangeId) {
169            this.showExchangeId = showExchangeId;
170        }
171    
172        public boolean isShowProperties() {
173            return showProperties;
174        }
175    
176        public void setShowProperties(boolean showProperties) {
177            this.showProperties = showProperties;
178        }
179    
180        public boolean isShowHeaders() {
181            return showHeaders;
182        }
183    
184        public void setShowHeaders(boolean showHeaders) {
185            this.showHeaders = showHeaders;
186        }
187    
188        public boolean isShowBodyType() {
189            return showBodyType;
190        }
191    
192        public void setShowBodyType(boolean showBodyType) {
193            this.showBodyType = showBodyType;
194        }
195    
196        public boolean isShowBody() {
197            return showBody;
198        }
199    
200        public void setShowBody(boolean showBody) {
201            this.showBody = showBody;
202        }
203    
204        public boolean isShowOut() {
205            return showOut;
206        }
207    
208        public void setShowOut(boolean showOut) {
209            this.showOut = showOut;
210        }
211    
212        public boolean isShowAll() {
213            return showAll;
214        }
215    
216        public void setShowAll(boolean showAll) {
217            this.showAll = showAll;
218        }
219    
220        public boolean isShowException() {
221            return showException;
222        }
223    
224        public void setShowException(boolean showException) {
225            this.showException = showException;
226        }
227    
228        public boolean isShowStackTrace() {
229            return showStackTrace;
230        }
231    
232        public void setShowStackTrace(boolean showStackTrace) {
233            this.showStackTrace = showStackTrace;
234        }
235    
236        public boolean isShowCaughtException() {
237            return showCaughtException;
238        }
239    
240        public void setShowCaughtException(boolean showCaughtException) {
241            this.showCaughtException = showCaughtException;
242        }
243    
244        public boolean isMultiline() {
245            return multiline;
246        }
247    
248        public int getMaxChars() {
249            return maxChars;
250        }
251    
252        public void setMaxChars(int maxChars) {
253            this.maxChars = maxChars;
254        }
255    
256        /**
257         * If enabled then each information is outputted on a newline.
258         */
259        public void setMultiline(boolean multiline) {
260            this.multiline = multiline;
261        }
262    
263        // Implementation methods
264        //-------------------------------------------------------------------------
265        protected Object getBodyAsString(Message message) {
266            StreamCache newBody = message.getBody(StreamCache.class);
267            if (newBody != null) {
268                message.setBody(newBody);
269            }
270    
271            Object answer = message.getBody(String.class);
272            if (answer == null) {
273                answer = message.getBody();
274            }
275    
276            if (newBody != null) {
277                // Reset the StreamCache
278                newBody.reset();
279            }
280            return answer;
281        }
282    
283        protected Object getBodyTypeAsString(Message message) {
284            String answer = ObjectHelper.classCanonicalName(message.getBody());
285            if (answer != null && answer.startsWith("java.lang.")) {
286                return answer.substring(10);
287            }
288            return answer;
289        }
290    
291    }