【发布时间】:2023-03-07 06:07:02
【问题描述】:
我在 lib/tasks 制作了 taks 文件
并且任务文件总是在项目日志目录中记录文件
我知道错误行发生在哪里
现在记录行自定义文本“第 N 行发生了”
下面是我的tasks.rb文件
17 log_file = File.open("#{Rails.root}/log/log_file.log", "w")
18
19 begin
20 number_of_row = 8000
21 process_page = args[:page].to_i
22
23 conn_url = CONNECTION_URL
24 xml_page = Nokogiri::XML(open(conn_url))
25
26 root = xml_page.css("root")
27 if root.css("code").text != "0000"
28
29
30
31 log_file.write "\n\n-------------------------------"
32 log_file.write "Line 32 ---------------------------\n\n"
33 log_file.write "Parse Error : #{conn_url},\n code : #{root.css("code").text}, \n msg: #{root.css("message").text} "
34 log_file.write "\n\n-------------------------------\n\n"
35 end
36 rescue => e
37 log_file.write "Line 37 ---------------------------\n\n"
38 log_file.write "Line 37 ---------------------------\n\n"
39 end
如何获取行号并记录这些信息?
【问题讨论】:
-
你可以使用__LINE__ - stackoverflow.com/questions/12175793/…
-
@Lesleh Thx :) 效果很好
-
把这个放到文件
t.rb:def my_method; puts "line #{__LINE__} in file #{__FILE__}, in method '#{__method__}'"; end; my_method。然后Ruby 't.rb' => line 2 in file t.rb, in method 'my_method'.
标签: ruby-on-rails ruby ruby-on-rails-4 logging