All logs for RABBITMQ will be sent to the topic type of Exchange "Amq.rabbitmq.log" Routingkey debug,info,waring,error. If you receive all types of logs, you can use #
Packagelogs;Importjava.io.IOException;Importjava.util.concurrent.TimeoutException;ImportCom.rabbitmq.client.Channel;Importcom.rabbitmq.client.Connection;ImportCom.rabbitmq.client.DefaultConsumer;ImportCom.rabbitmq.client.Envelope;Importcom.rabbitmq.client.AMQP.BasicProperties;ImportUtils. Channelutils; Public classListenerrabbitmqlogs {Private Static FinalString queue_name_debug = "Queue_debug"; Private Static FinalString queue_name_info = "Queue_info"; Private Static FinalString queue_name_warning = "Queue_warning"; Private Static FinalString queue_name_error = "Queue_error"; Private Static FinalString Exchange_name_log = "Amq.rabbitmq.log"; Public Static voidMain (string[] args)throwsIOException, timeoutexception {Connection Connection= Channelutils.getconnection ("Listenerlog"); Channel Channeldebug=Connection.createchannel (); Channel Channelinfo=Connection.createchannel (); Channel channelwarning=Connection.createchannel (); Channel Channelerror=Connection.createchannel (); Channeldebug.queuedelete (Queue_name_debug); Channeldebug.queuedeclare (Queue_name_debug,false,false,false,NULL); Channelinfo.queuedelete (Queue_name_info); Channelinfo.queuedeclare (Queue_name_info,false,false,false,NULL); Channelwarning.queuedelete (queue_name_warning); Channelwarning.queuedeclare (queue_name_warning,false,false,false,NULL); Channelerror.queuedelete (Queue_name_error); Channelerror.queuedeclare (Queue_name_error,false,false,false,NULL); Channeldebug.queuebind (Queue_name_debug, Exchange_name_log,"Debug"); Channelinfo.queuebind (Queue_name_info, Exchange_name_log,"Info"); Channelwarning.queuebind (queue_name_warning, Exchange_name_log,"Warning"); Channelerror.queuebind (Queue_name_error, Exchange_name_log,"Error"); Channeldebug.basicconsume (Queue_name_debug,NewLogsconsumer (Channeldebug, "Debug")); Channelinfo.basicconsume (Queue_name_info,NewLogsconsumer (Channelinfo, "Info")); Channelwarning.basicconsume (queue_name_warning,NewLogsconsumer (channelwarning, "Warning")); Channelerror.basicconsume (Queue_name_error,NewLogsconsumer (Channelerror, "Error")); }}classLogsconsumerextendsDefaultconsumer {PrivateString LogLevel; PublicLogsconsumer (Channel channel,string logLevel) {Super(channel); This. LogLevel =LogLevel; } @Override Public voidHandledelivery (String Consumertag, Envelope Envelope, basicproperties properties,byte[] body)throwsIOException {System.out.println (logLevel+":"+NewString (body)); //Use the manual confirmation mode, where you need to acknowledge receipt of the message. Getchannel (). Basicack (Envelope.getdeliverytag (),false); }}
PackageUtils;Importjava.io.IOException;Importjava.util.concurrent.TimeoutException;ImportCom.rabbitmq.client.Channel;Importcom.rabbitmq.client.Connection;Importcom.rabbitmq.client.ConnectionFactory; Public classChannelutils { Public StaticChannel getchannelinstance (String connectiondescription) {Try { returngetconnection (connectiondescription). CreateChannel (); } Catch(Exception e) {Throw NewRuntimeException ("Get channel Connection Failed"); } } Public StaticConnection getconnection (String connectiondescription)throwsIOException, timeoutexception {connectionfactory connectionfactory=getconnectionfactory (); returnconnectionfactory.newconnection (connectiondescription); } Public Staticconnectionfactory getconnectionfactory () {connectionfactory connectionfactory=NewConnectionFactory (); Connectionfactory.sethost ("192.168.1.111"); Connectionfactory.setport (5672); Connectionfactory.setvirtualhost ("/"); Connectionfactory.setusername ("DRS"); Connectionfactory.setpassword ("123456");
returnConnectionFactory; }}
A simple Java program for RABBITMQ log monitoring