Skip to content
Browse files

Embellish logger facility per recent discussion.

  • Loading branch information...
1 parent 0b083ea commit 55e51fde18a0375536c638df62f383c5919bf6d6 @gmallard gmallard committed May 22, 2011
Showing with 46 additions and 10 deletions.
  1. +9 −1 examples/logexamp.rb
  2. +33 −7 examples/slogger.rb
  3. +4 −2 lib/stomp/connection.rb
View
10 examples/logexamp.rb
@@ -23,10 +23,18 @@
{:login => user, :passcode => password, :host => host, :port => port},
],
:logger => mylog,
+ :max_reconnect_attempts => 5,
}
# //////////////////////////////////////////////////////////////////////////////
conn = Stomp::Connection.new(hash)
conn.disconnect
# //////////////////////////////////////////////////////////////////////////////
-llog.debug "Done"
+llog.debug "Connection processing complete"
+# //////////////////////////////////////////////////////////////////////////////
+conn = Stomp::Client.new(hash)
+conn.close
+# //////////////////////////////////////////////////////////////////////////////
+llog.debug "Client processing complete"
+# //////////////////////////////////////////////////////////////////////////////
+llog.debug "Ending"
View
40 examples/slogger.rb
@@ -4,7 +4,9 @@
Optional callback methods
- sl_connect: successful connect
+ sl_connecting: connection starting
+ sl_connected: successful connect
+ sl_connectfail: unsuccessful connect (will usually be retried)
sl_disconnect: successful disconnect
Note: call back logging methods *must* not raise exceptions, otherwise the
@@ -14,24 +16,48 @@
require 'logger'
class Slogger
#
- def initialize(*parms)
+ def initialize(init_parms = nil)
@log = Logger::new(STDOUT)
@log.level = Logger::DEBUG
# @log.debug("Logger initialization complete.")
end
- # Log connect events
- def sl_connect(*parms)
+ # Log connecting events
+ def sl_connecting(parms)
begin
- @log.debug("Connected.")
+ # parms: A copy of the Connection's @parameters instance variable (a Hash)
+ curr_host = parms[:hosts][0]
+ @log.debug("Connecting.... " + curr_host[:host] + ":" + curr_host[:port].to_s)
+ rescue
+ end
+ end
+
+ # Log connected events
+ def sl_connected(parms)
+ begin
+ # parms: A copy of the Connection's @parameters instance variable (a Hash)
+ curr_host = parms[:hosts][0]
+ @log.debug("Connected.... " + curr_host[:host] + ":" + curr_host[:port].to_s)
+ rescue
+ end
+ end
+
+ # Log connectfail events
+ def sl_connectfail(parms)
+ begin
+ # parms: A copy of the Connection's @parameters instance variable (a Hash)
+ curr_host = parms[:hosts][0]
+ @log.debug("Connect Fail.... " + curr_host[:host] + ":" + curr_host[:port].to_s)
rescue
end
end
# Log disconnect events
- def sl_disconnect(*parms)
+ def sl_disconnect(parms)
begin
- @log.debug("Disconnected.")
+ # parms: A copy of the Connection's @parameters instance variable (a Hash)
+ curr_host = parms[:hosts][0]
+ @log.debug("Disconnected.... " + curr_host[:host] + ":" + curr_host[:port].to_s)
rescue
end
end
View
6 lib/stomp/connection.rb
@@ -99,6 +99,7 @@ def hashed_initialize(params)
@logger = @parameters[:logger]
#sets the first host to connect
change_host
+ @logger.sl_connecting(@parameters.clone) if @logger && @logger.respond_to?(:sl_connecting)
end
# Syntactic sugar for 'Connection.new' See 'initialize' for usage.
@@ -118,12 +119,13 @@ def socket
# Open complete
connect(used_socket)
- @logger.sl_connect(nil) if @logger && @logger.respond_to?(:sl_connect)
+ @logger.sl_connected(@parameters.clone) if @logger && @logger.respond_to?(:sl_connected)
@connection_attempts = 0
rescue
@failure = $!
used_socket = nil
raise unless @reliable
+ @logger.sl_connectfail(@parameters.clone) if @logger && @logger.respond_to?(:sl_connectfail)
$stderr.print "connect to #{@host} failed: #{$!} will retry(##{@connection_attempts}) in #{@reconnect_delay}\n"
raise Stomp::Error::MaxReconnectAttempts if max_reconnect_attempts?
@@ -313,7 +315,7 @@ def disconnect(headers = {})
transmit("DISCONNECT", headers)
headers = headers.symbolize_keys
@disconnect_receipt = receive if headers[:receipt]
- @logger.sl_disconnect(nil) if @logger && @logger.respond_to?(:sl_disconnect)
+ @logger.sl_disconnect(@parameters.clone) if @logger && @logger.respond_to?(:sl_disconnect)
close_socket
end

0 comments on commit 55e51fd

Please sign in to comment.
Something went wrong with that request. Please try again.