Skip to content

Commit e5b556a

Browse files
rhys117crmne
andcommitted
Add support for logging to file via configuration (#148)
## Purpose Introduces some basic configuration for the logging capabilities, allowing users to specify a custom log file and log level. This feature enhances debugging and monitoring capabilities by providing more control over where and how logs are recorded. ## Implementation Details - Added log_file configuration option (default: STDOUT) - Added log_level configuration option (default: INFO, or DEBUG if RUBYLLM_DEBUG env var is set) - Updated logger initialisation to use the configured log file and level - Added documentation for logging configuration - Maintained backward compatibility with existing logging behaviour ## Usage Example ```ruby # Global configuration RubyLLM.configure do |config| config.log_file = '/logs/ruby_llm.log' # Custom log file location config.log_level = :debug # Set log level end ``` ## Testing Manual - Verified logger initialisation with custom log file - Confirmed log level changes based on configuration - Tested environment variable override for debug level - Validated default behaviour (STDOUT logging) ## Documentation Added a new section in configuration.md for logging settings Co-authored-by: Carmine Paolino <carmine@paolino.me>
1 parent faf737d commit e5b556a

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

docs/configuration.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ After reading this guide, you will know:
2727
* How to customize connection timeouts and retries.
2828
* How to connect to custom endpoints (like Azure OpenAI).
2929
* How to use temporary, scoped configurations with `RubyLLM.context`.
30+
* How to configure the logging location.
3031

3132
## Global Configuration (`RubyLLM.configure`)
3233

@@ -75,6 +76,10 @@ RubyLLM.configure do |config|
7576
config.retry_interval = 0.1 # Initial delay in seconds (default: 0.1)
7677
config.retry_backoff_factor = 2 # Multiplier for subsequent retries (default: 2)
7778
config.retry_interval_randomness = 0.5 # Jitter factor (default: 0.5)
79+
80+
# --- Logging Settings ---
81+
config.log_file = '/logs/ruby_llm.log'
82+
config.level = :debug # debug level can also be set to debug by setting RUBYLLM_DEBUG envar to true
7883
end
7984
```
8085

@@ -134,6 +139,31 @@ Fine-tune how RubyLLM handles HTTP connections and retries.
134139

135140
Adjust these based on network conditions and provider reliability.
136141

142+
## Logging Settings
143+
144+
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.
145+
146+
```ruby
147+
RubyLLM.configure do |config|
148+
# --- Logging Settings ---
149+
config.log_file = '/logs/ruby_llm.log' # Path to log file (default: nil, logs to STDOUT)
150+
config.level = :debug # Log level (:debug, :info, :warn)
151+
end
152+
```
153+
154+
### Log File Configuration
155+
156+
* `config.log_file`: Specifies the path where logs should be written. If not set, logs will be written to STDOUT.
157+
* The log file will be created if it doesn't exist, and logs will be appended to it.
158+
159+
### Log Levels
160+
161+
* `:debug`: Most verbose level, includes detailed request/response information as provided by the faraday client
162+
* `:info`: General operational information
163+
* `:warn`: Warning messages for non-critical issues that may need attention
164+
165+
You can also set the debug level by setting the `RUBYLLM_DEBUG` environment variable to `true`.
166+
137167
## Scoped Configuration with Contexts
138168
{: .d-inline-block }
139169

@@ -178,4 +208,5 @@ default_response = default_chat.ask("Query using global production settings...")
178208
* **Isolation:** Modifying configuration within a context block does **not** affect the global `RubyLLM.config`.
179209
* **Thread Safety:** Each context is independent, making them safe for use across different threads.
180210

181-
Contexts provide a clean and safe mechanism for managing diverse configuration needs within a single application.
211+
Contexts provide a clean and safe mechanism for managing diverse configuration needs within a single application.
212+

lib/ruby_llm.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def config
6868

6969
def logger
7070
@logger ||= Logger.new(
71-
$stdout,
71+
config.log_file,
7272
progname: 'RubyLLM',
73-
level: ENV['RUBYLLM_DEBUG'] ? Logger::DEBUG : Logger::INFO
73+
level: config.log_level
7474
)
7575
end
7676
end

lib/ruby_llm/configuration.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class Configuration
3131
:max_retries,
3232
:retry_interval,
3333
:retry_backoff_factor,
34-
:retry_interval_randomness
34+
:retry_interval_randomness,
35+
# Logging configuration
36+
:log_file,
37+
:log_level
3538

3639
def initialize
3740
# Connection configuration
@@ -45,6 +48,10 @@ def initialize
4548
@default_model = 'gpt-4.1-nano'
4649
@default_embedding_model = 'text-embedding-3-small'
4750
@default_image_model = 'dall-e-3'
51+
52+
# Logging configuration
53+
@log_file = $stdout
54+
@log_level = ENV['RUBYLLM_DEBUG'] ? Logger::DEBUG : Logger::INFO
4855
end
4956

5057
def inspect # rubocop:disable Metrics/MethodLength

0 commit comments

Comments
 (0)