Skip to content

Commit 94c8485

Browse files
authored
Merge pull request #75 from launchdarkly/eb/ch19339/exception-logging
log exceptions at error level and stacktraces at debug level
2 parents f5092af + d73d66c commit 94c8485

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

lib/ldclient-rb/events.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def main_loop(queue, buffer, flush_workers)
142142
message.completed
143143
end
144144
rescue => e
145-
@config.logger.warn { "[LDClient] Unexpected error in event processor: #{e.inspect}. \nTrace: #{e.backtrace}" }
145+
Util.log_exception(@config.logger, "Unexpected error in event processor", e)
146146
end
147147
end
148148
end
@@ -226,7 +226,7 @@ def trigger_flush(buffer, flush_workers)
226226
resp = EventPayloadSendTask.new.run(@sdk_key, @config, @client, payload, @formatter)
227227
handle_response(resp) if !resp.nil?
228228
rescue => e
229-
@config.logger.warn { "[LDClient] Unexpected error in event processor: #{e.inspect}. \nTrace: #{e.backtrace}" }
229+
Util.log_exception(@config.logger, "Unexpected error in event processor", e)
230230
end
231231
end
232232
buffer.clear if success # Reset our internal state, these events now belong to the flush worker

lib/ldclient-rb/ldclient.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def variation(key, user, default)
162162
@event_processor.add_event(make_feature_event(feature, user, res[:variation], value, default))
163163
return value
164164
rescue => exn
165-
@config.logger.warn { "[LDClient] Error evaluating feature flag: #{exn.inspect}. \nTrace: #{exn.backtrace}" }
165+
Util.log_exception(@config.logger, "Error evaluating feature flag", exn)
166166
@event_processor.add_event(make_feature_event(feature, user, nil, default, default))
167167
return default
168168
end
@@ -210,7 +210,7 @@ def all_flags(user)
210210
# TODO rescue if necessary
211211
Hash[features.map{ |k, f| [k, evaluate(f, user, @store, @config.logger)[:value]] }]
212212
rescue => exn
213-
@config.logger.warn { "[LDClient] Error evaluating all flags: #{exn.inspect}. \nTrace: #{exn.backtrace}" }
213+
Util.log_exception(@config.logger, "Error evaluating all flags", exn)
214214
return Hash.new
215215
end
216216
end
@@ -226,12 +226,6 @@ def close
226226
@store.stop
227227
end
228228

229-
def log_exception(caller, exn)
230-
error_traceback = "#{exn.inspect} #{exn}\n\t#{exn.backtrace.join("\n\t")}"
231-
error = "[LDClient] Unexpected exception in #{caller}: #{error_traceback}"
232-
@config.logger.error { error }
233-
end
234-
235229
def sanitize_user(user)
236230
if user[:key]
237231
user[:key] = user[:key].to_s
@@ -252,7 +246,7 @@ def make_feature_event(flag, user, variation, value, default)
252246
}
253247
end
254248

255-
private :evaluate, :log_exception, :sanitize_user, :make_feature_event
249+
private :evaluate, :sanitize_user, :make_feature_event
256250
end
257251

258252
#

lib/ldclient-rb/util.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
module LaunchDarkly
33
module Util
4+
def self.log_exception(logger, message, exc)
5+
logger.error { "[LDClient] #{message}: #{exc.inspect}" }
6+
logger.debug { "[LDClient] Exception trace: #{exc.backtrace}" }
7+
end
8+
49
def self.http_error_recoverable?(status)
510
if status >= 400 && status < 500
611
status == 400 || status == 408 || status == 429

spec/ldclient_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,6 @@ def event_processor
130130
end
131131
end
132132

133-
describe '#log_exception' do
134-
it "log error data" do
135-
expect(client.instance_variable_get(:@config).logger).to receive(:error)
136-
begin
137-
raise StandardError.new 'asdf'
138-
rescue StandardError => exn
139-
client.send(:log_exception, 'caller', exn)
140-
end
141-
end
142-
end
143-
144133
describe 'with send_events: false' do
145134
let(:config) { LaunchDarkly::Config.new({offline: true, send_events: false, update_processor: update_processor}) }
146135
let(:client) { subject.new("secret", config) }

spec/util_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "spec_helper"
2+
3+
describe LaunchDarkly::Util do
4+
describe 'log_exception' do
5+
let(:logger) { double() }
6+
7+
it "logs error data" do
8+
expect(logger).to receive(:error)
9+
expect(logger).to receive(:debug)
10+
begin
11+
raise StandardError.new 'asdf'
12+
rescue StandardError => exn
13+
LaunchDarkly::Util.log_exception(logger, "message", exn)
14+
end
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)