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