Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Active Support Logger in Addition to Custom Logger in Rails 7.1 x BroadcastLogger Class in Development environment #51812

Closed
buck4chuka opened this issue May 14, 2024 · 1 comment

Comments

@buck4chuka
Copy link

Steps to reproduce

  # Custom logger
     class CustomLogger < ActiveSupport::Logger
    include MessageSerializer

    def info(*msg, &block)
      value = as_hash(msg[0], msg[1], &block)
      super(value, &nil)
    end
    
    config.logger =  ActiveSupport::BroadcastLogger.new(CustomLogger.new)
# Your reproduction script goes here

Expected behavior

Rails.logger.info("message",foo: "foo", bar: "bar")

Should be able to take variable arguments without throwing an error.
Works as expected in production

Actual behavior

Rails.logger.info("message",foo: "foo", bar: "bar")

In local it fails with
wrong number of arguments (given 2, expected 0..1)

I tracked this down to the fact that in local development,
The Broadcast Logger is instantiated with two sinks even when I want only one sink.

Rails.logger.broadcasts =>

[#<CustomLogger:0x0000000119402468>
#<ActiveSupport::Logger:0x000000011fdf8b10>]

bundle exec rails s --no-log-to-stdout does remove the ActiveSupport::Logger but this behaviour is unexpected in my opinion.
If I add a customer logger I expect the Broadcast logger to contain only the sinks that I explicitly included

System configuration

Rails version:
7.1.3
Ruby version:
3.2.2

@rafaelfranca
Copy link
Member

Thank you for the issue.

This is the default behavior of the framework and we have no intention to change it.

rails s in development without --no-log-to-stdout will add a new logger to the broadcast unless the broadcast is already configured to log to stdout.

If you want, you can make you CustomLogger to report it is logging to STDOUT. See

def self.logger_outputs_to?(logger, *sources)
loggers = if logger.is_a?(BroadcastLogger)
logger.broadcasts
else
[logger]
end
logdevs = loggers.map { |logger| logger.instance_variable_get(:@logdev) }
logger_sources = logdevs.filter_map { |logdev| logdev.try(:filename) || logdev.try(:dev) }
normalize_sources(sources).intersect?(normalize_sources(logger_sources))
end

@rafaelfranca rafaelfranca closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants