berkeley_library-logging 0.2.1 → 0.2.2

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: f88d32f15e7f95d27b1ec0ba158068a7d2950a22e89ae2b169fccc4aa385c3f6
4
- data.tar.gz: '07193aef296c9b347993c02304ff506b9789ec45e6362b86180ab9da00c84519'
3
+ metadata.gz: 1c60520b7195f791061df89827e2a6c7b00ac1352262d7cf750511a125d30c02
4
+ data.tar.gz: bcb3ddb18cb7bfa442a52bbe5116316b74ae7f6d7c4e2e414812a5838e4a84fa
5
5
  SHA512:
6
- metadata.gz: a1d6d0b7d9692ef52ea6b266d2cdf782d90bea3caa1cacc785191445b21b1fdf0daf6126ebdf0f45278635c3d9abed4f5b239dc4d013aab91410a2cb949165d5
7
- data.tar.gz: 8eba0dc24f2d163827d415ccbbe780d0f3b23c3d5b6e583573b55c15a48d11e1c1ab6098c33cdc18b98461945f9ca19fad8c7158b572c9c8ec2825e35fd52a69
6
+ metadata.gz: '085e7cc941c61021cfa90d6c2f2cb7c53d05d8900dc067aa87cc2ba6302e96032e31683ff3e9d1d2ba62770556569ebda96566f81c8eee0eec50c7683ed00b36'
7
+ data.tar.gz: 0ca861efeca0e9b7c94cf87bbf410a0d826cfd9a1918e5b351148f102a9b8bbc64e4e7979aeb6044ab122def7d6bf82e798447ed19fe36ef7a34b47fa502ad61
@@ -6,7 +6,7 @@ jobs:
6
6
  fail-fast: false
7
7
  matrix:
8
8
  os: [ ubuntu-latest, macos-latest ]
9
- ruby-version: [ '2.7', '3.0' ]
9
+ ruby: [ '2.7', '3.0' ]
10
10
  runs-on: ${{ matrix.os }}
11
11
 
12
12
  steps:
data/.simplecov CHANGED
@@ -1,4 +1,15 @@
1
1
  SimpleCov.start 'rails' do
2
2
  add_filter 'module_info.rb'
3
- formatter SimpleCov::Formatter::SimpleFormatter
3
+
4
+ if ENV['GITHUB_ACTION']
5
+ formatter SimpleCov::Formatter::SimpleFormatter
6
+ else
7
+ # TODO: figure out why this doesn't work in CI
8
+ require 'simplecov-rcov'
9
+ coverage_dir 'artifacts'
10
+
11
+ SimpleCov.collate Dir['artifacts/simplecov/**/.resultset.json'] do
12
+ formatter SimpleCov::Formatter::RcovFormatter
13
+ end
14
+ end
4
15
  end
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.2.2 (2021-09-02)
2
+
3
+ - JSON formatter now strips ANSI color escapes from strings
4
+ (fixes [#1](https://github.com/BerkeleyLibrary/logging/issues/1))
5
+
1
6
  # 0.2.1 (2021-08-19)
2
7
 
3
8
  - Recursively log error cause
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_dependency 'activesupport', '~> 6.0'
29
29
  spec.add_dependency 'amazing_print', '~> 1.1'
30
+ spec.add_dependency 'colorize', '~> 0.8.1'
30
31
  spec.add_dependency 'lograge', '~> 0.11'
31
32
  spec.add_dependency 'ougai', '~> 1.8'
32
33
 
@@ -23,6 +23,15 @@ module BerkeleyLibrary
23
23
 
24
24
  { msg: message }
25
25
  end
26
+
27
+ def decolorize(message)
28
+ return unless message
29
+ return message.uncolorize if message.is_a?(String)
30
+ return message.map { |v| decolorize(v) } if message.is_a?(Array)
31
+ return message.transform_values { |v| decolorize(v) } if message.is_a?(Hash)
32
+
33
+ message
34
+ end
26
35
  end
27
36
 
28
37
  # ------------------------------------------------------------
@@ -78,11 +87,12 @@ module BerkeleyLibrary
78
87
 
79
88
  def _call(severity, time, progname, data)
80
89
  original_data = Formatters.ensure_hash(data)
90
+ decolorized_data = Formatters.decolorize(original_data)
81
91
 
82
92
  # Ougai::Formatters::Bunyan replaces the human-readable severity string
83
93
  # with a numeric level, so we add it here as a separate attribute
84
94
  severity = ensure_human_readable(severity)
85
- merged_data = { severity: severity }.merge(original_data)
95
+ merged_data = { severity: severity }.merge(decolorized_data)
86
96
  super(severity, time, progname, merged_data)
87
97
  end
88
98
 
@@ -7,7 +7,7 @@ module BerkeleyLibrary
7
7
  SUMMARY = 'Opinionated Ruby/Rails logging for UC Berkeley Library'.freeze
8
8
  DESCRIPTION = 'A gem providing shared logging code for UC Berkeley Library gems and Rails applications'.freeze
9
9
  LICENSE = 'MIT'.freeze
10
- VERSION = '0.2.1'.freeze
10
+ VERSION = '0.2.2'.freeze
11
11
  HOMEPAGE = 'https://github.com/BerkeleyLibrary/logging'.freeze
12
12
 
13
13
  private_class_method :new
@@ -1,27 +1,72 @@
1
1
  require 'rails_helper'
2
2
  require 'json'
3
+ require 'colorize'
3
4
  require 'berkeley_library/logging'
4
5
 
5
6
  module BerkeleyLibrary
6
7
  module Logging
7
8
  describe Formatters do
8
9
  describe :new_json_formatter do
9
- it 'supports tagged logging' do
10
- out = StringIO.new
11
- logger = Logger.new(out)
10
+ attr_reader :out, :logger
11
+
12
+ before(:each) do
13
+ @out = StringIO.new
14
+ @logger = Logger.new(out)
12
15
  logger.formatter = Formatters.new_json_formatter
16
+ end
13
17
 
14
- logger = ActiveSupport::TaggedLogging.new(logger)
18
+ it 'supports tagged logging' do
19
+ tagged_logger = ActiveSupport::TaggedLogging.new(logger)
15
20
 
16
21
  expected_tag = 'hello'
17
22
  expected_msg = 'this is a test'
18
23
 
19
- logger.tagged(expected_tag) { logger.info(expected_msg) }
24
+ tagged_logger.tagged(expected_tag) { tagged_logger.info(expected_msg) }
20
25
 
21
26
  logged_json = JSON.parse(out.string)
22
27
  expect(logged_json['msg']).to eq(expected_msg)
23
28
  expect(logged_json['tags']).to eq([expected_tag])
24
29
  end
30
+
31
+ it 'decolorizes ANSI-colored strings' do
32
+ colors = %i[red green yellow blue magenta cyan]
33
+ colorized_string = colors.map { |c| c.to_s.colorize(c) }.join(' ')
34
+ expect(colorized_string).to include("\u001b") # just to be sure
35
+
36
+ expected_string = colors.map(&:to_s).join(' ')
37
+
38
+ logger.info(colorized_string)
39
+ logged_json = JSON.parse(out.string)
40
+ msg = logged_json['msg']
41
+ expect(msg).not_to include("\u001b")
42
+ expect(msg).to eq(expected_string)
43
+ end
44
+
45
+ it 'decolorizes ANSI-colored strings in attached data' do
46
+ colors = %i[red green yellow blue magenta cyan]
47
+ colorized_string = colors.map { |c| c.to_s.colorize(c) }.join(' ')
48
+ expect(colorized_string).to include("\u001b") # just to be sure
49
+
50
+ expected_string = colors.map(&:to_s).join(' ')
51
+
52
+ data = {
53
+ the_string: colorized_string,
54
+ additional_data: {
55
+ not_a_string: 12,
56
+ another_string: colorized_string,
57
+ more_strings: [colorized_string, colorized_string]
58
+ }
59
+ }
60
+ logger.info('a colorized string', data)
61
+
62
+ logged_json = JSON.parse(out.string)
63
+ data = logged_json
64
+ expect(data['the_string']).to eq(expected_string)
65
+ additional_data = data['additional_data']
66
+ expect(additional_data['not_a_string']).to eq(12)
67
+ expect(additional_data['another_string']).to eq(expected_string)
68
+ expect(additional_data['more_strings']).to eq([expected_string, expected_string])
69
+ end
25
70
  end
26
71
 
27
72
  describe :ensure_hash do
@@ -14,6 +14,20 @@ module BerkeleyLibrary
14
14
  Rails.env = orig_rails_env
15
15
  end
16
16
 
17
+ describe :new_readable_logger do
18
+ it 'logs ANSI colors' do
19
+ out = StringIO.new
20
+
21
+ logger = Loggers.new_readable_logger(out)
22
+ colors = %i[red green yellow blue magenta cyan]
23
+ colorized_string = colors.map { |c| c.to_s.colorize(c) }.join(' ')
24
+ expect(colorized_string).to include("\u001b") # just to be sure
25
+
26
+ logger.info(colorized_string)
27
+ expect(out.string).to include(colorized_string)
28
+ end
29
+ end
30
+
17
31
  describe :new_json_logger do
18
32
  it 'supports tagged logging' do
19
33
  out = StringIO.new
@@ -1,27 +1,70 @@
1
1
  require 'standalone_helper'
2
2
  require 'json'
3
+ require 'colorize'
3
4
  require 'berkeley_library/logging'
4
5
 
5
6
  module BerkeleyLibrary
6
7
  module Logging
7
8
  describe Formatters do
8
9
  describe :new_json_formatter do
9
- it 'supports tagged logging' do
10
- out = StringIO.new
11
- logger = Logger.new(out)
10
+ attr_reader :out, :logger
11
+
12
+ before(:each) do
13
+ @out = StringIO.new
14
+ @logger = Logger.new(out)
12
15
  logger.formatter = Formatters.new_json_formatter
16
+ end
13
17
 
14
- logger = ActiveSupport::TaggedLogging.new(logger)
18
+ it 'supports tagged logging' do
19
+ tagged_logger = ActiveSupport::TaggedLogging.new(logger)
15
20
 
16
21
  expected_tag = 'hello'
17
22
  expected_msg = 'this is a test'
18
23
 
19
- logger.tagged(expected_tag) { logger.info(expected_msg) }
24
+ tagged_logger.tagged(expected_tag) { tagged_logger.info(expected_msg) }
20
25
 
21
26
  logged_json = JSON.parse(out.string)
22
27
  expect(logged_json['msg']).to eq(expected_msg)
23
28
  expect(logged_json['tags']).to eq([expected_tag])
24
29
  end
30
+
31
+ it 'decolorizes ANSI-colored strings' do
32
+ colors = %i[red green yellow blue magenta cyan]
33
+ colorized_string = colors.map { |c| c.to_s.colorize(c) }.join(' ')
34
+ expect(colorized_string).to include("\u001b") # just to be sure
35
+
36
+ expected_string = colors.map(&:to_s).join(' ')
37
+
38
+ logger.info(colorized_string)
39
+ logged_json = JSON.parse(out.string)
40
+ msg = logged_json['msg']
41
+ expect(msg).not_to include("\u001b")
42
+ expect(msg).to eq(expected_string)
43
+ end
44
+
45
+ it 'decolorizes ANSI-colored strings in attached data' do
46
+ colors = %i[red green yellow blue magenta cyan]
47
+ colorized_string = colors.map { |c| c.to_s.colorize(c) }.join(' ')
48
+ expect(colorized_string).to include("\u001b") # just to be sure
49
+
50
+ expected_string = colors.map(&:to_s).join(' ')
51
+
52
+ data = {
53
+ the_string: colorized_string,
54
+ additional_data: {
55
+ another_string: colorized_string,
56
+ more_strings: [colorized_string, colorized_string]
57
+ }
58
+ }
59
+ logger.info('a colorized string', data)
60
+
61
+ logged_json = JSON.parse(out.string)
62
+ data = logged_json
63
+ expect(data['the_string']).to eq(expected_string)
64
+ additional_data = data['additional_data']
65
+ expect(additional_data['another_string']).to eq(expected_string)
66
+ expect(additional_data['more_strings']).to eq([expected_string, expected_string])
67
+ end
25
68
  end
26
69
 
27
70
  describe :ensure_hash do
@@ -1,5 +1,6 @@
1
1
  require 'standalone_helper'
2
2
  require 'json'
3
+ require 'colorize'
3
4
 
4
5
  module BerkeleyLibrary
5
6
  module Logging
@@ -23,6 +24,18 @@ module BerkeleyLibrary
23
24
  Object.send(:remove_const, :TestError)
24
25
  end
25
26
 
27
+ describe :new_readable_logger do
28
+ it 'logs ANSI colors' do
29
+ logger = Loggers.new_readable_logger(out)
30
+ colors = %i[red green yellow blue magenta cyan]
31
+ colorized_string = colors.map { |c| c.to_s.colorize(c) }.join(' ')
32
+ expect(colorized_string).to include("\u001b") # just to be sure
33
+
34
+ logger.info(colorized_string)
35
+ expect(out.string).to include(colorized_string)
36
+ end
37
+ end
38
+
26
39
  describe :new_json_logger do
27
40
 
28
41
  # TODO: rewrite this as a matcher
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berkeley_library-logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
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-08-19 00:00:00.000000000 Z
11
+ date: 2021-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: lograge
43
57
  requirement: !ruby/object:Gem::Requirement