Skip to content

Commit 3ee88cc

Browse files
tristandunncrmne
andauthored
Add support for custom logger via configuration (#225)
Fixes #223. --------- Co-authored-by: Carmine Paolino <carmine@paolino.me>
1 parent 45040db commit 3ee88cc

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

docs/configuration.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ RubyLLM.configure do |config|
8686
# config.http_proxy = "socks5://proxy.company.com:1080" # SOCKS5 proxy
8787

8888
# --- Logging Settings ---
89+
# config.logger = Rails.logger # When set the file and level settings are not used.
8990
config.log_file = '/logs/ruby_llm.log'
9091
config.log_level = :debug # debug level can also be set to debug by setting RUBYLLM_DEBUG envar to true
9192
config.log_assume_model_exists = false # Silence "Assuming model exists for provider" warning
@@ -158,13 +159,16 @@ Fine-tune how RubyLLM handles HTTP connections and retries.
158159
Adjust these based on network conditions and provider reliability.
159160

160161
## Logging Settings
161-
RubyLLM provides flexible logging configuration to help you monitor and debug API interactions. You can configure both the log file location and the logging level.
162+
RubyLLM provides flexible logging configuration to help you monitor and debug API interactions. You can configure both the log file location and the logging level, or set a custom logger.
162163

163164
```ruby
164165
RubyLLM.configure do |config|
165166
# --- Logging Settings ---
166167
config.log_file = '/logs/ruby_llm.log' # Path to log file (default: nil, logs to STDOUT)
167168
config.log_level = :debug # Log level (:debug, :info, :warn)
169+
170+
# --- Custom Logger ---
171+
config.logger = Rails.logger
168172
end
169173
```
170174

@@ -181,6 +185,14 @@ end
181185

182186
You can also set the debug level by setting the `RUBYLLM_DEBUG` environment variable to `true`.
183187

188+
### Custom Logger
189+
190+
* `config.logger`: Specifies a custom `Logger` for where logs should be written.
191+
192+
{: .note }
193+
If you set a customer logger the `config.log_file` and `config.log_level`
194+
settings are not used.
195+
184196
## Scoped Configuration with Contexts
185197

186198
While `RubyLLM.configure` sets global defaults, `RubyLLM.context` allows you to create temporary, isolated configuration scopes for specific API calls. This is ideal for situations requiring different keys, endpoints, or timeouts temporarily without affecting the rest of the application.

lib/ruby_llm.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def config
6767
end
6868

6969
def logger
70-
@logger ||= Logger.new(
70+
@logger ||= config.logger || Logger.new(
7171
config.log_file,
7272
progname: 'RubyLLM',
7373
level: config.log_level

lib/ruby_llm/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Configuration
3636
:retry_interval_randomness,
3737
:http_proxy,
3838
# Logging configuration
39+
:logger,
3940
:log_file,
4041
:log_level,
4142
:log_assume_model_exists

spec/ruby_llm_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe RubyLLM do
6+
describe '.logger' do
7+
let(:logger) { instance_double(Logger) }
8+
let(:log_file) { double }
9+
let(:log_level) { double }
10+
11+
before do
12+
described_class.instance_variable_set(:@config, nil)
13+
described_class.instance_variable_set(:@logger, nil)
14+
end
15+
16+
after do
17+
described_class.instance_variable_set(:@config, nil)
18+
described_class.instance_variable_set(:@logger, nil)
19+
end
20+
21+
context 'with configuration options' do
22+
before do
23+
described_class.configure do |config|
24+
config.log_file = log_file
25+
config.log_level = log_level
26+
end
27+
end
28+
29+
it 'returns a default Logger' do
30+
allow(Logger).to receive(:new).with(log_file, progname: 'RubyLLM', level: log_level).and_return(logger)
31+
32+
expect(described_class.logger).to eq(logger)
33+
end
34+
end
35+
36+
context 'with a custom logger' do
37+
before do
38+
described_class.configure do |config|
39+
config.logger = logger
40+
config.log_file = log_file
41+
config.log_level = log_level
42+
end
43+
end
44+
45+
it 'returns a the custom Logger' do
46+
expect(described_class.logger).to eq(logger)
47+
end
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)