betterlog 1.1.1 → 2.0.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: 745c31c2af64dead7f6c4caef9ba763c4f03242d981668a945047017bd040e4d
4
- data.tar.gz: e63e066eeae23e30048c2f1d132ffb8c6c93e633d1670e3dfd208c5682ef54f7
3
+ metadata.gz: 41bb65aafa33ccbe83390a1d8a4a68a98f9a88ff94c50de62b71afe32f81fac2
4
+ data.tar.gz: e4c9c13fdb23f7448744ef34dfd6667cb1ce8de6a6e8dda6017ccbf874d19fd2
5
5
  SHA512:
6
- metadata.gz: 3cb7b7a8b3ff230deca829d39f319d63d4177dacf19f2f5bf8b5688ea6cb8aa0c61c1c33eaf10370480fe0cf449273989e8a1cd2ca97bfc45bfe2fc7ffc79514
7
- data.tar.gz: '08df00e3be93a83e87be11098b71a79b8c073d62951ff1aadcc1772e4ecbf871796abaa13f02b250849ae8534f0bd2af25e9699099be46bfc872d52ebf014669'
6
+ metadata.gz: 55b3b6f9ac7ed4d641902c798cb6c372c082302c2a3a8b93373b333055168e7647f90229c285a287b886d5f90d8cd09ef4c49fefa268e720cd25b3f6590a902b
7
+ data.tar.gz: 96415752787e2ac1e3345bdfa448fdb6f752dad4d338d0ef940473c7596c167635ea9e5660ef7d8313af3a1201ae2efd21a80a92eb6838f25cbaa37e1051853d
data/.all_images.yml ADDED
@@ -0,0 +1,17 @@
1
+ dockerfile: |-
2
+ RUN apk add --no-cache build-base git
3
+ RUN gem update --system
4
+ RUN gem install gem_hadar bundler
5
+
6
+ script: &script |-
7
+ echo -e "\e[1m"
8
+ ruby -v
9
+ echo -e "\e[0m"
10
+ bundle
11
+ rake spec
12
+
13
+ fail_fast: yes
14
+
15
+ images:
16
+ ruby:3.2-alpine: *script
17
+ ruby:3.1-alpine: *script
data/.tool-versions CHANGED
@@ -1,2 +1,2 @@
1
- ruby 3.1.2
1
+ ruby 3.1.4
2
2
  bundler 2.3.17
data/Rakefile CHANGED
@@ -29,6 +29,8 @@ GemHadar do
29
29
  development_dependency 'rake'
30
30
  development_dependency 'rspec'
31
31
  development_dependency 'simplecov'
32
+ development_dependency 'debug'
33
+ development_dependency 'all_images'
32
34
  end
33
35
 
34
36
  task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 2.0.0
data/betterlog.gemspec CHANGED
@@ -1,22 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: betterlog 1.1.1 ruby lib
2
+ # stub: betterlog 2.0.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "betterlog".freeze
6
- s.version = "1.1.1"
6
+ s.version = "2.0.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["betterplace Developers".freeze]
11
- s.date = "2023-08-16"
11
+ s.date = "2023-08-17"
12
12
  s.description = "This library provides structure json logging for our rails projects".freeze
13
13
  s.email = "developers@betterplace.org".freeze
14
14
  s.executables = ["betterlog".freeze]
15
15
  s.extra_rdoc_files = ["README.md".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/notifiers.rb".freeze, "lib/betterlog/railtie.rb".freeze, "lib/betterlog/version.rb".freeze]
16
- s.files = [".github/workflows/codeql-analysis.yml".freeze, ".gitignore".freeze, ".semaphore/semaphore.yml".freeze, ".tool-versions".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "betterlog.gemspec".freeze, "bin/betterlog".freeze, "config/log.yml".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/notifiers.rb".freeze, "lib/betterlog/railtie.rb".freeze, "lib/betterlog/version.rb".freeze, "spec/betterlog/global_metadata_spec.rb".freeze, "spec/betterlog/log/event_spec.rb".freeze, "spec/betterlog/log/severity_spec.rb".freeze, "spec/betterlog/log_event_formatter_spec.rb".freeze, "spec/betterlog/log_spec.rb".freeze, "spec/betterlog/version_spec.rb".freeze, "spec/spec_helper.rb".freeze]
16
+ s.files = [".all_images.yml".freeze, ".github/workflows/codeql-analysis.yml".freeze, ".gitignore".freeze, ".semaphore/semaphore.yml".freeze, ".tool-versions".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "betterlog.gemspec".freeze, "bin/betterlog".freeze, "config/log.yml".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/notifiers.rb".freeze, "lib/betterlog/railtie.rb".freeze, "lib/betterlog/version.rb".freeze, "spec/betterlog/global_metadata_spec.rb".freeze, "spec/betterlog/log/event_spec.rb".freeze, "spec/betterlog/log/severity_spec.rb".freeze, "spec/betterlog/log_event_formatter_spec.rb".freeze, "spec/betterlog/log_spec.rb".freeze, "spec/betterlog/version_spec.rb".freeze, "spec/spec_helper.rb".freeze]
17
17
  s.homepage = "http://github.com/betterplace/betterlog".freeze
18
18
  s.rdoc_options = ["--title".freeze, "Betterlog".freeze, "--main".freeze, "README.md".freeze]
19
- s.rubygems_version = "3.3.25".freeze
19
+ s.rubygems_version = "3.3.26".freeze
20
20
  s.summary = "Structured logging support for bp".freeze
21
21
  s.test_files = ["spec/betterlog/global_metadata_spec.rb".freeze, "spec/betterlog/log/event_spec.rb".freeze, "spec/betterlog/log/severity_spec.rb".freeze, "spec/betterlog/log_event_formatter_spec.rb".freeze, "spec/betterlog/log_spec.rb".freeze, "spec/betterlog/version_spec.rb".freeze, "spec/spec_helper.rb".freeze]
22
22
 
@@ -29,6 +29,8 @@ Gem::Specification.new do |s|
29
29
  s.add_development_dependency(%q<rake>.freeze, [">= 0"])
30
30
  s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
31
31
  s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
32
+ s.add_development_dependency(%q<debug>.freeze, [">= 0"])
33
+ s.add_development_dependency(%q<all_images>.freeze, [">= 0"])
32
34
  s.add_runtime_dependency(%q<tins>.freeze, ["~> 1.3", ">= 1.22.0"])
33
35
  s.add_runtime_dependency(%q<complex_config>.freeze, [">= 0"])
34
36
  s.add_runtime_dependency(%q<file-tail>.freeze, ["~> 1.0"])
@@ -41,6 +43,8 @@ Gem::Specification.new do |s|
41
43
  s.add_dependency(%q<rake>.freeze, [">= 0"])
42
44
  s.add_dependency(%q<rspec>.freeze, [">= 0"])
43
45
  s.add_dependency(%q<simplecov>.freeze, [">= 0"])
46
+ s.add_dependency(%q<debug>.freeze, [">= 0"])
47
+ s.add_dependency(%q<all_images>.freeze, [">= 0"])
44
48
  s.add_dependency(%q<tins>.freeze, ["~> 1.3", ">= 1.22.0"])
45
49
  s.add_dependency(%q<complex_config>.freeze, [">= 0"])
46
50
  s.add_dependency(%q<file-tail>.freeze, ["~> 1.0"])
@@ -1,19 +1,30 @@
1
1
  # To retrieve thread-global metadata that is used to enrich data that is sent
2
2
  # to logging and debugging tools. In addition to holding the data
3
- # thread-global, this will also attempt to update context of error reporting
3
+ # thread-global, this will also attempt to update current of error reporting
4
4
  # tools etc.
5
5
 
6
6
  module Betterlog
7
7
  class GlobalMetadata
8
8
  include Tins::SexySingleton
9
9
 
10
- thread_local(:data) { {} }
10
+ thread_local(:current) { {} }
11
11
 
12
- def add(data_hash)
13
- data_hash = data_hash.symbolize_keys_recursive
14
- data = data_hash | data
15
- Notifiers.context(data_hash)
12
+ def add(data)
13
+ data = data.symbolize_keys_recursive
14
+ self.current = data | current
16
15
  self
17
16
  end
17
+
18
+ def remove(data)
19
+ keys = data.ask_and_send_or_self(:keys).map(&:to_sym)
20
+ keys.each { current.delete(_1) }
21
+ end
22
+
23
+ def with_context(data = {})
24
+ add data
25
+ yield current.dup.freeze
26
+ ensure
27
+ remove data
28
+ end
18
29
  end
19
30
  end
@@ -3,42 +3,23 @@ module Betterlog
3
3
  class Event
4
4
  require 'socket'
5
5
 
6
- def self.ify(arg, severity: :debug, notify: nil, rest: {})
7
- notify ||= rest.delete(:notify)
6
+ def self.ify(arg, **rest)
7
+ rest = rest.symbolize_keys_recursive(circular: :circular)
8
8
  if e = arg.ask_and_send(:exception)
9
- ify(
9
+ new(
10
10
  {
11
11
  error_class: e.class.name,
12
12
  message: "#{e.class.name}: #{e.message}",
13
13
  backtrace: e.backtrace,
14
- },
15
- severity: severity,
16
- rest: rest,
17
- notify: notify,
18
- )
19
- elsif s = arg.ask_and_send(:to_str)
20
- new(
21
- ({ notify: s } if notify).to_h |
22
- {
23
- message: s,
24
- severity: severity,
25
14
  } | rest
26
15
  )
27
- elsif h = arg.ask_and_send(:to_hash)
28
- arg = h | { severity: severity } | rest
29
- new(
30
- ({ notify: h[:message] || arg.to_s } if notify).to_h |
31
- arg
32
- )
16
+ elsif message = arg.ask_and_send(:to_str)
17
+ new({ message: } | rest)
18
+ elsif hash = arg.ask_and_send(:to_hash)
19
+ new(hash | rest)
33
20
  else
34
21
  message = "Logging #{arg.inspect}"
35
- new(
36
- ({ notify: message } if notify).to_h |
37
- {
38
- message: message,
39
- severity: severity,
40
- } | rest
41
- )
22
+ new({ message: } | rest)
42
23
  end
43
24
  end
44
25
 
@@ -57,16 +38,16 @@ module Betterlog
57
38
  end
58
39
 
59
40
  def initialize(data = {})
60
- data = data.symbolize_keys_recursive(circular: :circular) | meta
61
- unless data.key?(:message)
62
- data[:message] = "a #{data[:type]} type log message of severity #{data[:severity]}"
63
- end
41
+ data = compute_data(data.symbolize_keys_recursive(circular: :circular))
64
42
  data[:severity] =
65
43
  begin
66
44
  Severity.new((data[:severity] || :debug))
67
45
  rescue
68
46
  Severity.new(:debug)
69
47
  end
48
+ unless data.key?(:message)
49
+ data[:message] = "a #{data[:type]} type log message of severity #{data[:severity]}"
50
+ end
70
51
  @data = Hash[data.sort_by(&:first)]
71
52
  end
72
53
 
@@ -124,8 +105,8 @@ module Betterlog
124
105
 
125
106
  private
126
107
 
127
- def meta
128
- m = {
108
+ def compute_data(data)
109
+ d = data | {
129
110
  timestamp: Time.now.utc.iso8601(3),
130
111
  pid: $$,
131
112
  program: File.basename($0),
@@ -133,15 +114,12 @@ module Betterlog
133
114
  type: 'rails',
134
115
  facility: 'local0',
135
116
  host: (Socket.gethostname rescue nil),
136
- thread_id: Thread.current.object_id
117
+ thread_id: Thread.current.object_id,
137
118
  }
138
- if defined? GlobalMetadata
139
- m |= GlobalMetadata.data
140
- end
141
- if defined? Sidekiq::Context.current
142
- m |= Sidekiq::Context.current&.symbolize_keys_recursive
143
- end
144
- m
119
+ d[:meta] ||= {}
120
+ d[:meta] |= (GlobalMetadata.current |
121
+ (Sidekiq::Context.current&.symbolize_keys_recursive if defined?(Sidekiq::Context)))
122
+ d
145
123
  end
146
124
  end
147
125
  end
data/lib/betterlog/log.rb CHANGED
@@ -26,7 +26,8 @@ module Betterlog
26
26
  # @return [ Log ] this object itself.
27
27
  def info(object, **rest)
28
28
  protect do
29
- emit Log::Event.ify(object, severity: __method__, rest: rest)
29
+ rest = { severity: __method__ } | rest
30
+ emit Log::Event.ify(object, **rest)
30
31
  end
31
32
  end
32
33
 
@@ -37,7 +38,8 @@ module Betterlog
37
38
  # @return [ Log ] this object itself.
38
39
  def warn(object, **rest)
39
40
  protect do
40
- emit Log::Event.ify(object, severity: __method__, rest: rest)
41
+ rest = { severity: __method__ } | rest
42
+ emit Log::Event.ify(object, **rest)
41
43
  end
42
44
  end
43
45
 
@@ -48,7 +50,8 @@ module Betterlog
48
50
  # @return [ Log ] this object itself.
49
51
  def debug(object, **rest)
50
52
  protect do
51
- emit Log::Event.ify(object, severity: __method__, rest: rest)
53
+ rest = { severity: __method__ } | rest
54
+ emit Log::Event.ify(object, **rest)
52
55
  end
53
56
  end
54
57
 
@@ -59,7 +62,8 @@ module Betterlog
59
62
  # @return [ Log ] this object itself.
60
63
  def error(object, **rest)
61
64
  protect do
62
- emit Log::Event.ify(object, severity: __method__, rest: rest)
65
+ rest = { severity: __method__ } | rest
66
+ emit Log::Event.ify(object, **rest)
63
67
  end
64
68
  end
65
69
 
@@ -70,7 +74,8 @@ module Betterlog
70
74
  # @return [ Log ] this object itself.
71
75
  def fatal(object, **rest)
72
76
  protect do
73
- emit Log::Event.ify(object, severity: __method__, rest: rest)
77
+ rest = { severity: __method__ } | rest
78
+ emit Log::Event.ify(object, **rest)
74
79
  end
75
80
  end
76
81
 
@@ -82,51 +87,12 @@ module Betterlog
82
87
  # @return [ Log ] this object itself.
83
88
  def output(object, **rest)
84
89
  protect do
85
- emit Log::Event.ify(object, severity: rest[:severity], rest: rest)
90
+ emit Log::Event.ify(object, **rest)
86
91
  end
87
92
  end
88
93
 
89
- # Logs a metric on severity debug, by default, this can be changed by passing
90
- # the severity: keyword. +name+ is for example 'Donation.Confirmation' and
91
- # +value+ can be any value, but has to be somewhat consistent in terms of
92
- # structure with +name+ to allow for correct
93
- # evaluation.
94
- #
95
- # @param name the name of the recorded metric.
96
- # @param value of the recorded metric, defaults to duration if block was given.
97
- # @param success a Proc with parameter +result+ that returns true iff block
98
- # result was asuccessful
99
- # @param **rest additional rest is logged as well.
100
- # @return [ Log ] this object itself.
101
94
  def metric(name:, value: nil, success: -> result { true }, **rest, &block)
102
- result = time_block(&block)
103
- rescue => error
104
- e = Log::Event.ify(error)
105
- rest |= e.as_json.subhash(:error_class, :backtrace, :message)
106
- rest[:message] = "#{rest[:message].inspect} while measuring metric #{name}"
107
- raise error
108
- ensure
109
- protect do
110
- if timed_duration
111
- rest[:duration] = timed_duration
112
- end
113
- event = build_metric(
114
- name: name,
115
- value: value || timed_duration,
116
- success: success.(result),
117
- **rest
118
- )
119
- emit event
120
- end
121
- end
122
-
123
- def context(data_hash)
124
- GlobalMetadata.add data_hash
125
- self
126
- end
127
-
128
- def self.context(data_hash)
129
- instance.context(data_hash)
95
+ warn "#{self.class}##{__method__} is deprecated"
130
96
  end
131
97
 
132
98
  def emit(event)
@@ -140,8 +106,6 @@ module Betterlog
140
106
  notify(event)
141
107
  logger.send(event.severity.to_sym, JSON.generate(event))
142
108
  self
143
- ensure
144
- GlobalMetadata.data.clear
145
109
  end
146
110
 
147
111
  private
@@ -163,7 +127,6 @@ module Betterlog
163
127
  end
164
128
 
165
129
  def build_metric(name:, value:, **rest)
166
- severity = rest.fetch(:severity, :info)
167
130
  rest |= {
168
131
  message: "a metric #{name}=#{value}",
169
132
  }
@@ -173,7 +136,6 @@ module Betterlog
173
136
  value: value,
174
137
  type: 'metric'
175
138
  } | rest,
176
- severity: severity
177
139
  )
178
140
  end
179
141
 
@@ -48,6 +48,7 @@ module Betterlog
48
48
  rescue => e
49
49
  Betterlog::Log.logger.error(e)
50
50
  ensure
51
+ message = message.to_s
51
52
  # Do not "message << ?\n" - A frozen string may be passed in
52
53
  message.end_with?(?\n) or message = "#{message}\n"
53
54
  return message
@@ -13,15 +13,17 @@ module Betterlog
13
13
  end
14
14
 
15
15
  def self.notify(event)
16
+ event.notify? or return
16
17
  notifiers.each do |notifier|
17
- notifier.notify(event.notify?, event.as_json)
18
+ context(event.as_json)
19
+ notifier.notify(event[:message], event.as_json)
18
20
  end
19
21
  end
20
22
 
21
- def self.context(data_hash)
23
+ def self.context(data)
22
24
  notifiers.each do |notifier|
23
25
  notifier.respond_to?(:context) or next
24
- notifier.context(data_hash)
26
+ notifier.context(data)
25
27
  end
26
28
  end
27
29
  end
@@ -1,6 +1,6 @@
1
1
  module Betterlog
2
2
  # Betterlog version
3
- VERSION = '1.1.1'
3
+ VERSION = '2.0.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -1,6 +1,72 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Betterlog::GlobalMetadata do
4
+ describe '.add' do
5
+ it 'can add to context' do
6
+ expect(Betterlog::GlobalMetadata.current).to be_empty
7
+ Betterlog::GlobalMetadata.add(
8
+ 'foo' => 'bar',
9
+ )
10
+ expect(Betterlog::GlobalMetadata.current).to eq(foo: 'bar')
11
+ end
12
+ end
13
+
14
+ describe '.remove' do
15
+ it 'can remove by hash' do
16
+ Betterlog::GlobalMetadata.add(
17
+ 'foo' => 'bar',
18
+ )
19
+ expect(Betterlog::GlobalMetadata.current).to eq(foo: 'bar')
20
+ Betterlog::GlobalMetadata.remove(
21
+ 'foo' => 'bar',
22
+ )
23
+ expect(Betterlog::GlobalMetadata.current).to be_empty
24
+ end
25
+
26
+ it 'can remove by array' do
27
+ Betterlog::GlobalMetadata.add(
28
+ 'foo' => 'bar',
29
+ )
30
+ expect(Betterlog::GlobalMetadata.current).to eq(foo: 'bar')
31
+ Betterlog::GlobalMetadata.remove(%i[ foo ])
32
+ expect(Betterlog::GlobalMetadata.current).to be_empty
33
+ end
34
+ end
35
+
36
+ describe '.with_context' do
37
+ it 'can add to context and remove it' do
38
+ expect(Betterlog::GlobalMetadata.current).to be_empty
39
+ Betterlog::GlobalMetadata.with_context(
40
+ 'foo' => 'bar',
41
+ :bar => 'foo',
42
+ ) do |my_context|
43
+ expect(Betterlog::GlobalMetadata.current).to eq(foo: 'bar', bar: 'foo')
44
+ expect(my_context).to eq(foo: 'bar', bar: 'foo')
45
+ expect(my_context).to be_frozen
46
+ end
47
+ expect(Betterlog::GlobalMetadata.current).to be_empty
48
+ end
49
+
50
+ it 'can add to nested context and remove it' do
51
+ expect(Betterlog::GlobalMetadata.current).to be_empty
52
+ Betterlog::GlobalMetadata.with_context(
53
+ 'foo' => 'bar',
54
+ :bar => 'foo',
55
+ ) do |my_context|
56
+ expect(Betterlog::GlobalMetadata.current).to eq(foo: 'bar', bar: 'foo')
57
+ expect(my_context).to eq(foo: 'bar', bar: 'foo')
58
+ expect(my_context).to be_frozen
59
+ Betterlog::GlobalMetadata.with_context('quux' => 'quark') do |my_context|
60
+ expect(my_context).to be_frozen
61
+ expect(Betterlog::GlobalMetadata.current).to eq(foo: 'bar', bar: 'foo', quux: 'quark')
62
+ expect(my_context).to eq(foo: 'bar', bar: 'foo', quux: 'quark')
63
+ end
64
+ expect(my_context).to eq(foo: 'bar', bar: 'foo')
65
+ end
66
+ expect(Betterlog::GlobalMetadata.current).to be_empty
67
+ end
68
+ end
69
+
4
70
  class FakeNotifierClass
5
71
  def notify(message, hash) end
6
72
 
@@ -16,25 +82,15 @@ describe Betterlog::GlobalMetadata do
16
82
  example.run
17
83
  ensure
18
84
  Betterlog::Notifiers.notifiers.clear
19
- described_class.data.clear
85
+ described_class.current.clear
20
86
  end
21
87
 
22
88
  it 'can haz empty data' do
23
- expect(described_class.data).to eq({})
89
+ expect(described_class.current).to eq({})
24
90
  end
25
91
 
26
92
  it 'can haz some data' do
27
- described_class.data |= { foo: 'bar' }
28
- expect(described_class.data).to eq({ foo: 'bar' })
29
- end
30
-
31
- it 'can "add" data' do
32
- expect_any_instance_of(FakeNotifierClass).to receive(:context).with(foo: 'bar')
33
- expect(described_class.add(foo: 'bar')).to eq described_class.instance
34
- end
35
-
36
- it 'can "add" data via Log.context' do
37
- expect_any_instance_of(FakeNotifierClass).to receive(:context).with(foo: 'bar')
38
- Betterlog::Log.context(foo: 'bar')
93
+ described_class.current |= { foo: 'bar' }
94
+ expect(described_class.current).to eq({ foo: 'bar' })
39
95
  end
40
96
  end
@@ -78,6 +78,8 @@ describe Betterlog::Log do
78
78
  let :notifier do
79
79
  Class.new do
80
80
  def notify(message, hash) end
81
+
82
+ def context(data) end
81
83
  end.new
82
84
  end
83
85
 
@@ -103,9 +105,19 @@ describe Betterlog::Log do
103
105
  it 'can send explicit notifications with additional hash values' do
104
106
  expect(notifier).to receive(:notify).with(
105
107
  'test',
106
- hash_including(message: 'test', foo: 'bar')
108
+ hash_including(meta: { foo: 'bar' }),
107
109
  )
108
- Log.info({ message: 'test', foo: 'bar' }, notify: true)
110
+ Log.info({ message: 'test', meta: { foo: 'bar' } }, notify: true)
111
+ end
112
+
113
+ it 'can send notifications with additional hash values for context' do
114
+ expect(notifier).to receive(:notify).with(
115
+ 'test',
116
+ hash_including(meta: { foo: 'bar' }),
117
+ )
118
+ Betterlog::GlobalMetadata.with_context(foo: 'bar') do
119
+ Log.info('test', notify: true)
120
+ end
109
121
  end
110
122
 
111
123
  it 'can send explicit notifications for exceptions' do
@@ -147,103 +159,4 @@ describe Betterlog::Log do
147
159
  Log.error(exception, foo: 'bar')
148
160
  end
149
161
  end
150
-
151
- describe '#metric' do
152
- after do
153
- Time.dummy = nil
154
- end
155
-
156
- it 'logs metrics with a specific structure on info log level' do
157
- expected_event = Log::Event.new(
158
- message: 'a metric controller.action=0.123',
159
- name: 'controller.action',
160
- value: 0.123,
161
- success: true,
162
- type: 'metric',
163
- severity: 'info'
164
- )
165
- expect(instance).to receive(:emit).with(expected_event)
166
- Log.metric(name: 'controller.action', value: 0.123)
167
- end
168
-
169
- it 'logs metrics on a given log level' do
170
- expected_event = Log::Event.new(
171
- message: 'a metric controller.action=0.123',
172
- name: 'controller.action',
173
- value: 0.123,
174
- success: true,
175
- type: 'metric',
176
- severity: :info,
177
- )
178
- expect(instance).to receive(:emit).with(expected_event)
179
- Log.metric(severity: :info, name: 'controller.action', value: 0.123)
180
- end
181
-
182
- it 'logs metrics with additional data' do
183
- expected_event = Log::Event.new(
184
- message: 'a metric controller.action=0.123',
185
- foo: 'bar',
186
- name: 'controller.action',
187
- value: 0.123,
188
- success: true,
189
- type: 'metric',
190
- severity: 'info'
191
- )
192
- expect(instance).to receive(:emit).with(expected_event)
193
- Log.metric(name: 'controller.action', value: 0.123, foo: 'bar')
194
- end
195
-
196
- it 'can be sent after measuring times' do
197
- expected_event = Log::Event.new(
198
- message: 'a metric foo=10.0',
199
- name: 'foo',
200
- value: 10.0,
201
- success: true,
202
- duration: 10.0,
203
- timestamp: "2011-11-11T10:11:21.000Z",
204
- type: 'metric',
205
- severity: 'info'
206
- )
207
- expect(instance).to receive(:emit).with(expected_event)
208
- Log.metric(name: 'foo', success: -> result { result == :success }) do
209
- Time.dummy = Time.now + 10
210
- :success
211
- end
212
- end
213
-
214
- class MyEx < StandardError
215
- def backtrace
216
- %w[ backtrace ]
217
- end
218
- end
219
-
220
- it 'adds exception information if block raises' do
221
- expected_event = Log::Event.new(
222
- name: 'foo',
223
- value: 3.0,
224
- success: false,
225
- duration: 3.0,
226
- timestamp: "2011-11-11T10:11:14.000Z",
227
- message: '"MyEx: we were fucked" while measuring metric foo',
228
- error_class: 'MyEx',
229
- backtrace: %w[ backtrace ],
230
- type: 'metric',
231
- severity: 'info'
232
- )
233
- expect(instance).to receive(:emit).with(expected_event)
234
- raised = false
235
- begin
236
- Log.metric(name: 'foo', success: -> result { result == :success }) do
237
- Time.dummy = Time.now + 3
238
- raise MyEx, "we were fucked"
239
- Time.dummy = Time.now + 7
240
- :success
241
- end
242
- rescue MyEx
243
- raised = true
244
- end
245
- expect(raised).to eq true
246
- end
247
-
248
- end
249
162
  end
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,7 @@ if ENV['START_SIMPLECOV'].to_i == 1
6
6
  end
7
7
  require 'rspec'
8
8
  begin
9
- require 'byebug'
9
+ require 'debug'
10
10
  rescue LoadError
11
11
  end
12
12
  require 'betterlog'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betterlog
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - betterplace Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-16 00:00:00.000000000 Z
11
+ date: 2023-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: debug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: all_images
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: tins
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -188,6 +216,7 @@ extra_rdoc_files:
188
216
  - lib/betterlog/railtie.rb
189
217
  - lib/betterlog/version.rb
190
218
  files:
219
+ - ".all_images.yml"
191
220
  - ".github/workflows/codeql-analysis.yml"
192
221
  - ".gitignore"
193
222
  - ".semaphore/semaphore.yml"
@@ -239,7 +268,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
239
268
  - !ruby/object:Gem::Version
240
269
  version: '0'
241
270
  requirements: []
242
- rubygems_version: 3.3.25
271
+ rubygems_version: 3.3.26
243
272
  signing_key:
244
273
  specification_version: 4
245
274
  summary: Structured logging support for bp