Replace "puts" with JSONLogger. It's easier to query and locate faults.
Signed-off-by: Wu Zhende wuzhende666@163.com --- src/monitoring.cr | 12 +++++++++++- src/monitoring/amqp.cr | 6 ++++-- src/monitoring/filter.cr | 4 +++- 3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/src/monitoring.cr b/src/monitoring.cr index b1b534b..0f88be4 100644 --- a/src/monitoring.cr +++ b/src/monitoring.cr @@ -1,6 +1,16 @@ +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. + +require "./lib/json_logger" require "monitoring/monitoring" require "monitoring/constants"
module Monitoring - Kemal.run(MONITOR_PORT) + log = JSONLogger.new + + begin + Kemal.run(MONITOR_PORT) + rescue e + log.error(e) + end end diff --git a/src/monitoring/amqp.cr b/src/monitoring/amqp.cr index b0341ce..27ad3d2 100644 --- a/src/monitoring/amqp.cr +++ b/src/monitoring/amqp.cr @@ -3,10 +3,12 @@ require "amqp-client" require "./monitoring" require "./filter" require "./constants" +require "../lib/json_logger"
class MessageQueueClient def initialize(host = MQ_HOST, port = MQ_PORT) @client = AMQP::Client.new("amqp://#{host}:#{port}") + @log = JSONLogger.new end
private def start @@ -24,7 +26,7 @@ class MessageQueueClient begin filter.filter_msg(msg.body_io) rescue e - puts "filter message error: #{e}" + @log.warn("filter message failed: #{e}") end end end @@ -37,7 +39,7 @@ class MessageQueueClient filter_msg(conn, filter, exchange_name, queue_name) end rescue e - puts "monitoring_message_queue error: #{e}" + @log.warn("monitoring_message_queue failed: #{e}") sleep 5 end end diff --git a/src/monitoring/filter.cr b/src/monitoring/filter.cr index 79af3ab..2bd1d1a 100644 --- a/src/monitoring/filter.cr +++ b/src/monitoring/filter.cr @@ -2,6 +2,7 @@ require "set" require "json"
require "./parse_serial_logs" +require "../lib/json_logger"
class Filter def initialize @@ -9,6 +10,7 @@ class Filter # like {query => [socket1, socket2]} @hash = Hash(JSON::Any, Array(HTTP::WebSocket)).new @sp = SerialParser.new + @log = JSONLogger.new end
def add_filter_rule(query : JSON::Any, socket : HTTP::WebSocket) @@ -42,7 +44,7 @@ class Filter @hash[query].each do |socket| socket.send msg.to_json rescue e - puts "send msg failed: #{e}" + @log.warn("send msg failed: #{e}") remove_filter_rule(query, socket) end end