Simplify the log format for Dry Run

- Omit the severity initial and progname
- Show the elapsed time in HH:MM:SS instead of full date-time

Akinori MUSHA 9 年之前
父节点
当前提交
cb6f77f858
共有 1 个文件被更改,包括 16 次插入1 次删除
  1. 16 1
      app/concerns/dry_runnable.rb

+ 16 - 1
app/concerns/dry_runnable.rb

@@ -5,7 +5,12 @@ module DryRunnable
5 5
     @dry_run = true
6 6
 
7 7
     log = StringIO.new
8
-    @dry_run_logger = Logger.new(log)
8
+    @started_at = Time.zone.now
9
+    @dry_run_logger = Logger.new(log).tap { |logger|
10
+      logger.formatter = proc { |severity, datetime, progname, message|
11
+        "[#{elapsed_time(datetime)}] #{severity} -- #{progname}: #{message}\n"
12
+      }
13
+    }
9 14
     @dry_run_results = {
10 15
       events: [],
11 16
     }
@@ -13,13 +18,17 @@ module DryRunnable
13 18
     begin
14 19
       raise "#{short_type} does not support dry-run" unless can_dry_run?
15 20
       readonly!
21
+      @started_at = Time.zone.now
22
+      @dry_run_logger.info('Dry Run started')
16 23
       if event
17 24
         raise "This agent cannot receive an event!" unless can_receive_events?
18 25
         receive([event])
19 26
       else
20 27
         check
21 28
       end
29
+      @dry_run_logger.info('Dry Run finished')
22 30
     rescue => e
31
+      @dry_run_logger.info('Dry Run failed')
23 32
       error "Exception during dry-run. #{e.message}: #{e.backtrace.join("\n")}"
24 33
     end
25 34
 
@@ -35,6 +44,12 @@ module DryRunnable
35 44
     !!@dry_run
36 45
   end
37 46
 
47
+  def elapsed_time(datetime)
48
+    '%02d:%02d:%02d' % 2.times.inject([datetime - @started_at]) { |(x, *xs)|
49
+      [*x.divmod(60), *xs]
50
+    }
51
+  end
52
+
38 53
   included do
39 54
     prepend Wrapper
40 55
   end