Disable rails log for ActionCable events
Asked Answered
T

4

15

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?

Trophic answered 14/7, 2016 at 5:21 Comment(0)
A
18

To completely disable logging from ActionCable you should configure ActionCable to use logger that do nothing

ActionCable.server.config.logger = Logger.new(nil)
Anglicism answered 18/2, 2017 at 13:45 Comment(2)
To clarify, this line goes in config/environments/development.rbPiotrowski
I"m still seeing: │ │ Started GET "/cable?...." for 1.2.3.4 at 2022-07-10 03:19:09 +0000Debit
D
3

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.

Dempster answered 24/8, 2016 at 15:5 Comment(1)
This didn't helped... I've searched source code and found that action cable have many function that use logging... not only dispatch_action...Anglicism
J
0

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.

Jolynjolynn answered 27/6, 2023 at 17:1 Comment(0)
P
0

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.

Practically answered 17/11, 2023 at 19:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.