+# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true
+require 'logger'
+# define a logger format +class Mylog < Logger
- def initialize(filename)
- @name = filename
- rm_exist?
- super(filename)
- format
- end
- private
- def rm_exist?
- if FileTest.exist?(@name)
File.delete(@name)
- end
File.delete(@name) if File.exist?(@name)
- end
- def format
- self.datetime_format = '%Y-%m-%d %H:%M:%s'
- self.formatter = proc do |severity, datetime, _progname, 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
msg.map { |m| "#{datetime} #{severity} -- #{m}\n" }.join
- end
- end
+end
2.23.0