Setup logger and use it

Stephen Paul Weber created

Change summary

bin/sim_job | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)

Detailed changes

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)