Skip to content

Commit 79d486d

Browse files
committed
chore: news:import タスクを logger 出力に統一
1 parent f0160ad commit 79d486d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/tasks/import_news.rake

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ require 'yaml'
33
namespace :news do
44
desc 'db/news.yml を読み込んで News テーブルを upsert する'
55
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+
612
yaml_path = Rails.root.join('db', 'news.yml')
713
raw = YAML.safe_load(File.read(yaml_path), permitted_classes: [Time], aliases: true)
814

@@ -22,17 +28,15 @@ namespace :news do
2228

2329
if is_new || news.changed?
2430
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 に変更
3336
end
3437
end
3538

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 ===="
3741
end
3842
end

0 commit comments

Comments
 (0)