Merge pull request #1386 from cantino/simplify_log_format

Simplify the log format for Dry Run

Akinori MUSHA 8 years ago
parent
commit
90cd771c5a

+ 14 - 1
app/concerns/dry_runnable.rb

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

+ 3 - 3
spec/concerns/dry_runnable_spec.rb

@@ -70,7 +70,7 @@ describe DryRunnable do
70 70
       [@agent.memory, counts]
71 71
     }
72 72
 
73
-    expect(results[:log]).to match(/\AE, .+ ERROR -- : Exception during dry-run. SandboxedAgent does not support dry-run: /)
73
+    expect(results[:log]).to match(/\A\[\d\d:\d\d:\d\d\] INFO -- : Dry Run failed\n\[\d\d:\d\d:\d\d\] ERROR -- : Exception during dry-run. SandboxedAgent does not support dry-run: /)
74 74
     expect(results[:events]).to eq([])
75 75
     expect(results[:memory]).to eq({})
76 76
   end
@@ -86,7 +86,7 @@ describe DryRunnable do
86 86
         [@agent.memory, counts]
87 87
       }
88 88
 
89
-      expect(results[:log]).to match(/\AI, .+ INFO -- : Logging\nE, .+ ERROR -- : Recording error\n/)
89
+      expect(results[:log]).to match(/\A\[\d\d:\d\d:\d\d\] INFO -- : Dry Run started\n\[\d\d:\d\d:\d\d\] INFO -- : Logging\n\[\d\d:\d\d:\d\d\] ERROR -- : Recording error\n/)
90 90
       expect(results[:events]).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
91 91
       expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
92 92
     end
@@ -101,7 +101,7 @@ describe DryRunnable do
101 101
         [@agent.memory, counts]
102 102
       }
103 103
 
104
-      expect(results[:log]).to match(/\AI, .+ INFO -- : Logging\nE, .+ ERROR -- : Recording error\n/)
104
+      expect(results[:log]).to match(/\A\[\d\d:\d\d:\d\d\] INFO -- : Dry Run started\n\[\d\d:\d\d:\d\d\] INFO -- : Logging\n\[\d\d:\d\d:\d\d\] ERROR -- : Recording error\n/)
105 105
       expect(results[:events]).to eq([{ 'test' => 'superfoo' }, { 'test' => 'superbar' }])
106 106
       expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
107 107
     end

+ 1 - 1
spec/controllers/agents_controller_spec.rb

@@ -406,7 +406,7 @@ describe AgentsController do
406 406
         [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count, agent.name, agent.updated_at]
407 407
       }
408 408
       json = JSON.parse(response.body)
409
-      expect(json['log']).to match(/^I, .* : Fetching #{Regexp.quote(url_from_event)}$/)
409
+      expect(json['log']).to match(/^\[\d\d:\d\d:\d\d\] INFO -- : Fetching #{Regexp.quote(url_from_event)}$/)
410 410
     end
411 411
   end
412 412