epilog 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5fd0a8c3e9e8399b9f1e5f7c16a3de10b61a46274a0490ac1f39ed168b5812b8
4
- data.tar.gz: f83444e51908ca573918cfc5e8fd9515ccb1f4275aa80b17fa1da66c0593bf15
3
+ metadata.gz: 7c182e5b6fad45d60dcbf0e37843e075e6775d76e3888a632056cef356235768
4
+ data.tar.gz: bb7095818994ab3532c70c691180fe1018089e587cc7819faf1441bb6532acf9
5
5
  SHA512:
6
- metadata.gz: ff61b605c231ebd750c5e3ce1909c498afe203824fbfd0c70f061e71ffdb7ec7e02e0bc6a33e2ee44f4043d27e2bd14befbc23ad33c6f464d65910dedc200b3e
7
- data.tar.gz: 750683aa14e5c0c965088af4bcaf8de2180f58156e44a074d7ca4382275eaa6db1e12c02cc6afcce5dfa8122ceecb75a892876b4cad8144c0dc111c2b868843e
6
+ metadata.gz: 07552c37f2ebcc5febb7d94f227801c8f89e15eae7904e493c6575bd83819a1989b7229a9d1559ffc9f4651884611160b77779cfce11fa1abcf48c71bd484612
7
+ data.tar.gz: 5664a1841cdd95b1af21e8796a374f30d5ddfd26faf6ef4782abc9de435098d351f3e29d2b778358e74c1af5ae55b9cb4d4155c0cba776201ed9d350286abc41
data/CHANGLOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ - Add context support to Logger [#12](https://github.com/machinima/epilog/pull/12)
6
+
3
7
  ## 0.3.1
4
8
 
5
9
  - Add context to request log [#10](https://github.com/machinima/epilog/pull/10)
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Epilog
4
+ module ContextLogger
5
+ def with_context(context)
6
+ push_context(context)
7
+ yield
8
+ pop_context
9
+ end
10
+
11
+ def push_context(context)
12
+ formatter.push_context(context)
13
+ end
14
+
15
+ def pop_context
16
+ formatter.pop_context
17
+ end
18
+ end
19
+
20
+ module ContextFormatter
21
+ def context
22
+ Thread.current[current_context_key] ||= begin
23
+ result = {}
24
+ context_stack.each { |frame| result.merge!(frame) }
25
+ result
26
+ end.freeze
27
+ end
28
+
29
+ def push_context(frame)
30
+ clear_context
31
+ context_stack.push(frame)
32
+ end
33
+
34
+ def pop_context
35
+ clear_context
36
+ context_stack.pop
37
+ end
38
+
39
+ private
40
+
41
+ def clear_context
42
+ Thread.current[current_context_key] = nil
43
+ end
44
+
45
+ def context_stack
46
+ Thread.current[stack_key] ||= []
47
+ end
48
+
49
+ def stack_key
50
+ @stack_key ||= "epilog_context_stack:#{object_id}"
51
+ end
52
+
53
+ def current_context_key
54
+ @current_context_key ||= "epilog_context_current:#{object_id}"
55
+ end
56
+ end
57
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Epilog
4
4
  class Formatter
5
+ include ContextFormatter
6
+
5
7
  SEVERITY_MAP = {
6
8
  'FATAL' => 'ALERT',
7
9
  'WARN' => 'WARNING'
@@ -18,6 +20,7 @@ module Epilog
18
20
 
19
21
  def call(severity, time, progname, msg)
20
22
  log = base_log(severity, time, progname)
23
+ log.merge!(context)
21
24
  log.merge!(message(msg))
22
25
 
23
26
  if log[:exception].is_a?(Exception)
data/lib/epilog/logger.rb CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Epilog
4
4
  class Logger < ::Logger
5
+ include ContextLogger
6
+
5
7
  def initialize(*args, **options)
6
8
  super
7
9
  self.formatter = Formatter.new
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Epilog
4
4
  class MockLogger < ::Logger
5
- def initialize
6
- super(nil)
5
+ def initialize(**options)
6
+ super(nil, **options)
7
7
  reset
8
8
  end
9
9
 
@@ -44,6 +44,21 @@ module Epilog
44
44
 
45
45
  def reset
46
46
  @logs = []
47
+ @context = []
48
+ end
49
+
50
+ def with_context(context)
51
+ push_context(context)
52
+ yield
53
+ pop_context
54
+ end
55
+
56
+ def push_context(context)
57
+ @context << context
58
+ end
59
+
60
+ def pop_context
61
+ @context.pop
47
62
  end
48
63
 
49
64
  private
@@ -53,7 +68,7 @@ module Epilog
53
68
  end
54
69
 
55
70
  def write(severity, time, prog, message)
56
- @logs << [severity, time, prog, message]
71
+ @logs << [severity, time, prog, message, @context.dup]
57
72
  end
58
73
  end
59
74
  end
@@ -7,24 +7,31 @@ module Epilog
7
7
  RAILS_PARAMS = %i[controller action format _method only_path].freeze
8
8
 
9
9
  def request_received(event)
10
+ push_context(
11
+ { request: short_request_hash(event) }
12
+ .merge(event.payload[:context])
13
+ )
14
+
10
15
  info do
11
- event.payload[:context].merge(
16
+ {
12
17
  message: "#{request_string(event)} started",
13
18
  request: request_hash(event)
14
- )
19
+ }
15
20
  end
16
21
  end
17
22
 
18
23
  def process_request(event)
19
24
  info do
20
- event.payload[:context].merge(
25
+ {
21
26
  message: response_string(event),
22
27
  request: short_request_hash(event),
23
28
  response: response_hash(event),
24
29
  metrics: process_metrics(event.payload[:metrics]
25
30
  .merge(request_runtime: event.duration.round(2)))
26
- )
31
+ }
27
32
  end
33
+
34
+ pop_context
28
35
  end
29
36
 
30
37
  def start_processing(*)
@@ -12,6 +12,7 @@ module Epilog
12
12
  end
13
13
 
14
14
  def perform_start(event)
15
+ push_context(job: short_job_hash(event.payload[:job]))
15
16
  info { event_hash('Performing job', event) }
16
17
  end
17
18
 
@@ -23,6 +24,7 @@ module Epilog
23
24
  }
24
25
  )
25
26
  end
27
+ pop_context
26
28
  end
27
29
 
28
30
  private
@@ -45,6 +47,13 @@ module Epilog
45
47
  }
46
48
  end
47
49
 
50
+ def short_job_hash(job)
51
+ {
52
+ class: job.class.name,
53
+ id: job.job_id
54
+ }
55
+ end
56
+
48
57
  def format_time(time)
49
58
  Time.at(time).utc.strftime(Epilog::Formatter::DEFAULT_TIME_FORMAT)
50
59
  end
@@ -9,6 +9,14 @@ module Epilog
9
9
  super()
10
10
  @logger = logger
11
11
  end
12
+
13
+ def push_context(context)
14
+ @logger.try(:push_context, context)
15
+ end
16
+
17
+ def pop_context
18
+ @logger.try(:pop_context)
19
+ end
12
20
  end
13
21
  end
14
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Epilog
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/epilog.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'logger'
4
4
 
5
5
  require 'epilog/version'
6
+ require 'epilog/context_logging'
6
7
  require 'epilog/logger'
7
8
  require 'epilog/mock_logger'
8
9
  require 'epilog/log_formatter'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epilog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Howard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-08 00:00:00.000000000 Z
11
+ date: 2019-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -231,6 +231,7 @@ files:
231
231
  - bin/yri
232
232
  - epilog.gemspec
233
233
  - lib/epilog.rb
234
+ - lib/epilog/context_logging.rb
234
235
  - lib/epilog/filter.rb
235
236
  - lib/epilog/filter/blacklist.rb
236
237
  - lib/epilog/filter/filter_parameters.rb