From d52e900ffcf3f08e6711e330d9cc9defccc58fed Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 4 Aug 2021 20:34:51 -0500 Subject: [PATCH] Slightly better logging 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. --- Gemfile | 2 ++ lib/registration.rb | 2 +- sgx_jmp.rb | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 98cf256..622a7e5 100644 --- a/Gemfile +++ b/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" diff --git a/lib/registration.rb b/lib/registration.rb index b4f35da..a9578ad 100644 --- a/lib/registration.rb +++ b/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([ diff --git a/sgx_jmp.rb b/sgx_jmp.rb index a094920..8c24367 100644 --- a/sgx_jmp.rb +++ b/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 -- 2.34.2