berkeley_library-logging 0.2.4 → 0.2.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,11 +6,11 @@ module BerkeleyLibrary
6
6
  describe Loggers do
7
7
  attr_reader :orig_rails_env
8
8
 
9
- before(:each) do
9
+ before do
10
10
  @orig_rails_env = Rails.env
11
11
  end
12
12
 
13
- after(:each) do
13
+ after do
14
14
  Rails.env = orig_rails_env
15
15
  end
16
16
 
@@ -46,8 +46,9 @@ module BerkeleyLibrary
46
46
  end
47
47
 
48
48
  describe :default_logger do
49
- before(:each) { @rails_logger = Rails.logger }
50
- after(:each) { Rails.logger = @rails_logger }
49
+ before { @rails_logger = Rails.logger }
50
+
51
+ after { Rails.logger = @rails_logger }
51
52
 
52
53
  it 'returns the Rails logger' do
53
54
  expected_logger = double(::Logger)
@@ -86,7 +87,7 @@ module BerkeleyLibrary
86
87
  describe :new_default_logger do
87
88
  attr_reader :config
88
89
 
89
- before(:each) do
90
+ before do
90
91
  app = Class.new(Rails::Application).new
91
92
  @config = app.config
92
93
  end
@@ -6,7 +6,7 @@ module BerkeleyLibrary
6
6
  attr_reader :app
7
7
  attr_reader :config
8
8
 
9
- before(:each) do
9
+ before do
10
10
  @app = Class.new(Rails::Application).new
11
11
  allow(Rails).to receive(:application).and_return(app)
12
12
  @config = app.config
@@ -16,7 +16,7 @@ module BerkeleyLibrary
16
16
  attr_reader :logging_initializer
17
17
  attr_reader :bootstrap_logger_initializer
18
18
 
19
- before(:each) do
19
+ before do
20
20
  expected_file, _line = Module.const_source_location(BerkeleyLibrary::Logging::Railtie.name)
21
21
  @logging_initializer = app.initializers.find do |init|
22
22
  block = init.block
@@ -0,0 +1,23 @@
1
+ require 'rails_helper'
2
+
3
+ require 'active_support/time'
4
+
5
+ module BerkeleyLibrary
6
+ module Logging
7
+ describe SafeSerializer do
8
+ describe :serialize do
9
+ it 'handles ActiveSupport::TimeWithZone' do
10
+ Time.zone = 'America/Los_Angeles'
11
+
12
+ t = Time.current
13
+ expect(t).to be_a(ActiveSupport::TimeWithZone) # just to be sure
14
+
15
+ expect(SafeSerializer.serialize(t)).to be(t)
16
+
17
+ h = { time: t }
18
+ expect(SafeSerializer.serialize(h)).to eq(h)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -25,7 +25,7 @@ module BerkeleyLibrary
25
25
  mock_logger_class.new
26
26
  end
27
27
 
28
- before(:each) do
28
+ before do
29
29
  @rails_logger_orig = Rails.logger
30
30
  Rails.logger = new_mock_logger
31
31
 
@@ -35,7 +35,7 @@ module BerkeleyLibrary
35
35
  end
36
36
  end
37
37
 
38
- after(:each) do
38
+ after do
39
39
  @logger_was_defined ? reset_logger! : undefine_logger!
40
40
  Rails.logger = @rails_logger_orig
41
41
  end
@@ -74,7 +74,7 @@ module BerkeleyLibrary
74
74
  describe 'included' do
75
75
  attr_reader :logificator
76
76
 
77
- before(:each) do
77
+ before do
78
78
  @logificator = Object.new
79
79
  @logificator.singleton_class.include(Logging)
80
80
  end
data/spec/spec_helper.rb CHANGED
@@ -8,7 +8,7 @@ if ENV['COVERAGE']
8
8
  spec_group_re = %r{(?<=^#{spec_root}/)[^/]+(?=/)}
9
9
 
10
10
  RSpec.configure do |config|
11
- config.before(:each) do |example|
11
+ config.before do |example|
12
12
  abs_path = File.realpath(example.metadata[:absolute_file_path])
13
13
  match_data = spec_group_re.match(abs_path)
14
14
  raise ArgumentError, "Unable to determine group for example at #{abs_path}" unless match_data
@@ -8,7 +8,7 @@ module BerkeleyLibrary
8
8
  describe :configure! do
9
9
  attr_reader :config
10
10
 
11
- before(:each) do
11
+ before do
12
12
  @stdout_orig = $stdout
13
13
  $stdout = StringIO.new
14
14
 
@@ -16,7 +16,7 @@ module BerkeleyLibrary
16
16
  config.lograge = Lograge::OrderedOptions.new
17
17
  end
18
18
 
19
- after(:each) do
19
+ after do
20
20
  $stdout = @stdout_orig
21
21
  end
22
22
 
@@ -9,7 +9,7 @@ module BerkeleyLibrary
9
9
  describe :new_json_formatter do
10
10
  attr_reader :out, :logger
11
11
 
12
- before(:each) do
12
+ before do
13
13
  @out = StringIO.new
14
14
  @logger = Logger.new(out)
15
15
  logger.formatter = Formatters.new_json_formatter
@@ -66,6 +66,7 @@ module BerkeleyLibrary
66
66
  expect(additional_data['more_strings']).to eq([expected_string, expected_string])
67
67
  end
68
68
 
69
+ # rubocop:disable Layout/LineLength
69
70
  it 'removes ANSI formatting from ActiveRecord logs' do
70
71
  original = " \e[1m\e[36mLendingItem Load (2.0ms)\e[0m \e[1m\e[34mSELECT \"lending_items\".* FROM \"lending_items\" WHERE \"lending_items\".\"directory\" = $1 LIMIT $2\e[0m [[\"directory\", \"b135297126_C068087930\"], [\"LIMIT\", 1]]"
71
72
  expected = ' LendingItem Load (2.0ms) SELECT "lending_items".* FROM "lending_items" WHERE "lending_items"."directory" = $1 LIMIT $2 [["directory", "b135297126_C068087930"], ["LIMIT", 1]]'
@@ -74,6 +75,7 @@ module BerkeleyLibrary
74
75
  msg = logged_json['msg']
75
76
  expect(msg).to eq(expected)
76
77
  end
78
+ # rubocop:enable Layout/LineLength
77
79
  end
78
80
 
79
81
  describe :ensure_hash do
@@ -8,7 +8,7 @@ module BerkeleyLibrary
8
8
  attr_reader :out
9
9
 
10
10
  # rubocop:disable Lint/ConstantDefinitionInBlock
11
- before(:each) do
11
+ before do
12
12
  @out = StringIO.new
13
13
  class ::TestError < StandardError
14
14
  attr_writer :cause
@@ -20,7 +20,7 @@ module BerkeleyLibrary
20
20
  end
21
21
  # rubocop:enable Lint/ConstantDefinitionInBlock
22
22
 
23
- after(:each) do
23
+ after do
24
24
  Object.send(:remove_const, :TestError)
25
25
  end
26
26
 
@@ -39,7 +39,6 @@ module BerkeleyLibrary
39
39
  describe :new_json_logger do
40
40
 
41
41
  # TODO: rewrite this as a matcher
42
- # rubocop:disable Metrics/AbcSize
43
42
  def assert_serialized_error(err_json, err)
44
43
  expect(err_json).to be_a(Hash)
45
44
  expect(err_json['name']).to eq(TestError.name)
@@ -52,8 +51,6 @@ module BerkeleyLibrary
52
51
  expect(err_stack).to include(line)
53
52
  end
54
53
  end
55
- # rubocop:enable Metrics/AbcSize
56
-
57
54
  it 'supports tagged logging' do
58
55
  logger = Loggers.new_json_logger(out)
59
56
  logger = ActiveSupport::TaggedLogging.new(logger)
@@ -132,6 +129,9 @@ module BerkeleyLibrary
132
129
  rescue SystemStackError => e
133
130
  RSpec::Expectations.fail_with("Expected no SystemStackError, but got #{e}: #{e.backtrace[0]}")
134
131
  end
132
+
133
+ expect(out.string).to include(msg_outer)
134
+ expect(out.string).to include(msg_inner)
135
135
  end
136
136
  # rubocop:enable Naming/RescuedExceptionsVariableName
137
137
 
@@ -326,11 +326,11 @@ module BerkeleyLibrary
326
326
  describe :new_default_logger do
327
327
  attr_reader :config
328
328
 
329
- before(:each) do
329
+ before do
330
330
  @config = OpenStruct.new
331
331
  end
332
332
 
333
- after(:each) do
333
+ after do
334
334
  BerkeleyLibrary::Logging.instance_variable_set(:@env, nil)
335
335
  end
336
336
 
@@ -0,0 +1,137 @@
1
+ require 'standalone_helper'
2
+
3
+ module BerkeleyLibrary
4
+ module Logging
5
+ describe SafeSerializer do
6
+ describe :serialize do
7
+ it 'returns primitives unchanged' do
8
+ values = [
9
+ nil,
10
+ false,
11
+ true,
12
+ 1,
13
+ 1.0,
14
+ '1/3'.to_r,
15
+ 'a string',
16
+ :a_symbol,
17
+ Date.today,
18
+ Time.now
19
+ ]
20
+
21
+ aggregate_failures do
22
+ values.each do |original|
23
+ actual = SafeSerializer.serialize(original)
24
+ expect(actual).to be(original)
25
+ end
26
+ end
27
+ end
28
+
29
+ it 'returns an object as a string' do
30
+ value = Object.new
31
+ expect(SafeSerializer.serialize(value)).to eq(value.to_s)
32
+ end
33
+
34
+ it 'returns a hash as a hash' do
35
+ hash = { a: 1, b: 2 }
36
+ expect(SafeSerializer.serialize(hash)).to eq(hash)
37
+ end
38
+
39
+ it 'returns an array as an array' do
40
+ arr = [0, 1, 2, 'elvis', false]
41
+ expect(SafeSerializer.serialize(arr)).to eq(arr)
42
+ end
43
+
44
+ it 'cleans nested values' do
45
+ b_value = Object.new
46
+ c_key = Object.new
47
+ d_value = Object.new
48
+
49
+ d_array = ['d', :d, { d: "\ud7ff", 'd' => d_value }]
50
+
51
+ h = {
52
+ a: 1,
53
+ b: b_value,
54
+ c_key => 'c value',
55
+ 0xd => d_array,
56
+ "\uEEEE" => 0xe
57
+ }
58
+
59
+ expected = {
60
+ a: 1,
61
+ b: b_value.to_s,
62
+ c_key.to_s => 'c value',
63
+ 0xd => ['d', :d, { d: "\ud7ff", 'd' => d_value.to_s }],
64
+ "\uEEEE" => 0xe
65
+ }
66
+
67
+ actual = SafeSerializer.serialize(h)
68
+ expect(actual).to eq(expected)
69
+ end
70
+
71
+ it 'handles recursive structures' do
72
+ a = []
73
+ h = { a: a }
74
+ a << h
75
+
76
+ h_expected = { a: [SafeSerializer.placeholder_for(h)] }
77
+ a_expected = [{ a: SafeSerializer.placeholder_for(a) }]
78
+
79
+ expect(SafeSerializer.serialize(h)).to eq(h_expected)
80
+ expect(SafeSerializer.serialize(a)).to eq(a_expected)
81
+ end
82
+
83
+ context 'exceptions' do
84
+ # rubocop:disable Lint/ConstantDefinitionInBlock
85
+ before do
86
+ class ::TestError < StandardError
87
+ attr_writer :cause
88
+
89
+ def cause
90
+ @cause || super
91
+ end
92
+ end
93
+ end
94
+ # rubocop:enable Lint/ConstantDefinitionInBlock
95
+
96
+ after do
97
+ Object.send(:remove_const, :TestError)
98
+ end
99
+
100
+ # rubocop:disable Naming/RescuedExceptionsVariableName
101
+ it 'handles exceptions' do
102
+ msg_outer = 'Help I am trapped in the outer part of a unit test'
103
+ msg_inner = 'Help I am trapped in the inner part of a unit test'
104
+
105
+ begin
106
+ raise TestError, msg_inner
107
+ rescue TestError => ex_inner
108
+ begin
109
+ raise TestError, msg_outer
110
+ rescue TestError => ex_outer
111
+ ex_inner.cause = ex_outer
112
+ end
113
+ end
114
+
115
+ expect(ex_outer.cause).to eq(ex_inner) # just to be sure
116
+ expect(ex_inner.cause).to eq(ex_outer) # just to be sure
117
+
118
+ expected = {
119
+ name: 'TestError',
120
+ message: msg_outer,
121
+ stack: ex_outer.backtrace,
122
+ cause: {
123
+ name: 'TestError',
124
+ message: msg_inner,
125
+ stack: ex_inner.backtrace
126
+ }
127
+ }
128
+
129
+ expect(SafeSerializer.serialize(ex_outer)).to eq(expected)
130
+ end
131
+ # rubocop:enable Naming/RescuedExceptionsVariableName
132
+
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -25,7 +25,7 @@ module BerkeleyLibrary
25
25
  mock_logger_class.new
26
26
  end
27
27
 
28
- before(:each) do
28
+ before do
29
29
  if (@logger_was_defined = logger_defined?)
30
30
  @logging_logger_orig = Logging.instance_variable_get(:@logger)
31
31
  undefine_logger!
@@ -37,7 +37,7 @@ module BerkeleyLibrary
37
37
  allow(Logging::Loggers).to receive(:default_logger).and_return(persistent_default_logger)
38
38
  end
39
39
 
40
- after(:each) do
40
+ after do
41
41
  @logger_was_defined ? reset_logger! : undefine_logger!
42
42
  end
43
43
 
@@ -75,7 +75,7 @@ module BerkeleyLibrary
75
75
  describe 'included' do
76
76
  attr_reader :logificator
77
77
 
78
- before(:each) do
78
+ before do
79
79
  @logificator = Object.new
80
80
  @logificator.singleton_class.include(Logging)
81
81
  end
@@ -7,7 +7,7 @@ require 'spec_helper'
7
7
  # RSpec
8
8
 
9
9
  RSpec.configure do |config|
10
- config.around(:example) do |example|
10
+ config.around do |example|
11
11
  next example.run unless defined?(Rails)
12
12
 
13
13
  rails_orig = Object.send(:remove_const, :Rails)
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berkeley_library-logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Moles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-03 00:00:00.000000000 Z
11
+ date: 2022-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '6.0'
19
+ version: '6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '6.0'
26
+ version: '6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: amazing_print
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -174,16 +174,16 @@ dependencies:
174
174
  name: rails
175
175
  requirement: !ruby/object:Gem::Requirement
176
176
  requirements:
177
- - - "~>"
177
+ - - ">="
178
178
  - !ruby/object:Gem::Version
179
- version: '6.0'
179
+ version: '6'
180
180
  type: :development
181
181
  prerelease: false
182
182
  version_requirements: !ruby/object:Gem::Requirement
183
183
  requirements:
184
- - - "~>"
184
+ - - ">="
185
185
  - !ruby/object:Gem::Version
186
- version: '6.0'
186
+ version: '6'
187
187
  - !ruby/object:Gem::Dependency
188
188
  name: rake
189
189
  requirement: !ruby/object:Gem::Requirement
@@ -218,14 +218,28 @@ dependencies:
218
218
  requirements:
219
219
  - - "~>"
220
220
  - !ruby/object:Gem::Version
221
- version: 0.91.0
221
+ version: 1.26.0
222
+ type: :development
223
+ prerelease: false
224
+ version_requirements: !ruby/object:Gem::Requirement
225
+ requirements:
226
+ - - "~>"
227
+ - !ruby/object:Gem::Version
228
+ version: 1.26.0
229
+ - !ruby/object:Gem::Dependency
230
+ name: rubocop-rspec
231
+ requirement: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - "~>"
234
+ - !ruby/object:Gem::Version
235
+ version: 2.4.0
222
236
  type: :development
223
237
  prerelease: false
224
238
  version_requirements: !ruby/object:Gem::Requirement
225
239
  requirements:
226
240
  - - "~>"
227
241
  - !ruby/object:Gem::Version
228
- version: 0.91.0
242
+ version: 2.4.0
229
243
  - !ruby/object:Gem::Dependency
230
244
  name: simplecov
231
245
  requirement: !ruby/object:Gem::Requirement
@@ -297,11 +311,13 @@ files:
297
311
  - lib/berkeley_library/logging/configurator.rb
298
312
  - lib/berkeley_library/logging/env.rb
299
313
  - lib/berkeley_library/logging/events.rb
314
+ - lib/berkeley_library/logging/exception_serializer.rb
300
315
  - lib/berkeley_library/logging/formatters.rb
301
316
  - lib/berkeley_library/logging/logger.rb
302
317
  - lib/berkeley_library/logging/loggers.rb
303
318
  - lib/berkeley_library/logging/module_info.rb
304
319
  - lib/berkeley_library/logging/railtie.rb
320
+ - lib/berkeley_library/logging/safe_serializer.rb
305
321
  - lib/berkeley_library/logging/tagged_logging_extensions.rb
306
322
  - rakelib/.rubocop.yml
307
323
  - rakelib/bundle.rake
@@ -315,18 +331,21 @@ files:
315
331
  - spec/rails/ucblit/logging/formatters_spec.rb
316
332
  - spec/rails/ucblit/logging/loggers_spec.rb
317
333
  - spec/rails/ucblit/logging/railtie_spec.rb
334
+ - spec/rails/ucblit/logging/safe_serializer_spec.rb
318
335
  - spec/rails/ucblit/logging_spec.rb
319
336
  - spec/rails_helper.rb
320
337
  - spec/spec_helper.rb
321
338
  - spec/standalone/ucblit/logging/configurator_spec.rb
322
339
  - spec/standalone/ucblit/logging/formatters_spec.rb
323
340
  - spec/standalone/ucblit/logging/loggers_spec.rb
341
+ - spec/standalone/ucblit/logging/safe_serializer_spec.rb
324
342
  - spec/standalone/ucblit/logging_spec.rb
325
343
  - spec/standalone_helper.rb
326
344
  homepage: https://github.com/BerkeleyLibrary/logging
327
345
  licenses:
328
346
  - MIT
329
- metadata: {}
347
+ metadata:
348
+ rubygems_mfa_required: 'true'
330
349
  post_install_message:
331
350
  rdoc_options: []
332
351
  require_paths:
@@ -342,22 +361,25 @@ required_rubygems_version: !ruby/object:Gem::Requirement
342
361
  - !ruby/object:Gem::Version
343
362
  version: '0'
344
363
  requirements: []
345
- rubygems_version: 3.1.4
364
+ rubygems_version: 3.1.6
346
365
  signing_key:
347
366
  specification_version: 4
348
367
  summary: Opinionated Ruby/Rails logging for UC Berkeley Library
349
368
  test_files:
369
+ - artifacts/.keep
350
370
  - spec/.rubocop.yml
351
371
  - spec/rails/ucblit/logging/configurator_spec.rb
352
372
  - spec/rails/ucblit/logging/env_spec.rb
353
373
  - spec/rails/ucblit/logging/formatters_spec.rb
354
374
  - spec/rails/ucblit/logging/loggers_spec.rb
355
375
  - spec/rails/ucblit/logging/railtie_spec.rb
376
+ - spec/rails/ucblit/logging/safe_serializer_spec.rb
356
377
  - spec/rails/ucblit/logging_spec.rb
357
378
  - spec/rails_helper.rb
358
379
  - spec/spec_helper.rb
359
380
  - spec/standalone/ucblit/logging/configurator_spec.rb
360
381
  - spec/standalone/ucblit/logging/formatters_spec.rb
361
382
  - spec/standalone/ucblit/logging/loggers_spec.rb
383
+ - spec/standalone/ucblit/logging/safe_serializer_spec.rb
362
384
  - spec/standalone/ucblit/logging_spec.rb
363
385
  - spec/standalone_helper.rb