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