lorekeeper 1.7.2 → 1.8.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: d8b0a7ef791f5abaf9fc2452ac135e9c5ec37510b879b19a02039cb3d1096342
4
- data.tar.gz: 418c008350fd3f1a221291266b3ab39e59d8333f796148bffba16f717399c066
3
+ metadata.gz: b72498ad2cf067d82c9371e7affa84ce85974332d2eaa962d34fa9b687ff897f
4
+ data.tar.gz: af9356d08c95dbadd903dd340a3a3acbe8f1d26768333e20add816f232f93bf8
5
5
  SHA512:
6
- metadata.gz: fb7b5c1889b2e133bfe6f6a55be4424bcdfbe26718f2096889b1409d09609678ad57fc50fa2363fa17b7e59f79342422e1ac2ff3c22fd7572fec14fa09da5692
7
- data.tar.gz: 6933ce6d182c41fd7fd23950df831bc3e9f93c3d72c31198dd93906882f50a24395919b5cb2ba1a65587c0a5adc240b397872a27e25e2a7e251324661669e795
6
+ metadata.gz: a1e4267df3ae911fccb7245e22f6c8271df78e4d454e39740fb966cb881cadeb27ee67cfef54b3f580ffa554bd1c0370895c9287af6b5c25a8fd5dd8ed90659e
7
+ data.tar.gz: a7e7c0f9c916abf6bb575053ba6a5ce29832ff0eb0f4bdc7f9044bd2a615c0eacb5f6c0a9aa32e063d11bcce83f68fa67ecc86875b379db9f9e3bc77efee22b3
@@ -1,3 +1,6 @@
1
+ # 1.8.0
2
+ * Allow to use named parameters in the .exception method
3
+
1
4
  # 1.7.2
2
5
  * Add a second parameter to respond_to? to avoid Ruby 2.6 warnings
3
6
  * Drop support for unsupported Ruby 2.1 and Ruby 2.2
data/README.md CHANGED
@@ -169,6 +169,24 @@ Will output:
169
169
  }
170
170
  ```
171
171
 
172
+ Alternatively you can use named parameters:
173
+
174
+
175
+ ```ruby
176
+ rescue => e
177
+ logger.exception(e, message: "custom msg!", data: { some: { data: 123 } }, level: :warn)
178
+ end
179
+ ```
180
+
181
+ This is specially useful when there is no custom message or data:
182
+
183
+ ```ruby
184
+ rescue => e
185
+ logger.exception(e, level: :warn)
186
+ end
187
+ ```
188
+
189
+
172
190
 
173
191
  ## License
174
192
 
data/Rakefile CHANGED
@@ -50,10 +50,10 @@ task :benchmark do
50
50
  simple_log = create_simple_logger
51
51
 
52
52
  Benchmark.ips do |bm|
53
- bm.report('short content') { log.error(contents) }
54
- bm.report('Logger short content') { simple_log.info(contents) }
55
- bm.report('long content') { log.info(long_contents) }
56
- bm.report('Logger long content') { simple_log.info(long_contents) }
53
+ bm.report('JSON short content') { log.debug(contents) }
54
+ bm.report('Logger short content') { simple_log.debug(contents) }
55
+ bm.report('JSON long content') { log.debug(long_contents) }
56
+ bm.report('Logger long content') { simple_log.debug(long_contents) }
57
57
  bm.compare!
58
58
  end
59
59
 
@@ -29,7 +29,7 @@ module Lorekeeper
29
29
  LOGGING_METHODS.each do |method_name|
30
30
  define_method "#{method_name}_with_data", ->(message_param = nil, data = {}, &block) do
31
31
  return true if METHOD_SEVERITY_MAP[method_name] < @level
32
- extra_fields = { 'data' => (data || {}) }
32
+ extra_fields = { DATA => (data || {}) }
33
33
  with_extra_fields(extra_fields) { # Using do/end here only valid on Ruby>= 2.3
34
34
  add(METHOD_SEVERITY_MAP[method_name], message_param, nil, &block)
35
35
  }
@@ -61,25 +61,31 @@ module Lorekeeper
61
61
  end
62
62
 
63
63
  # @param exception: instance of a class inheriting from Exception
64
- # We will output backtrace twice. Once inside the stack so it can be parsed by software
65
- # And the other inside the message so it is readable to humans
66
- def exception(exception, custom_message = nil, custom_data = nil, level = :error)
67
- log_level = METHOD_SEVERITY_MAP[level] || ERROR
64
+ # By default message comes from exception.message
65
+ # Optional and named parameters to overwrite message, level and add data
66
+ def exception(exception, custom_message = nil, custom_data = nil, custom_level = :error,
67
+ message: nil, data: nil, level: nil) # Backwards compatible named params
68
+
69
+ param_level = level || custom_level
70
+ param_data = data || custom_data
71
+ param_message = message || custom_message
72
+
73
+ log_level = METHOD_SEVERITY_MAP[param_level] || ERROR
68
74
 
69
75
  if exception.is_a?(Exception)
70
76
  backtrace = exception.backtrace || []
71
77
  exception_fields = {
72
- 'exception' => "#{exception.class}: #{exception.message}",
73
- 'stack' => backtrace
78
+ EXCEPTION => "#{exception.class}: #{exception.message}",
79
+ STACK => backtrace
74
80
  }
75
- exception_fields['data'] = custom_data if custom_data
81
+ exception_fields[DATA] = param_data if param_data
76
82
 
77
- message = custom_message || exception.message
83
+ message = param_message || exception.message
78
84
  with_extra_fields(exception_fields) { log_data(log_level, message) }
79
85
  else
80
86
  log_data(METHOD_SEVERITY_MAP[:warn], 'Logger exception called without exception class.')
81
- message = "#{exception.class}: #{exception.inspect} #{custom_message}"
82
- with_extra_fields('data' => (custom_data || {})) { log_data(log_level, message) }
87
+ message = "#{exception.class}: #{exception.inspect} #{param_message}"
88
+ with_extra_fields(DATA => (param_data || {})) { log_data(log_level, message) }
83
89
  end
84
90
  end
85
91
 
@@ -94,6 +100,9 @@ module Lorekeeper
94
100
  MESSAGE = 'message'
95
101
  TIMESTAMP = 'timestamp'
96
102
  DATE_FORMAT = '%FT%T.%6NZ'
103
+ EXCEPTION = 'exception'
104
+ STACK = 'stack'
105
+ DATA = 'data'
97
106
 
98
107
  def with_extra_fields(fields)
99
108
  state[:extra_fields] = fields
@@ -108,11 +117,12 @@ module Lorekeeper
108
117
  end
109
118
 
110
119
  def log_data(severity, message)
120
+ current_state = state # Accessing state is slow. Do it only once per call.
111
121
  # merging is slow, we do not want to merge with empty hash if possible
112
- fields_to_log = if state[:extra_fields].empty?
113
- state[:base_fields]
122
+ fields_to_log = if current_state[:extra_fields].empty?
123
+ current_state[:base_fields]
114
124
  else
115
- state[:base_fields].merge(state[:extra_fields])
125
+ current_state[:base_fields].merge(current_state[:extra_fields])
116
126
  end
117
127
 
118
128
  fields_to_log[MESSAGE] = message
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lorekeeper
4
- VERSION = '1.7.2'
4
+ VERSION = '1.8.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lorekeeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordi Polo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-24 00:00:00.000000000 Z
11
+ date: 2019-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj