On Mon, Jan 25, 2021 at 11:27:16AM +0800, Xiao Shenwei wrote:
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 | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/log.rb
diff --git a/lib/log.rb b/lib/log.rb new file mode 100644 index 0000000..762c0b2 --- /dev/null +++ b/lib/log.rb @@ -0,0 +1,33 @@ +# 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)
- File.delete(filename) if FileTest.exist?(filename)
How do I deal the same log file in different program?
Thanks, Xueliang
- 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
2.23.0