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 */ 017package org.apache.activemq.transport.logwriters; 018 019import java.io.IOException; 020 021import org.apache.activemq.command.BaseCommand; 022import org.apache.activemq.command.ConnectionInfo; 023import org.apache.activemq.command.Message; 024import org.apache.activemq.command.MessageAck; 025import org.apache.activemq.command.MessageDispatch; 026import org.apache.activemq.command.ProducerAck; 027import org.apache.activemq.command.ProducerId; 028import org.apache.activemq.command.WireFormatInfo; 029import org.apache.activemq.transport.LogWriter; 030import org.slf4j.Logger; 031 032/** 033 * Custom implementation of LogWriter interface. 034 * 035 * @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com 036 * 037 */ 038public class CustomLogWriter implements LogWriter { 039 040 @Override 041 public void setPrefix(String prefix) { 042 // for the custom case, revert to the logger per connection 043 } 044 045 // doc comment inherited from LogWriter 046 public void initialMessage(Logger log) { 047 048 } 049 050 // doc comment inherited from LogWriter 051 public void logRequest (Logger log, Object command) { 052 log.debug("$$ SENDREQ: " + CustomLogWriter.commandToString(command)); 053 } 054 055 // doc comment inherited from LogWriter 056 public void logResponse (Logger log, Object response) { 057 log.debug("$$ GOT_RESPONSE: "+response); 058 } 059 060 // doc comment inherited from LogWriter 061 public void logAsyncRequest (Logger log, Object command) { 062 log.debug("$$ SENDING_ASNYC_REQUEST: "+command); 063 } 064 065 // doc comment inherited from LogWriter 066 public void logOneWay (Logger log, Object command) { 067 log.debug("$$ SENDING: " + CustomLogWriter.commandToString(command)); 068 } 069 070 // doc comment inherited from LogWriter 071 public void logReceivedCommand (Logger log, Object command) { 072 log.debug("$$ RECEIVED: " + CustomLogWriter.commandToString(command)); 073 } 074 075 // doc comment inherited from LogWriter 076 public void logReceivedException (Logger log, IOException error) { 077 log.debug("$$ RECEIVED_EXCEPTION: "+error, error); 078 } 079 080 /** 081 * Transforms a command into a String 082 * @param command An object (hopefully of the BaseCommand class or subclass) 083 * to be transformed into String. 084 * @return A String which will be written by the CustomLogWriter. 085 * If the object is not a BaseCommand, the String 086 * "Unrecognized_object " + command.toString() 087 * will be returned. 088 */ 089 private static String commandToString(Object command) { 090 StringBuilder sb = new StringBuilder(); 091 092 if (command instanceof BaseCommand) { 093 094 BaseCommand bc = (BaseCommand)command; 095 sb.append(command.getClass().getSimpleName()); 096 sb.append(' '); 097 sb.append(bc.isResponseRequired() ? 'T' : 'F'); 098 099 100 Message m = null; 101 102 if (bc instanceof Message) { 103 m = (Message)bc; 104 } 105 if (bc instanceof MessageDispatch){ 106 m = ((MessageDispatch)bc).getMessage(); 107 } 108 109 if (m != null) { 110 sb.append(' '); 111 sb.append(m.getMessageId()); 112 sb.append(','); 113 sb.append(m.getCommandId()); 114 ProducerId pid = m.getProducerId(); 115 long sid = pid.getSessionId(); 116 sb.append(','); 117 sb.append(pid.getConnectionId()); 118 sb.append(','); 119 sb.append(sid); 120 sb.append(','); 121 sb.append(pid.getValue()); 122 sb.append(','); 123 sb.append(m.getCorrelationId()); 124 sb.append(','); 125 sb.append(m.getType()); 126 } 127 128 if (bc instanceof MessageDispatch){ 129 sb.append(" toConsumer:"); 130 sb.append(((MessageDispatch)bc).getConsumerId()); 131 } 132 133 if (bc instanceof ProducerAck) { 134 sb.append(" ProducerId:"); 135 sb.append(((ProducerAck)bc).getProducerId()); 136 } 137 138 if (bc instanceof MessageAck) { 139 MessageAck ma = (MessageAck)bc; 140 sb.append(" ConsumerID:"); 141 sb.append(ma.getConsumerId()); 142 sb.append(" ack:"); 143 sb.append(ma.getFirstMessageId()); 144 sb.append('-'); 145 sb.append(ma.getLastMessageId()); 146 } 147 148 if (bc instanceof ConnectionInfo) { 149 ConnectionInfo ci = (ConnectionInfo)bc; 150 151 sb.append(' '); 152 sb.append(ci.getConnectionId()); 153 } 154 155 } else if (command instanceof WireFormatInfo){ 156 sb.append("WireFormatInfo"); 157 158 } else { 159 sb.append("Unrecognized_object "); 160 sb.append(command.toString()); 161 } 162 163 return sb.toString(); 164 } 165 166}