From 9fba119dbd9c5fff17f6634f8cb3ae5ebc790521 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 16 Oct 2023 15:47:53 -0500 Subject: [PATCH] Setup logger and use it --- bin/sim_job | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/sim_job b/bin/sim_job index ace9d9761b899b0237dabb73c28baea1e1783bd6..dc69c6c55db292a99a5c3f773749a4ed4e14fb09 100755 --- a/bin/sim_job +++ b/bin/sim_job @@ -7,8 +7,22 @@ require "eventmachine" require "em_promise" require "em-hiredis" require "dhall" +require "ougai" require "sentry-ruby" +$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 +) + +def log + LOG +end + Sentry.init do |config| config.background_worker_threads = 0 end @@ -95,7 +109,7 @@ module SimAction amount: -price, note: note ).insert }.then do - puts "Refilled #{customer.customer_id} #{iccid}" + LOG.info "Refilled #{customer.customer_id} #{iccid}" end end @@ -124,7 +138,7 @@ class SimTopUp LowBalance.for(customer, refill_price).then(&:notify!).then do |result| next call if result.positive? - puts "Low balance #{customer.customer_id} #{iccid}" + LOG.info "Low balance #{customer.customer_id} #{iccid}" end end @@ -156,7 +170,7 @@ class SimWarn next unless spent >= limit || low_balance_and_not_auto_top_up notify - puts "Data warning #{customer.customer_id} #{sim.iccid}" + LOG.info "Data warning #{customer.customer_id} #{@sim.iccid}" end end @@ -244,7 +258,7 @@ EM.run do items = items.values.select(&:customer) EMPromise.all(items.map(&:call)) }.catch { |e| - p e + LOG.error e if e.is_a?(::Exception) Sentry.capture_exception(e)