Records the run logs of the ruby program message format: 2021-01-25 11:00:09 +0800 INFO -- SCHED_PORT: 3000
Signed-off-by: Xiao Shenwei xiaoshenwei96@163.com --- lib/log.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/log.rb
diff --git a/lib/log.rb b/lib/log.rb new file mode 100644 index 0000000..25c92c6 --- /dev/null +++ b/lib/log.rb @@ -0,0 +1,32 @@ +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true + +require 'logger' + +# records the run logs of the ruby program +class Log < Logger + def initialize(filename) + super(filename) + format + end + + private + + def format + self.datetime_format = '%Y-%m-%d %H:%M:%s' + self.formatter = proc do |severity, datetime, _progname, msg| + transform_msg(msg).map { |m| "#{datetime} #{severity} -- #{m}\n" }.join + end + end + + def transform_msg(msg) + msg = if msg.is_a? Exception + ["#{msg.backtrace.first}: #{msg.message.split("\n").first} (#{msg.class.name})", + msg.backtrace[1..-1].map { |m| "\tfrom #{m}" }].flatten + else + msg.to_s.split("\n") + end + return msg + end +end