skip to content

Since Rails 7.1, Rails.logger is an ActiveSupport::BroadcastLogger; how do you add a second log sink in production, and what traps come with it?

level: seniorimportance: should knowfreq 24%

answer

  1. Rails 7.1 replaced Logger.broadcast
  2. broadcast_to and stop_broadcasting_to
  3. production writes to STDOUT
  4. level returns the lowest sink's level
  5. tags live in the formatter

basics

~20 s

Call Rails.logger.broadcast_to(sink) in an initializer and every line also goes to that sink. Give the sink the app's formatter so request-id tags appear, set its level, and remember that Rails.logger.level reports the lowest level of any sink.

solid answer

~40 s

Since 7.1, boot wraps whatever `config.logger` is in an `ActiveSupport::BroadcastLogger`. In a new app's production that is `ActiveSupport::TaggedLogging.logger(STDOUT)`, because containers collect STDOUT. `Rails.logger.broadcast_to(sink)` adds a destination, `stop_broadcasting_to` removes one, and `broadcasts` lists them. The framework loggers hold the same object, so they follow too. The traps: the request tags live in the tagged formatter, so a new sink with its own formatter prints lines without `[request_id]`. Share it with `sink.formatter = Rails.logger.formatter`, as `bin/rails server` does for its STDOUT sink. A new logger starts at debug, and `Rails.logger.level` returns the **lowest** level of any sink. Writes are synchronous, so a slow sink slows requests. The pre-7.1 `ActiveSupport::Logger.broadcast` extension is gone.

go deeper

for a junior

Recall that Rails.logger forwards to several sinks, that production logs to STDOUT, and that broadcast_to adds a sink.

for a middle

Explain how boot wraps config.logger in a BroadcastLogger and how bin/rails server adds a STDOUT sink with the app's formatter in development.

for a senior

Add a sink without losing tags, without dropping the effective level to debug and without putting slow I/O on the request path.

for a principal

Decide whether extra copies belong in the app at all or in the platform's log pipeline, weighing coupling, latency and retention.

## What changed in Rails 7.1 Before 7.1, sending logs to two places meant extending one logger with `ActiveSupport::Logger.broadcast(other)`. That method was private and has been removed. Since 7.1, **`Rails.logger` is always an `ActiveSupport::BroadcastLogger`**. During boot, the `initialize_logger` initializer takes `config.logger`, or builds a file logger when none is set, applies `config.log_level`, and wraps it: ```ruby Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger) ``` The `BroadcastLogger` keeps a list of sinks and forwards the standard logger calls (`info`, `debug`, `add`, `level=`, `formatter` and the rest) to them. Its own API is small: | Method | Effect | |---|---| | `broadcast_to(*loggers)` | Add one or more sinks | | `stop_broadcasting_to(logger)` | Remove a sink | | `broadcasts` | The current sinks, for example to reach a custom logger you configured | | `level=` | Set the level on every sink | | `level` | Return the lowest level of all sinks | Active Record, Action Controller and the other frameworks set their `logger` to `Rails.logger`, which is this same object. So a sink added in an initializer receives framework lines too. Any other method, such as `tagged` or `push_tags`, is forwarded only to the sinks that respond to it. ## Where Rails points it by default - **Production.** The generated `production.rb` sets `config.logger = ActiveSupport::TaggedLogging.logger(STDOUT)` with `log_tags [:request_id]`. Containers and process managers collect a process's standard output, so the app never manages log files or rotation inside an image. - **Development and test.** Without `config.logger`, Rails writes `log/development.log` or `log/test.log`. Under `load_defaults 7.1` and later, `config.log_file_size` caps these local files at 100 MB. - **`bin/rails server` in development.** When the logger does not already write to STDOUT or STDERR, the server command builds an `ActiveSupport::Logger.new(STDOUT)`, copies `Rails.logger.formatter` and `Rails.logger.level` onto it, and adds it with `broadcast_to`. You see the same tagged lines in the terminal and in the file. The `--log-to-stdout` option controls this, and it is on by default in development when the server is not daemonized. ## Adding a sink in production Say an electronics store must keep a local copy of payment request logs for an audit, alongside STDOUT. Follow the server command's pattern: ```ruby # config/initializers/audit_log.rb audit = ActiveSupport::Logger.new("log/payments_audit.log") audit.formatter = Rails.logger.formatter # the tagged formatter, which holds the tag stack audit.level = :info Rails.logger.broadcast_to(audit) ``` Each of the middle two lines avoids a trap. ## The traps 1. **Lost tags.** `Rails::Rack::Logger` pushes `[request_id]` onto the tagged formatter's per-thread tag stack. A new `ActiveSupport::Logger` has its own `SimpleFormatter`, which knows nothing of that stack, so its lines arrive **without the tag**. Sharing `Rails.logger.formatter` fixes it. 2. **A second tagged sink.** Wrapping the new sink in `TaggedLogging` as well looks tidier, but it is a separate tag stack. The broadcast forwards `push_tags` and `tagged` to every sink that responds, so each sink then receives these calls separately, and a block given to `Rails.logger.tagged` is yielded by each tagged sink. Keep one tagged sink and share its formatter. 3. **The level.** A fresh logger starts at debug. `BroadcastLogger#level` returns the lowest sink level, and `debug?` is true when **any** sink accepts debug. So `Rails.logger.debug { ... }` blocks start running, and that sink collects every SQL statement with its binds. 4. **Blocking writes.** Each line is written to every sink in the request's thread. A sink on slow storage, or one that makes a network call per line, adds that latency to every request. 5. **Duplicates.** A second STDOUT sink prints every line twice. `ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDOUT)` tells you whether one is already there. The server command makes the same check. ## When not to add a sink An extra sink inside the app is not always the right tool. If the platform already collects STDOUT, a second copy is often better made there: the log pipeline can route payment lines to a separate store without the app holding a file handle, a disk or a network connection. Keep in-app sinks for cases the platform cannot serve, such as a test capturing output into a `StringIO`, or a short-lived debugging session on one machine. ## Removing a sink `stop_broadcasting_to(audit)` removes a sink at runtime. Tests use it to capture output temporarily without leaving the extra sink behind. At least one sink must remain, or lines go nowhere. ## Takeaway The broadcast design makes an extra destination a one-line change, but each sink is a full logger with its own level and formatter, and request tags live in the formatter. Set both on purpose, keep a single tagged sink, and keep STDOUT as the production default that the platform collects.

  • You configured `config.logger = MyJsonLogger.new(STDOUT)`. How do you reach that instance later, since Rails.logger is now a BroadcastLogger?
    Use `Rails.logger.broadcasts`, which returns the wrapped sinks, here `[MyJsonLogger]`. Custom methods also work through `Rails.logger` itself. The broadcast forwards an unknown method to the sinks that respond and returns that sink's value, or an array when several respond.
  • Why does Rails production write logs to STDOUT instead of log/production.log?
    The generated production.rb sets `config.logger = ActiveSupport::TaggedLogging.logger(STDOUT)` because apps usually run in containers, where the runtime collects standard output. The app never handles file paths, disk space or rotation inside an ephemeral image, and the platform's log tooling takes over from there.
  • How does `bin/rails server` in development show tagged lines in the terminal when its STDOUT logger is a plain ActiveSupport::Logger?
    The server command copies `Rails.logger.formatter` onto that STDOUT logger before calling `broadcast_to`. The formatter is the tagged one that holds the per-thread tag stack, so both sinks format lines with the same tags. Only one sink responds to `push_tags`, so tags are pushed once.

  • Rails.logger in 7.1 and later is a plain ActiveSupport::Logger with an extra method.
  • A new Logger added with broadcast_to prints the request-id tag with its default formatter.
  • Rails.logger.level returns the level of the first sink you configured.
  • Broadcast sinks are written on a background thread, so slow sinks cost nothing.
  • Use ActiveSupport::Logger.broadcast to add a sink in a Rails 8.1 app.
  • A new sink inherits Rails' formatter and log level automatically.

Assumes Rails 8.1; Rails.logger has been an ActiveSupport::BroadcastLogger since 7.1, which removed ActiveSupport::Logger.broadcast.