File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,12 @@ require 'yaml'
3
3
namespace :news do
4
4
desc 'db/news.yml を読み込んで News テーブルを upsert する'
5
5
task import_from_yaml : :environment do
6
+ file_logger = ActiveSupport ::Logger . new ( 'log/news.log' )
7
+ console = ActiveSupport ::Logger . new ( STDOUT )
8
+ logger = ActiveSupport ::BroadcastLogger . new ( file_logger , console )
9
+
10
+ logger . info "==== START news:import_from_yaml ===="
11
+
6
12
yaml_path = Rails . root . join ( 'db' , 'news.yml' )
7
13
raw = YAML . safe_load ( File . read ( yaml_path ) , permitted_classes : [ Time ] , aliases : true )
8
14
@@ -22,17 +28,15 @@ namespace :news do
22
28
23
29
if is_new || news . changed?
24
30
news . save!
25
- if is_new
26
- new_count += 1
27
- status = 'new'
28
- else
29
- updated_count += 1
30
- status = 'updated'
31
- end
32
- puts "[News] #{ news . published_at . to_date } #{ news . title } (#{ status } )"
31
+ status = is_new ? 'new' : 'updated'
32
+ new_count += 1 if is_new
33
+ updated_count += 1 unless is_new
34
+
35
+ logger . info "[News] #{ news . published_at . to_date } #{ news . title } (#{ status } )" # ← puts から logger.info に変更
33
36
end
34
37
end
35
38
36
- puts "Imported #{ new_count + updated_count } items (#{ new_count } new, #{ updated_count } updated)."
39
+ logger . info "Imported #{ new_count + updated_count } items (#{ new_count } new, #{ updated_count } updated)."
40
+ logger . info "==== END news:import_from_yaml ===="
37
41
end
38
42
end
You can’t perform that action at this time.
0 commit comments