Slightly better logging

Stephen Paul Weber created

Import structured logging gem ougai and replace current use of puts/warn with
logging calls.  Gives us level, timestamp, and formatting for exceptions and/or
hash of arbitrary context data.  Can do JSON logging if we ever need that, but
set to human-readable (with colour if isatty) for now.

Change summary

Gemfile             |  2 ++
lib/registration.rb |  2 +-
sgx_jmp.rb          | 16 ++++++++++++----
3 files changed, 15 insertions(+), 5 deletions(-)

Detailed changes

Gemfile 🔗

@@ -2,6 +2,7 @@
 
 source "https://rubygems.org"
 
+gem "amazing_print"
 gem "blather", git: "https://github.com/singpolyma/blather.git", branch: "ergonomics"
 gem "braintree"
 gem "dhall"
@@ -12,6 +13,7 @@ gem "em-synchrony"
 gem "em_promise.rb", "~> 0.0.2"
 gem "eventmachine"
 gem "money-open-exchange-rates"
+gem "ougai"
 gem "ruby-bandwidth-iris"
 gem "sentry-ruby"
 gem "statsd-instrument", git: "https://github.com/singpolyma/statsd-instrument.git", branch: "graphite"

lib/registration.rb 🔗

@@ -482,7 +482,7 @@ class Registration
 
 		def customer_active_tel_purchased
 			@customer.register!(@tel).catch { |e|
-				puts e
+				LOG.error "@customer.register! failed", e
 				raise_setup_error
 			}.then {
 				EMPromise.all([

sgx_jmp.rb 🔗

@@ -9,11 +9,20 @@ require "date"
 require "dhall"
 require "em-hiredis"
 require "em_promise"
+require "ougai"
 require "ruby-bandwidth-iris"
 require "sentry-ruby"
 require "statsd-instrument"
 
 $stdout.sync = true
+LOG = Ougai::Logger.new($stdout)
+LOG.level = ENV.fetch("LOG_LEVEL", "info")
+LOG.formatter = Ougai::Formatters::Readable.new(
+	nil,
+	nil,
+	plain: !$stdout.isatty
+)
+LOG.info "Starting"
 
 Sentry.init
 
@@ -103,9 +112,7 @@ end
 BRAINTREE = AsyncBraintree.new(**CONFIG[:braintree])
 
 def panic(e, hub=nil)
-	m = e.respond_to?(:message) ? e.message : e
-	warn "Error raised during event loop: #{e.class}: #{m}"
-	warn e.backtrace if e.respond_to?(:backtrace)
+	LOG.fatal "Error raised during event loop: #{e.class}", e
 	if e.is_a?(::Exception)
 		(hub || Sentry).capture_exception(e, hint: { background: false })
 	else
@@ -117,6 +124,7 @@ end
 EM.error_handler(&method(:panic))
 
 when_ready do
+	LOG.info "Ready"
 	BLATHER = self
 	REDIS = EM::Hiredis.connect
 	BTC_SELL_PRICES = BTCSellPrices.new(REDIS, CONFIG[:oxr_app_id])
@@ -239,7 +247,7 @@ end
 message :error? do |m|
 	StatsD.increment("message_error")
 
-	puts "MESSAGE ERROR: #{m.inspect}"
+	LOG.error "MESSAGE ERROR", stanza: m
 end
 
 class SessionManager