主页

puts 和 Rails.logger 打印的区别

2023-10-11 06:10PM

puts 是在命令行里面打印输出。使用 puts 进行临时输出、调试和验证代码逻辑。

Rails.logger 是在日志里面打印输出。使用 Rails.logger 记录应用程序的运行时信息、错误和重要事件。

eg : 

puts

 test "should get index width paramater" do
     # 打印出所有的 Manager 记录
     puts Manager.all.inspect
     assert_equal 3, Manager.count
     assert Manager.count == 3
     get managers_url, params: { email: "admin@email.com" }
     assert_equal 1, assigns(:managers).size
     assert_response :success
   end

运行命令行输出:

$ bundle exec rails test test/controllers/managers_controller_test.rb:18
Run options: --seed 24542

# Running:

#<ActiveRecord::Relation [#<Manager id: 529899929, email: "admin2@email.com", created_at: "2023-10-11 10:03:48.784041000 +0000", updated_at: "2023-10-11 10:03:48.784041000 +0000", category: "管理员", region_id: nil, name: nil, owner: nil, status: nil>, #<Manager id: 680694018, email: "baogong_user_1@email.com", created_at: "2023-10-11 10:03:48.784041000 +0000", updated_at: "2023-10-11 10:03:48.784041000 +0000", category: nil, region_id: nil, name: nil, owner: nil, status: nil>, #<Manager id: 832291002, email: "admin@email.com", created_at: "2023-10-11 10:03:48.784041000 +0000", updated_at: "2023-10-11 10:03:48.784041000 +0000", category: nil, region_id: nil, name: nil, owner: nil, status: nil>]>
.

Finished in 0.305378s, 3.2746 runs/s, 13.0985 assertions/s.
1 runs, 4 assertions, 0 failures, 0 errors, 0 skips 

Rails.logger

test "should get index width paramater" do
     # 将Manager 模型的所有记录以 字符串的形式打印到 Rails 日志中 
     Rails.logger.info "====Manager: #{Manager.all}"
     assert_equal 3, Manager.count
     assert Manager.count == 3
     get managers_url, params: { email: "admin@email.com" }
     assert_equal 1, assigns(:managers).size
     assert_response :success
   end

运行日志输出: 

TRANSACTION (0.2ms)  BEGIN
-------------------------------------------------------------
ManagersControllerTest: test_should_get_index_width_paramater
-------------------------------------------------------------
  Manager Load (0.6ms)  SELECT `managers`.* FROM `managers` WHERE `managers`.`id` = 529899929 LIMIT 1
  Manager Load (0.4ms)  SELECT `managers`.* FROM `managers` WHERE `managers`.`id` = 832291002 LIMIT 1
====Manager: #<Manager::ActiveRecord_Relation:0x000055a7c347e508>
  Manager Count (0.5ms)  SELECT COUNT(*) FROM `managers`
  CACHE Manager Count (0.0ms)  SELECT COUNT(*) FROM `managers`
Started GET "/managers?email=admin%40email.com" for 127.0.0.1 at 2023-10-11 18:05:39 +0800
  TRANSACTION (0.3ms)  SAVEPOINT active_record_1
  Manager Update (0.5ms)  UPDATE `managers` SET `managers`.`updated_at` = '2023-10-11 10:05:39.751227', `managers`.`current_sign_in_at` = '2023-10-11 10:05:39.750463', `managers`.`last_sign_in_at` = '2023-10-11 10:05:39.750463', `managers`.`current_sign_in_ip` = '127.0.0.1', `managers`.`last_sign_in_ip` = '127.0.0.1', `managers`.`sign_in_count` = 1 WHERE `managers`.`id` = 529899929
  TRANSACTION (0.2ms)  RELEASE SAVEPOINT active_record_1
Processing by ManagersController#index as HTML
  Parameters: {"email"=>"admin@email.com"}
  Rendering layout layouts/application.html.erb
  Rendering managers/index.html.erb within layouts/application
  Manager Load (0.5ms)  SELECT `managers`.* FROM `managers` WHERE (email LIKE '%admin@email.com%') ORDER BY `managers`.`created_at` DESC LIMIT 10 OFFSET 0
  Rendered managers/index.html.erb within layouts/application (Duration: 4.8ms | Allocations: 3530)
  Message Count (0.3ms)  SELECT COUNT(*) FROM `messages` WHERE `messages`.`recipient_id` = 529899929 AND `messages`.`read` = FALSE
  Rendered home/_charts_menu.html.erb (Duration: 1.0ms | Allocations: 883)
  Rendered home/_statistics_menu.html.erb (Duration: 0.7ms | Allocations: 583)
  Rendered home/_warn_menu.html.erb (Duration: 0.7ms | Allocations: 582)
  Rendered home/_enterprises_menu.html.erb (Duration: 0.9ms | Allocations: 731)
  Rendered home/_managers_menu.html.erb (Duration: 0.5ms | Allocations: 320)
  Rendered home/_menu.html.erb (Duration: 7.6ms | Allocations: 5688)
  Rendered layout layouts/application.html.erb (Duration: 26.6ms | Allocations: 20490)
Completed 200 OK in 35ms (Views: 27.5ms | ActiveRecord: 2.1ms | Allocations: 26869)
  TRANSACTION (2.4ms)  ROLLBACK

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论