In development
environment, rails logger logs all ActionCable events which is sometimes annoying (for my project, every now and then it's transmitting messages and log tail is running like wild horse). What is an efficient way to suppress all event logs from ActionCable? Also, how can I suppress some specific ActionCable events?
To completely disable logging from ActionCable you should configure ActionCable to use logger that do nothing
ActionCable.server.config.logger = Logger.new(nil)
Same here, finally I found a solution. You should override a code in /app/channels/application_cable/channel.rb as follows
module ApplicationCable
class Channel < ActionCable::Channel::Base
def dispatch_action(action, data)
#logger.info action_signature(action, data)
if method(action).arity == 1
public_send action, data
else
public_send action
end
end
end
end
You can simulate the original behavior by uncommenting out #logger.info action_signature(action, data)
line.
Thanks.
To suppress all event logs from ActionCable, you can put in the configuration file for the environment you want (e.g. config/environments/development.rb):
config.action_cable.logger = Logger.new(nil)
Or if you prefer, in an initializer, like @Brazhnyk Yuriy suggests:
ActionCable.server.config.logger = Logger.new(nil)
IDK for specific ActionCable events, but if you are trying to supress logging of sensitive data, maybe you can use filter_parameters:
config.action_cable.filter_parameters = [:password]
which has recently been added to ActionCable, by this PR.
While this Answer and others gave us some insight into a way to turn logging completely on and off, they do not completely answer the question of how to exclude some actions or only when in some environments, from the OP's question.
Lets start with looking at the source method we are going to override:
def dispatch_action(action, data)
logger.debug action_signature(action, data)
if method(action).arity == 1
public_send action, data
else
public_send action
end
rescue Exception => exception
rescue_with_handler(exception) || raise
end
If we copy the original method and add a guard clause to the logging method, we preserve the original functionality as much as possible. In this case keeping the error handling that is dropped from the previous answer and the same logging level (debug vs info).
Something like this should do the trick:
# /app/channels/application_cable/channel.rb
def dispatch_action(action, data)
logger.debug action_signature(action, data) unless skip_logging_for(action:)
if method(action).arity == 1
public_send action, data
else
public_send action
end
rescue Exception => exception
rescue_with_handler(exception) || raise
end
here we can make the logic of the skip_logging_for(action:)
method what ever we need:
# /app/channels/application_cable/channel.rb
def skip_logging_for(action:)
case Rails.env.to_sym
when :development
action.in?(%i[ping custom_action])
# other env like staging or test could go here
else
action.in?(%i[custom_action])
end
end
USE WITH CAUTION: as other have mentioned there may be logging triggered by other methods and it can cause hard to troubleshoot problems when you start changing framework methods.
© 2022 - 2025 — McMap. All rights reserved.