Skip to content
7 changes: 6 additions & 1 deletion docs/_getting_started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ RubyLLM.configure do |config|

# Or use Rails logger
config.logger = Rails.logger # Overrides log_file and log_level

# Set custom timeout for log filtering
# which is useful when working with large payloads
config.log_regexp_timeout = 4 # seconds
Comment on lines +205 to +208
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not categorise this as basic logging. Let's remove that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth having a separate section called "Advanced logging options" where to put also the logger and log file options. Since that requires a rewrite of the section, I'm willing to take that on myself.

end
```

Expand Down Expand Up @@ -354,6 +358,7 @@ RubyLLM.configure do |config|
config.log_file = String
config.log_level = Symbol
config.log_stream_debug = Boolean # v1.6.0+
config.log_regexp_timeout = Integer # v1.6.5+
end
```

Expand All @@ -363,4 +368,4 @@ Now that you've configured RubyLLM, you're ready to:

- [Start chatting with AI models]({% link _core_features/chat.md %})
- [Work with different providers and models]({% link _advanced/models.md %})
- [Set up Rails integration]({% link _advanced/rails.md %})
- [Set up Rails integration]({% link _advanced/rails.md %})
4 changes: 3 additions & 1 deletion lib/ruby_llm/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Configuration
:logger,
:log_file,
:log_level,
:log_stream_debug
:log_stream_debug,
:log_regexp_timeout

def initialize
@request_timeout = 120
Expand All @@ -53,6 +54,7 @@ def initialize
@log_file = $stdout
@log_level = ENV['RUBYLLM_DEBUG'] ? Logger::DEBUG : Logger::INFO
@log_stream_debug = ENV['RUBYLLM_STREAM_DEBUG'] == 'true'
@log_regexp_timeout = Regexp.timeout
end

def instance_variables
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_llm/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def setup_logging(faraday)
errors: true,
headers: false,
log_level: :debug do |logger|
logger.filter(%r{[A-Za-z0-9+/=]{100,}}, 'data":"[BASE64 DATA]"')
logger.filter(/[-\d.e,\s]{100,}/, '[EMBEDDINGS ARRAY]')
logger.filter(Regexp.new('[A-Za-z0-9+/=]{100,}', timeout: @config.log_regexp_timeout), 'data":"[BASE64 DATA]"')
logger.filter(Regexp.new('[-\\d.e,\\s]{100,}', timeout: @config.log_regexp_timeout), '[EMBEDDINGS ARRAY]')
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/ruby_llm/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
# Get current config values
original_model = RubyLLM.config.default_model
original_api_key = RubyLLM.config.openai_api_key
original_log_regexp_timeout = RubyLLM.config.log_regexp_timeout

# Create context with modified config
context = RubyLLM.context do |config|
config.default_model = 'modified-model'
config.openai_api_key = 'modified-key'
config.log_regexp_timeout = 5.0
end

# Verify global config is unchanged
expect(RubyLLM.config.default_model).to eq(original_model)
expect(RubyLLM.config.openai_api_key).to eq(original_api_key)
expect(RubyLLM.config.log_regexp_timeout).to eq(original_log_regexp_timeout)

# Verify context has modified config
expect(context.config.default_model).to eq('modified-model')
expect(context.config.openai_api_key).to eq('modified-key')
expect(context.config.log_regexp_timeout).to eq(5.0)
end
end

Expand Down Expand Up @@ -95,6 +99,7 @@
it 'allows multiple contexts with different configurations' do
context1 = RubyLLM.context do |config|
config.default_model = 'gpt-4.1-nano'
config.log_regexp_timeout = 5.0
end

context2 = RubyLLM.context do |config|
Expand All @@ -105,7 +110,11 @@
chat2 = context2.chat

expect(chat1.model.id).to eq('gpt-4.1-nano')
expect(context1.config.log_regexp_timeout).not_to eq(Regexp.timeout)
expect(context1.config.log_regexp_timeout).to eq(5.0)

expect(chat2.model.id).to eq('claude-3-5-haiku-20241022')
expect(context2.config.log_regexp_timeout).to eq(Regexp.timeout)
end

it 'ensures changes in one context do not affect another' do
Expand Down
Loading