dry-logger 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d27303efa558c60b1f0f9710b1c9419223d7b421c61b7f6efbea241b54ca486
4
- data.tar.gz: ada0ee3f313045a3cfbcd26e15da7642741d6217efe3e834542935b5efbaa0b9
3
+ metadata.gz: c3b306d2cacde8698d0cdb90777988dca9eb02477828d4b1b1651c301bb294d4
4
+ data.tar.gz: a4af60c05fc7d9845d3a6e7fcd90aa514499c8c6e1172d058c057b6fc232e98f
5
5
  SHA512:
6
- metadata.gz: 5c42481abe364e3550091fe8d1d8fcfbd3e2b23fa00943ca22e8a13f9d6c20cf7bf7846390498c5fb098c7cb45ff9cef461f9bddde50f4382a719e288af9731d
7
- data.tar.gz: c91f8c1e74e4b1dc7fdeaa62870453e303018e7f5e4b0f25152a805cc20c15c04fea719caef6a6feb756f41291f319dd9ebca3b2275dbedc470e4ff22d5ed3fd
6
+ metadata.gz: '096ca26a2ffc2bcf1a824ef1f08b2e4aca1eec5ce7b03df8caec590b527d7fea3b91bd8f5e5461e8037cf7bf289a7a2ab6beac841a9ef85ebc16295f7f89e43a'
7
+ data.tar.gz: 5169ee98f018fecce1e55310f1b41a3324bc3e500287d6f8eb7868851045a84ccc3bee8a4c75d0045a6d493e1e65bbd34f817e5abd5e92387e2f140442e51d18
data/CHANGELOG.md CHANGED
@@ -19,7 +19,31 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
19
19
 
20
20
  ### Security
21
21
 
22
- [Unreleased]: https://github.com/dry-rb/dry-logger/compare/v1.2.1...main
22
+ [Unreleased]: https://github.com/dry-rb/dry-logger/compare/v1.2.2...main
23
+
24
+ ## [1.3.0] - 2026-11-18
25
+
26
+ ### Added
27
+
28
+ - Support `Regexp` log filters, matched against each key and its full dotted path, ignoring case. (@timriley in #43)
29
+
30
+ ### Changed
31
+
32
+ - Match log filters on substrings, ignoring case. For example, `"password"` now also filters `password_confirmation`, and `"user.token"` filters `admin.user.token_hash`. (@timriley in #43)
33
+ - Match string and symbol filters literally. Use a `Regexp` filter (such as `/pass/`) instead of regex syntax in a string. (@timriley in #43)
34
+ - Strip whitespace from the start and end of string and symbol filters. Ignore blank filters, and print a warning. (@timriley in #43)
35
+ - Filter hashes inside arrays, such as `{users: [{password: "secret"}]}`. (@timriley in #43)
36
+ - Only filter inside hashes and arrays, not hash-like `Enumerable` objects. Convert these to hashes (for example, with `#to_h`) before logging. (@timriley in #43)
37
+
38
+ [1.3.0]: https://github.com/dry-rb/dry-logger/compare/v1.2.2...v1.3.0
39
+
40
+ ## [1.2.2] - 2026-03-13
41
+
42
+ ### Fixed
43
+
44
+ - Exception when message and metadata have different encodings (@katafrakt in #40)
45
+
46
+ [1.2.2]: https://github.com/dry-rb/dry-logger/compare/v1.2.1...v1.2.2
23
47
 
24
48
  ## [1.2.1] - 2025-12-16
25
49
 
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-2025 Hanakai team
3
+ Copyright (c) 2015-2026 Hanakai team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
@@ -18,4 +18,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
18
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
19
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
20
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
data/README.md CHANGED
@@ -1,10 +1,15 @@
1
1
  <!--- This file is synced from hanakai-rb/repo-sync -->
2
2
 
3
- [rubygem]: https://rubygems.org/gems/dry-logger
4
3
  [actions]: https://github.com/dry-rb/dry-logger/actions
4
+ [chat]: https://discord.gg/naQApPAsZB
5
+ [forum]: https://discourse.hanakai.org
6
+ [rubygem]: https://rubygems.org/gems/dry-logger
5
7
 
6
8
  # dry-logger [![Gem Version](https://badge.fury.io/rb/dry-logger.svg)][rubygem] [![CI Status](https://github.com/dry-rb/dry-logger/workflows/CI/badge.svg)][actions]
7
9
 
10
+ [![Forum](https://img.shields.io/badge/Forum-dc360f?logo=discourse&logoColor=white)][forum]
11
+ [![Chat](https://img.shields.io/badge/Chat-717cf8?logo=discord&logoColor=white)][chat]
12
+
8
13
  ## Links
9
14
 
10
15
  - [User documentation](https://dry-rb.org/gems/dry-logger)
data/dry-logger.gemspec CHANGED
@@ -17,22 +17,20 @@ Gem::Specification.new do |spec|
17
17
  spec.description = spec.summary
18
18
  spec.homepage = "https://dry-rb.org/gems/dry-logger"
19
19
  spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-logger.gemspec", "lib/**/*"]
20
- spec.bindir = "bin"
21
- spec.executables = []
20
+ spec.bindir = "exe"
21
+ spec.executables = Dir["exe/*"].map { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
24
  spec.extra_rdoc_files = ["README.md", "CHANGELOG.md", "LICENSE"]
25
25
 
26
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
27
26
  spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-logger/blob/main/CHANGELOG.md"
28
27
  spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-logger"
29
28
  spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-logger/issues"
30
29
  spec.metadata["funding_uri"] = "https://github.com/sponsors/hanami"
31
30
 
32
- spec.required_ruby_version = ">= 3.2"
31
+ spec.required_ruby_version = ">= 3.3"
33
32
 
34
33
  spec.add_runtime_dependency "logger"
35
-
36
34
  spec.add_development_dependency "bundler"
37
35
  spec.add_development_dependency "rake"
38
36
  spec.add_development_dependency "rspec"
@@ -21,7 +21,7 @@ module Dry
21
21
 
22
22
  # @since 0.1.0
23
23
  # @api private
24
- def initialize( # rubocop:disable Style/ParameterLists
24
+ def initialize(
25
25
  stream:,
26
26
  formatter:,
27
27
  level: DEFAULT_LEVEL,
@@ -53,7 +53,9 @@ module Dry
53
53
  severity: "FATAL",
54
54
  progname: progname,
55
55
  time: Time.now,
56
- log_entry: [message, payload].map(&:to_s).reject(&:empty?).join(SEPARATOR),
56
+ log_entry: [message, payload].map { |s|
57
+ s.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?")
58
+ }.reject(&:empty?).join(SEPARATOR),
57
59
  exception: exception.class,
58
60
  message: exception.message,
59
61
  backtrace: TAB + exception.backtrace.join(NEW_LINE + TAB)
@@ -44,7 +44,6 @@ module Dry
44
44
 
45
45
  # @since 1.0.0
46
46
  # @api private
47
- # rubocop:disable Metrics/ParameterLists
48
47
  def initialize(clock:, progname:, severity:, tags: EMPTY_ARRAY, message: nil,
49
48
  payload: EMPTY_HASH)
50
49
  @clock = clock
@@ -2,73 +2,124 @@
2
2
 
3
3
  module Dry
4
4
  module Logger
5
- # Filtering logic
6
- # Originaly copied from hanami/utils (see Hanami::Logger)
5
+ # Log filtering logic.
6
+ #
7
+ # Filters match keys as follows:
8
+ #
9
+ # - A string or symbol matches any key where the text appears in the key or its full dotted
10
+ # path. For example, `"password"` matches `password`, `password_confirmation` and
11
+ # `user.new_password`, and `"user.token"` matches `user.token`, `user.token_type` and
12
+ # `admin.user.token`, but not a top-level `token`.
13
+ # - A regexp matches any key where the regexp matches the key or its full dotted path. For
14
+ # example, `/\Apin\z/` matches `pin` and `user.pin`, and `/\Acard\.number\z/` matches
15
+ # `card.number`, but not `payment.card.number`.
16
+ #
17
+ # All filters ignore case, both strings and regexps. To make a regexp filter case-sensitive, use
18
+ # Ruby's inline `(?-i)` modifier, such as `/(?-i)\APin\z/`.
19
+ #
20
+ # Whitespace is stripped from the start and end of string and symbol filters. Fully blank
21
+ # filters are ignored with a warning, since they would match every key.
22
+ #
23
+ # When a key matches and its value is a hash or an array, every value inside it is filtered.
24
+ #
25
+ # Hashes inside arrays are also filtered. Their keys use the array's key in their dotted path,
26
+ # with no index. For example, `"users.password"` matches `{users: [{password: "secret"}]}`.
7
27
  #
8
28
  # @api private
9
29
  class Filter
10
- # @api private
30
+ FILTERED = "[FILTERED]"
31
+ private_constant :FILTERED
32
+
33
+ PATH_SEPARATOR = "."
34
+ private_constant :PATH_SEPARATOR
35
+
36
+ private attr_reader :matcher
37
+
11
38
  def initialize(filters = [])
12
- @filters = filters
39
+ @matcher = build_matcher(reject_blank_filters(strip_filters(filters)))
13
40
  end
14
41
 
15
- # @api private
16
42
  def call(hash)
17
- hash = _deep_dup(hash)
18
-
19
- _filtered_keys(hash).each do |key|
20
- *keys, last = _actual_keys(hash, key.split("."))
21
- keys.inject(hash, :fetch)[last] = "[FILTERED]"
22
- end
23
-
24
- hash
43
+ _filter_hash(hash)
25
44
  end
26
45
 
27
46
  private
28
47
 
29
- attr_reader :filters
30
-
31
- def _filtered_keys(hash)
32
- _key_paths(hash).select { |key|
33
- filters.any? { |filter|
34
- key =~ /(\.|\A)#{filter}(\.|\z)/
35
- }
36
- }
48
+ # Strips whitespace from string filters.
49
+ def strip_filters(filters)
50
+ filters.map { |filter| filter.is_a?(Regexp) ? filter : filter.to_s.strip }
37
51
  end
38
52
 
39
- def _key_paths(hash, base = nil)
40
- hash.inject([]) do |results, (k, v)|
41
- results + (_key_paths?(v) ? _key_paths(v, _build_path(base, k)) : [_build_path(base, k)])
53
+ def reject_blank_filters(filters)
54
+ blank, present = filters.partition { |filter| filter.to_s.empty? }
55
+
56
+ unless blank.empty?
57
+ warn "dry-logger: ignoring blank log filter(s). Blank filters would match every key."
42
58
  end
59
+
60
+ present
43
61
  end
44
62
 
45
- def _build_path(base, key)
46
- [base, key.to_s].compact.join(".")
63
+ # Returns a regexp matching any of the filters, ignoring case. With no filters, it matches
64
+ # nothing.
65
+ #
66
+ # Regexp filters keep their other options. Inline modifiers such as `(?-i)` still apply, so
67
+ # they can opt back into case-sensitive matching.
68
+ def build_matcher(filters)
69
+ Regexp.union(
70
+ filters.map { |filter|
71
+ if filter.is_a?(Regexp)
72
+ Regexp.new(filter.source, filter.options | Regexp::IGNORECASE)
73
+ else
74
+ Regexp.new(Regexp.escape(filter), Regexp::IGNORECASE)
75
+ end
76
+ }
77
+ )
47
78
  end
48
79
 
49
- def _actual_keys(hash, keys)
50
- search_in = hash
80
+ # Returns a filtered copy of the hash, leaving the original unchanged.
81
+ def _filter_hash(hash, parent_path = nil)
82
+ hash.each_with_object({}) do |(key, value), result|
83
+ path = parent_path ? "#{parent_path}#{PATH_SEPARATOR}#{key}" : key.to_s
51
84
 
52
- keys.inject([]) do |res, key|
53
- correct_key = search_in.key?(key.to_sym) ? key.to_sym : key
54
- search_in = search_in[correct_key]
55
- res + [correct_key]
85
+ result[key] =
86
+ if _filter?(key.to_s, path)
87
+ _filtered_value(value)
88
+ else
89
+ _filter_value(value, path)
90
+ end
56
91
  end
57
92
  end
58
93
 
59
- # Check if the given value can be iterated (`Enumerable`) and that isn't a `File`.
60
- # This is useful to detect closed `Tempfiles`.
61
- #
62
- # @see https://github.com/hanami/utils/pull/342
63
- def _key_paths?(value)
64
- value.is_a?(Enumerable) && !value.is_a?(File)
94
+ def _filter_value(value, path)
95
+ case value
96
+ when Hash
97
+ _filter_hash(value, path)
98
+ when Array
99
+ value.map { |item| _filter_value(item, path) }
100
+ else
101
+ value
102
+ end
65
103
  end
66
104
 
67
- # Returns a deeply duplicated hash to avoid mutations of the original.
68
- def _deep_dup(hash)
69
- hash.transform_values { |value|
70
- value.is_a?(Hash) ? _deep_dup(value) : value
71
- }
105
+ # Returns true if any filter matches the key or its full dotted path.
106
+ def _filter?(key, path)
107
+ # Checking the key catches anchored regexps such as `/\Apin\z/` on nested keys. Checking the
108
+ # path catches filters that span keys, such as `"user.token"`.
109
+ matcher.match?(key) || matcher.match?(path)
110
+ end
111
+
112
+ # Filters every value inside a hash or array, keeping hash keys and array lengths so the
113
+ # shape of the data is still visible in the logs.
114
+ def _filtered_value(value)
115
+ case value
116
+ when Hash
117
+ value.transform_values { |nested| _filtered_value(nested) }
118
+ when Array
119
+ value.map { |item| _filtered_value(item) }
120
+ else
121
+ FILTERED
122
+ end
72
123
  end
73
124
  end
74
125
  end
@@ -68,8 +68,19 @@ module Dry
68
68
 
69
69
  # @since 1.0.0
70
70
  # @api private
71
- def %(tokens)
72
- output = value % tokens
71
+ def %(other)
72
+ begin
73
+ output = value % other
74
+ rescue Encoding::CompatibilityError
75
+ sanitized_tokens = other.transform_values { |v|
76
+ if v.respond_to?(:encode)
77
+ v.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?")
78
+ else
79
+ v
80
+ end
81
+ }
82
+ output = value % sanitized_tokens
83
+ end
73
84
  output.strip!
74
85
  output.split(NEW_LINE).map(&:rstrip).join(NEW_LINE)
75
86
  end
@@ -83,7 +83,7 @@ module Dry
83
83
  when nil then templates[:default]
84
84
  else
85
85
  raise ArgumentError,
86
- ":template option must be a Symbol or a String (`#{template_spec}` given)"
86
+ ":template option must be a Symbol or a String (`#{template_spec}` given)"
87
87
  end
88
88
 
89
89
  formatter_options = {**options, template: template}
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Logger
5
- VERSION = "1.2.1"
5
+ VERSION = "1.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hanakai team
8
- autorequire:
9
- bindir: bin
8
+ bindir: exe
10
9
  cert_chain: []
11
- date: 2025-12-15 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: logger
@@ -72,9 +71,9 @@ email:
72
71
  executables: []
73
72
  extensions: []
74
73
  extra_rdoc_files:
75
- - README.md
76
74
  - CHANGELOG.md
77
75
  - LICENSE
76
+ - README.md
78
77
  files:
79
78
  - CHANGELOG.md
80
79
  - LICENSE
@@ -104,12 +103,10 @@ homepage: https://dry-rb.org/gems/dry-logger
104
103
  licenses:
105
104
  - MIT
106
105
  metadata:
107
- allowed_push_host: https://rubygems.org
108
106
  changelog_uri: https://github.com/dry-rb/dry-logger/blob/main/CHANGELOG.md
109
107
  source_code_uri: https://github.com/dry-rb/dry-logger
110
108
  bug_tracker_uri: https://github.com/dry-rb/dry-logger/issues
111
109
  funding_uri: https://github.com/sponsors/hanami
112
- post_install_message:
113
110
  rdoc_options: []
114
111
  require_paths:
115
112
  - lib
@@ -117,15 +114,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
114
  requirements:
118
115
  - - ">="
119
116
  - !ruby/object:Gem::Version
120
- version: '3.2'
117
+ version: '3.3'
121
118
  required_rubygems_version: !ruby/object:Gem::Requirement
122
119
  requirements:
123
120
  - - ">="
124
121
  - !ruby/object:Gem::Version
125
122
  version: '0'
126
123
  requirements: []
127
- rubygems_version: 3.3.27
128
- signing_key:
124
+ rubygems_version: 3.6.9
129
125
  specification_version: 4
130
126
  summary: Lightweight structured logging for Ruby applications
131
127
  test_files: []