[ruby-list:50542] Re: [質問] raise によるメッセージをログファイルに出力するには?
From:
<yamataka@...08.itscom.net>
Date:
2017-06-05 09:10:58 UTC
List:
ruby-list #50542
山口です。
> > $stderr=$stdout
> > と書けば標準エラー出力もMyLogになり, 同じログファイルに含まれるよう
に
> なります。
>
> ご教示ありがとうございます。
> $stderr したつもりになっていました。
> もっと確認すべきでした。
> [実行結果]
> $ ruby MyLog.rb
> [1, 2, 3, 4, 5]
> This is a pen.
> MyLog.rb:35:in `<main>': tako (RuntimeError)
> $ cat 20170602_215052.log
> 06-02 21:50:52.024 [1, 2, 3, 4, 5]
> 06-02 21:50:52.024 This is a pen.
> 06-02 21:50:52.024 MyLog.rb:35:in `<main>'06-02 21:50:52.024 : 06-02
> 21:50:52.024 tako06-02 21:50:52.024 (06-02 21:50:52.024
> RuntimeError06-02 21:50:52.024 )
rescue 部分、ファイルへの出力
06-02 21:50:52.024 MyLog.rb:35:in `<main>': tako (RuntimeError)
の様に、出力できないものなのでしょうか?
rdebug にて、下記 write 関数の if で break 設定しても、
resuce 実行後、break されないので、解析できずで...
class MyLog < IO
def initialize(time=nil)
@time = time # if time is true, puts the time information on each log
line
file=Time.new.strftime("%Y%m%d_%H%M%S.log")
@f = File.open(file,"w")
super(1,"w")
end
def close
@f.close
super
end
def write(str)
# str != "¥n": str comes twice, first is line w/o "¥n", second is "¥n
" in each line
# without condition str != "¥n", puts out like this 06-02 05:27:32.
461 str 06-02 05:27:32.461
if @time && str != "¥n" then
time = Time.new.strftime("%m-%d %H:%M:%S.%L ")
@f.write(time)
end
@f.write(str)
super
end
end