Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve logging
  • Loading branch information
dhh committed Jun 19, 2015
1 parent 4ebd8ce commit 3759071
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
35 changes: 22 additions & 13 deletions lib/action_cable/channel/base.rb
Expand Up @@ -7,8 +7,6 @@ class Base
on_subscribe :start_periodic_timers
on_unsubscribe :stop_periodic_timers

on_unsubscribe :disconnect

attr_reader :params, :connection
delegate :logger, to: :connection

Expand All @@ -30,30 +28,34 @@ def initialize(connection, channel_identifier, params = {})
@_active_periodic_timers = []
@params = params

connect
run_subscribe_callbacks

logger.info "#{self.class.name} connected"
perform_connection
end

def perform_action(data)
if authorized?
action = extract_action(data)

if performable_action?(action)
logger.info "Processing #{compose_signature(action, data)}"
logger.info channel_name + compose_signature(action, data)
public_send action, data
else
logger.error "Failed to process #{compose_signature(action, data)}"
logger.error "#{channel_name} failed to process #{compose_signature(action, data)}"
end
else
unauthorized
end
end

def perform_disconnection
disconnect
run_unsubscribe_callbacks
logger.info "#{self.class.name} disconnected"
logger.info "#{channel_name} disconnected"
end

def perform_connection
logger.info "#{channel_name} connecting"
connect
run_subscribe_callbacks
end

protected
Expand All @@ -63,9 +65,10 @@ def authorized?
end

def unauthorized
logger.error "Unauthorized access to #{self.class.name}"
logger.error "#{channel_name}: Unauthorized access"
end


def connect
# Override in subclasses
end
Expand All @@ -74,15 +77,21 @@ def disconnect
# Override in subclasses
end


def broadcast(data)
if authorized?
logger.info "Broadcasting: #{data.inspect}"
logger.info "#{channel_name} broadcasting #{data.inspect}"
connection.broadcast({ identifier: @channel_identifier, message: data }.to_json)
else
unauthorized
end
end


def channel_name
self.class.name
end

private
def extract_action(data)
(data['action'].presence || :receive).to_sym
Expand All @@ -93,9 +102,9 @@ def performable_action?(action)
end

def compose_signature(action, data)
"#{self.class.name}##{action}".tap do |signature|
"##{action}".tap do |signature|
if (arguments = data.except('action')).any?
signature << ": #{arguments.inspect}"
signature << "(#{arguments.inspect})"
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/action_cable/channel/redis.rb
Expand Up @@ -13,15 +13,15 @@ def subscribe_to(redis_channel, callback = nil)
@_redis_channels ||= []
@_redis_channels << [ redis_channel, callback ]

logger.info "Subscribing to the redis channel: #{redis_channel}"
pubsub.subscribe(redis_channel, &callback)
logger.info "#{channel_name} subscribed to incoming actions from #{redis_channel}"
end

def unsubscribe_from_all_channels
if @_redis_channels
@_redis_channels.each do |channel, callback|
logger.info "Unsubscribing from the redis channel: #{channel}"
pubsub.unsubscribe_proc(channel, callback)
@_redis_channels.each do |redis_channel, callback|
pubsub.unsubscribe_proc(redis_channel, callback)
logger.info "#{channel_name} unsubscribed from incoming actions #{redis_channel}"
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/action_cable/connection/base.rb
Expand Up @@ -136,7 +136,6 @@ def subscribe_channel(data)
subscription_klass = server.registered_channels.detect { |channel_klass| channel_klass.find_name == id_options[:channel] }

if subscription_klass
logger.info "Subscribing to channel: #{id_key}"
@subscriptions[id_key] = subscription_klass.new(self, id_key, id_options)
else
logger.error "Subscription class not found (#{data.inspect})"
Expand Down

0 comments on commit 3759071

Please sign in to comment.