Add log_bodies, use the context logger in streaming, filter API keys from logs
Closes #1669.
Problem
At debug level, RubyLLM logs every request and response body and every Faraday exception. Gateways and OpenAI-compatible servers sometimes quote the API key back in an error body, so an app that turns on debug logging to troubleshoot can write credentials to its log sink. The only way to keep bodies out was to wrap the logger so debug? returns false.
Streaming also wrote to the global RubyLLM.logger, so a logger set on a context didn't cover it, and the "Accumulating error chunk" line logged raw provider error bodies without checking log_stream_debug.
What changes
config.log_bodies. Defaults tonil, which keeps today's behaviour (logger.debug?), so nobody's logs change on upgrade. Set it tofalseto stay at debug level without logging bodies. This covers error bodies too, since Faraday logs them only when bodies are on, the "Stream completed" line, which carries the whole answer, and the stream's parse-failure lines, whoseJSON::ParserErrormessages quote the text. Exception messages that quote the provider stay with the existingredact_error_messages.errors: truestays as it is.- Context logger in streaming.
Protocol::Streaming, the Bedrock event stream, Converse and Gemini streaming, andStreamAccumulatornow log through the protocol's configuration (config.logger || RubyLLM.logger), the same wayTransport::Connection#settings_fordoes, and readlog_stream_debugfrom that configuration rather than the global one. - Error chunks gated. "Accumulating error chunk", and its Bedrock counterpart, log only with
log_stream_debug, like the other chunk lines. - Configured API keys filtered. Every
*_key,*_secretand*_tokenoption set on the context is replaced with[FILTERED]in Faraday log lines (URL, bodies and exception messages) and in the streaming debug lines. There's no new option.
How the key filter reaches a shared connection
The Faraday connection, and so the logger middleware and its formatter, is cached and shared across contexts. A filter added when the connection is built would apply one context's keys to another context's requests. Alternatively it would need the keys in the Settings cache key, and so one connection per credential set.
So Connection#instrument_request sets the configuration in fiber storage around each whole request and restores the previous value afterwards (LogFormatter.with_config, the same pattern as with_fallback_ready). A request made inside a stream callback therefore leaves the outer request's filter in place. LogFormatter#debug, which every Faraday line passes through, replaces the secrets, and it reads them only when a line is actually written. Filtering there rather than in apply_filters also covers exception messages on Faraday 2.12 and earlier, which write exc.full_message without apply_filters.
If you'd rather put the secret values in Settings and add them as ordinary filter calls in setup_logging, that's a smaller change, and I'm happy to switch.
Connection.basic, which builds a connection per call for MCP over HTTP, MCP OAuth, Gemini uploads, Bedrock auth and downloads, now logs through the configuration's logger, and its LogFormatter is given that configuration directly. It never logs bodies, but it logs exception messages, which could echo a key.
StreamAccumulator takes a log: callable from the protocol rather than the configuration. Its stream-debug inspect line then doesn't dump the whole configuration on every chunk.
Specs
Configuration:log_bodiesdefaults tonil;secret_valueslists set credentials and ignores other options.Connection:log_bodies: falseturns bodies off at debug, andtrueturns them on above debug.log_bodiesseparates cached connections.LogFormatter: filters the request's keys from a response body, from an exception (skipped on Faraday versions that don't log exceptions, such as 1.x and 2.6), and from lines Faraday writes without its own filters. It keeps Faraday 1's progname, and a request made inside another leaves the outer configuration in place.LogFormatter.log_debugwrites to the configuration's logger with keys filtered.Connection: a real request (WebMock) logs a response body that echoes the key as[FILTERED]. A basic connection filters a key from an exception message it logs.Streaming: logs to the configuration's logger and not the global one. The completion and parse-failure lines followlog_bodies. Error chunks are silent withoutlog_stream_debug, and filtered with it.StreamAccumulator: logs through the callable it's given.
Each new spec fails on main. Existing streaming specs that extend modules onto a bare Object, or allocate a protocol, now give it a config, since the streaming modules read the protocol's configuration.
bundle exec rspec --tag ~live locally: 9,099 examples, and the same 90 failures as on main on the same machine. All 90 come from the machine itself: no Google application default credentials, no op-cache, and generator specs. The branch adds no new failures. The transport, protocol and configuration specs also pass under Faraday 1.10.3. RuboCop, ArchSpec, Flay and rake api:check pass.
Docs: log_bodies added to the logging section and the settings table in configuration-connection.md, with a note that raw chunks are still logged while log_stream_debug is on. [FILTERED] added to the redaction list. api.txt gains RubyLLM::Configuration#log_bodies.
Left out: the non-streaming RubyLLM.logger.warn lines in the protocols' chat.rb files still use the global logger. Happy to send those as a follow-up.
Summary by CodeRabbit
- New Features
- Added a
log_bodiessetting to control whether request and response bodies appear in debug logs. By default, body logging follows the logger’s debug level.
- Added a
- Bug Fixes
- Configured credentials are now filtered from logged request and response content, exceptions, and debug messages.
- Body logging can be disabled without suppressing raw stream chunks when stream debugging is enabled.