logstruct 0.1.0 → 0.1.2

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.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -1
  3. data/README.md +23 -3
  4. data/lib/log_struct/boot_buffer.rb +28 -0
  5. data/lib/log_struct/builders/active_job.rb +84 -0
  6. data/lib/log_struct/concerns/configuration.rb +178 -15
  7. data/lib/log_struct/concerns/error_handling.rb +3 -7
  8. data/lib/log_struct/config_struct/filters.rb +18 -0
  9. data/lib/log_struct/config_struct/integrations.rb +8 -12
  10. data/lib/log_struct/configuration.rb +13 -0
  11. data/lib/log_struct/enums/event.rb +13 -0
  12. data/lib/log_struct/enums/log_field.rb +154 -0
  13. data/lib/log_struct/enums/source.rb +4 -1
  14. data/lib/log_struct/formatter.rb +29 -17
  15. data/lib/log_struct/integrations/action_mailer/error_handling.rb +3 -11
  16. data/lib/log_struct/integrations/action_mailer/event_logging.rb +22 -12
  17. data/lib/log_struct/integrations/active_job/log_subscriber.rb +52 -48
  18. data/lib/log_struct/integrations/active_model_serializers.rb +8 -14
  19. data/lib/log_struct/integrations/active_record.rb +35 -5
  20. data/lib/log_struct/integrations/active_storage.rb +59 -20
  21. data/lib/log_struct/integrations/ahoy.rb +2 -1
  22. data/lib/log_struct/integrations/carrierwave.rb +13 -16
  23. data/lib/log_struct/integrations/dotenv.rb +278 -0
  24. data/lib/log_struct/integrations/good_job/log_subscriber.rb +86 -136
  25. data/lib/log_struct/integrations/good_job/logger.rb +8 -10
  26. data/lib/log_struct/integrations/good_job.rb +5 -7
  27. data/lib/log_struct/integrations/host_authorization.rb +25 -4
  28. data/lib/log_struct/integrations/lograge.rb +20 -14
  29. data/lib/log_struct/integrations/puma.rb +477 -0
  30. data/lib/log_struct/integrations/rack_error_handler/middleware.rb +11 -18
  31. data/lib/log_struct/integrations/shrine.rb +44 -19
  32. data/lib/log_struct/integrations/sorbet.rb +48 -0
  33. data/lib/log_struct/integrations.rb +21 -0
  34. data/lib/log_struct/log/action_mailer/delivered.rb +99 -0
  35. data/lib/log_struct/log/action_mailer/delivery.rb +99 -0
  36. data/lib/log_struct/log/action_mailer.rb +30 -45
  37. data/lib/log_struct/log/active_job/enqueue.rb +125 -0
  38. data/lib/log_struct/log/active_job/finish.rb +130 -0
  39. data/lib/log_struct/log/active_job/schedule.rb +125 -0
  40. data/lib/log_struct/log/active_job/start.rb +130 -0
  41. data/lib/log_struct/log/active_job.rb +41 -54
  42. data/lib/log_struct/log/active_model_serializers.rb +72 -33
  43. data/lib/log_struct/log/active_storage/delete.rb +87 -0
  44. data/lib/log_struct/log/active_storage/download.rb +103 -0
  45. data/lib/log_struct/log/active_storage/exist.rb +93 -0
  46. data/lib/log_struct/log/active_storage/metadata.rb +93 -0
  47. data/lib/log_struct/log/active_storage/stream.rb +93 -0
  48. data/lib/log_struct/log/active_storage/upload.rb +118 -0
  49. data/lib/log_struct/log/active_storage/url.rb +93 -0
  50. data/lib/log_struct/log/active_storage.rb +32 -68
  51. data/lib/log_struct/log/ahoy.rb +67 -33
  52. data/lib/log_struct/log/carrierwave/delete.rb +115 -0
  53. data/lib/log_struct/log/carrierwave/download.rb +131 -0
  54. data/lib/log_struct/log/carrierwave/upload.rb +141 -0
  55. data/lib/log_struct/log/carrierwave.rb +37 -72
  56. data/lib/log_struct/log/dotenv/load.rb +76 -0
  57. data/lib/log_struct/log/dotenv/restore.rb +76 -0
  58. data/lib/log_struct/log/dotenv/save.rb +76 -0
  59. data/lib/log_struct/log/dotenv/update.rb +76 -0
  60. data/lib/log_struct/log/dotenv.rb +12 -0
  61. data/lib/log_struct/log/error.rb +58 -47
  62. data/lib/log_struct/log/good_job/enqueue.rb +126 -0
  63. data/lib/log_struct/log/good_job/error.rb +151 -0
  64. data/lib/log_struct/log/good_job/finish.rb +136 -0
  65. data/lib/log_struct/log/good_job/log.rb +131 -0
  66. data/lib/log_struct/log/good_job/schedule.rb +136 -0
  67. data/lib/log_struct/log/good_job/start.rb +136 -0
  68. data/lib/log_struct/log/good_job.rb +40 -141
  69. data/lib/log_struct/log/interfaces/additional_data_field.rb +1 -17
  70. data/lib/log_struct/log/interfaces/common_fields.rb +1 -39
  71. data/lib/log_struct/log/interfaces/public_common_fields.rb +1 -28
  72. data/lib/log_struct/log/interfaces/request_fields.rb +1 -33
  73. data/lib/log_struct/log/plain.rb +59 -34
  74. data/lib/log_struct/log/puma/shutdown.rb +80 -0
  75. data/lib/log_struct/log/puma/start.rb +120 -0
  76. data/lib/log_struct/log/puma.rb +10 -0
  77. data/lib/log_struct/log/request.rb +132 -48
  78. data/lib/log_struct/log/security/blocked_host.rb +141 -0
  79. data/lib/log_struct/log/security/csrf_violation.rb +131 -0
  80. data/lib/log_struct/log/security/ip_spoof.rb +141 -0
  81. data/lib/log_struct/log/security.rb +40 -70
  82. data/lib/log_struct/log/shared/add_request_fields.rb +1 -26
  83. data/lib/log_struct/log/shared/merge_additional_data_fields.rb +1 -22
  84. data/lib/log_struct/log/shared/serialize_common.rb +1 -33
  85. data/lib/log_struct/log/shared/serialize_common_public.rb +9 -9
  86. data/lib/log_struct/log/shrine/delete.rb +85 -0
  87. data/lib/log_struct/log/shrine/download.rb +90 -0
  88. data/lib/log_struct/log/shrine/exist.rb +90 -0
  89. data/lib/log_struct/log/shrine/metadata.rb +90 -0
  90. data/lib/log_struct/log/shrine/upload.rb +105 -0
  91. data/lib/log_struct/log/shrine.rb +10 -67
  92. data/lib/log_struct/log/sidekiq.rb +65 -26
  93. data/lib/log_struct/log/sql.rb +113 -106
  94. data/lib/log_struct/log.rb +29 -36
  95. data/lib/log_struct/multi_error_reporter.rb +80 -22
  96. data/lib/log_struct/param_filters.rb +50 -7
  97. data/lib/log_struct/rails_boot_banner_silencer.rb +116 -0
  98. data/lib/log_struct/railtie.rb +67 -0
  99. data/lib/log_struct/semantic_logger/formatter.rb +4 -2
  100. data/lib/log_struct/semantic_logger/setup.rb +34 -18
  101. data/lib/log_struct/shared/interfaces/additional_data_field.rb +22 -0
  102. data/lib/log_struct/shared/interfaces/common_fields.rb +39 -0
  103. data/lib/log_struct/shared/interfaces/public_common_fields.rb +29 -0
  104. data/lib/log_struct/shared/interfaces/request_fields.rb +39 -0
  105. data/lib/log_struct/shared/shared/add_request_fields.rb +28 -0
  106. data/lib/log_struct/shared/shared/merge_additional_data_fields.rb +27 -0
  107. data/lib/log_struct/shared/shared/serialize_common.rb +58 -0
  108. data/lib/log_struct/version.rb +1 -1
  109. data/lib/log_struct.rb +36 -4
  110. data/logstruct.gemspec +2 -1
  111. metadata +78 -9
  112. data/lib/log_struct/log/interfaces/message_field.rb +0 -20
  113. data/lib/log_struct/log_keys.rb +0 -102
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a68693a793bd472926424e34f602e5f81a677e0bac2b0b8da189e7bc5adb10a
4
- data.tar.gz: 8d5dd33db82a1b4eda1b8f1b0631af0cb3b04eba1e9d034e275192a49d8b9285
3
+ metadata.gz: a65754f5cc8e4753585ca448eec8e78b5f8586aba5161d94d5c5ccce20386909
4
+ data.tar.gz: 9088ba81fe6319a9dfd1740949c87c7a793bd1535ae97a4d44bc98b0ca3b60b9
5
5
  SHA512:
6
- metadata.gz: 2c8afb5d952a2ec1ead718b5655cd6af260978cd001502cd2dde3f8fb66e15720a5fca9f1646b8e132cad03ab48c51507b6a5d9948080019ca17ec4011b3b347
7
- data.tar.gz: af94c2f9805ec7f121df828e25b1214395450acec0ed475a5b98ca26bef564c537c2bb522f3494152fb2df5d9b0b4850d7c158a3df794fad635a60613796fd51
6
+ metadata.gz: 1bccedd39623795cef4059ec14029c6ed4dd9edba94b355c9147c78052e163d41f6d1d46988bf367bc1c726e0cabaab8f30c8cc36a5470f3b7d1f0c5463b56d6
7
+ data.tar.gz: e0a3d5b784338c0d647ff03e12a92852e4c0de3fb2f93febba9df0bd3871f0b41ef262c1915db368a0b9db71afd0d005db05a325d2314f276dbd23e7b291f654
data/CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.1.2] - 2025-10-03
9
+
10
+ Better default policy for when JSON logs are enabled: machines get JSON, humans get readable logs.
11
+ Enable LogStruct for production servers or when running tests on CI.
12
+ Keep dev-friendly logging on local machines or when running interactive commands on production servers.
13
+
14
+ ## [0.1.1] - 2025-09-29
15
+
16
+ Added dotenv-rails integration. Many other fixes and improvements.
17
+
8
18
  ## [0.1.0] - 2025-09-07
9
19
 
10
- Initial release.
20
+ Initial beta release.
data/README.md CHANGED
@@ -6,7 +6,7 @@ We support all your other favorite gems too, like Sidekiq, Sentry, and Shrine. (
6
6
 
7
7
  ## Features
8
8
 
9
- - JSON logging enabled by default in production and test environments
9
+ - JSON logging enabled by default for server processes in production and test environments, and for CI test runs (automatically disabled for console, local tests, and other Rake tasks)
10
10
  - ActionMailer integration for email delivery logging
11
11
  - ActiveJob integration for job execution logging
12
12
  - Sidekiq integration for background job logging
@@ -48,9 +48,9 @@ Please see the [documentation](https://logstruct.com/docs/configuration/) for co
48
48
 
49
49
  ### Important Notes on Integration
50
50
 
51
- Once initialized, the gem automatically includes its modules into the appropriate base classes:
51
+ Once initialized (and enabled), the gem automatically includes its modules into the appropriate base classes:
52
52
 
53
- - `ActiveSupport::TaggedLogging` is patched to support both Hashes and Strings
53
+ - `ActiveSupport::TaggedLogging` is patched to support both Hashes and Strings (only when LogStruct is enabled)
54
54
  - `ActionMailer::Base` includes error handling and event logging modules
55
55
  - We configure `Lograge` for request logging
56
56
  - A Rack middleware is inserted to catch and log errors, including security violations (IP spoofing, CSRF, blocked hosts, etc.)
@@ -58,6 +58,26 @@ Once initialized, the gem automatically includes its modules into the appropriat
58
58
  - Rails `config.filter_parameters` are merged into LogStruct's filters and then cleared (to avoid double filtering). Configure sensitive keys via `LogStruct.config.filters`.
59
59
  - When `RAILS_LOG_TO_STDOUT` is set, we log to STDOUT only. Otherwise, we log to STDOUT by default without adding a file appender to avoid duplicate logs.
60
60
 
61
+ ### Default behavior by process type
62
+
63
+ - **Server processes** (`rails server`): JSON logging is enabled by default in production and test environments
64
+ - **CI test runs** (`rails test` when `CI=true`): JSON logging is enabled by default to catch production bugs in your automated tests
65
+ - **Local test runs** (`rails test` locally): JSON logging is disabled by default, providing human-readable logs for debugging
66
+ - **Console** (`rails console`): JSON logging is disabled by default in all environments, providing human-readable logs instead
67
+ - **Other Rake tasks** (`rake db:migrate`, etc.): JSON logging is disabled by default in production, providing human-readable logs instead
68
+ - **Development environment**: Disabled by default for all process types. Enable explicitly via `LOGSTRUCT_ENABLED=true` or `LogStruct.configure { |c| c.enabled = true }`.
69
+
70
+ When enabled in development, LogStruct defaults to production‑style JSON output so you can preview exactly what will be shipped in prod. You can opt back into the colorful human formatter with:
71
+
72
+ ```ruby
73
+ LogStruct.configure do |c|
74
+ c.prefer_json_in_development = false
75
+ c.enable_color_output = true
76
+ end
77
+ ```
78
+
79
+ To force JSON logs in console, local test runs, or other Rake tasks (e.g., for debugging), set `LOGSTRUCT_ENABLED=true` in your environment.
80
+
61
81
  ## Documentation
62
82
 
63
83
  Please see the [documentation](https://logstruct.com/docs) for more details. (All code examples are type-checked and tested, and it's harder to keep a README up to date.)
@@ -0,0 +1,28 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module LogStruct
5
+ # Collects structured logs during very early boot before the logger is ready.
6
+ module BootBuffer
7
+ extend T::Sig
8
+
9
+ @@logs = T.let([], T::Array[LogStruct::Log::Interfaces::CommonFields])
10
+
11
+ sig { params(log: LogStruct::Log::Interfaces::CommonFields).void }
12
+ def self.add(log)
13
+ @@logs << log
14
+ end
15
+
16
+ sig { void }
17
+ def self.flush
18
+ return if @@logs.empty?
19
+ @@logs.each { |l| LogStruct.info(l) }
20
+ @@logs.clear
21
+ end
22
+
23
+ sig { void }
24
+ def self.clear
25
+ @@logs.clear
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,84 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../log/active_job/enqueue"
5
+ require_relative "../log/active_job/schedule"
6
+ require_relative "../log/active_job/start"
7
+ require_relative "../log/active_job/finish"
8
+
9
+ module LogStruct
10
+ module Builders
11
+ module ActiveJob
12
+ extend T::Sig
13
+
14
+ sig { params(job: T.untyped).returns(T.nilable(String)) }
15
+ def self.safe_provider_job_id(job)
16
+ job.respond_to?(:provider_job_id) ? job.provider_job_id : nil
17
+ end
18
+
19
+ sig { params(job: T.untyped).returns(T.nilable(Integer)) }
20
+ def self.safe_executions(job)
21
+ job.respond_to?(:executions) ? job.executions : nil
22
+ end
23
+
24
+ sig { params(job: T.untyped).returns(T.nilable(T::Array[T.untyped])) }
25
+ def self.safe_arguments(job)
26
+ return nil unless job.class.respond_to?(:log_arguments?)
27
+ job.class.log_arguments? ? job.arguments : nil
28
+ end
29
+
30
+ sig { params(job: T.untyped).returns(Log::ActiveJob::Enqueue) }
31
+ def self.enqueue(job)
32
+ Log::ActiveJob::Enqueue.new(
33
+ job_id: job.job_id,
34
+ job_class: job.class.to_s,
35
+ queue_name: job.queue_name,
36
+ arguments: safe_arguments(job),
37
+ executions: safe_executions(job),
38
+ provider_job_id: safe_provider_job_id(job)
39
+ )
40
+ end
41
+
42
+ sig { params(job: T.untyped, scheduled_at: Time).returns(Log::ActiveJob::Schedule) }
43
+ def self.schedule(job, scheduled_at:)
44
+ Log::ActiveJob::Schedule.new(
45
+ job_id: job.job_id,
46
+ job_class: job.class.to_s,
47
+ queue_name: job.queue_name,
48
+ arguments: safe_arguments(job),
49
+ executions: safe_executions(job),
50
+ provider_job_id: safe_provider_job_id(job),
51
+ scheduled_at: scheduled_at
52
+ )
53
+ end
54
+
55
+ sig { params(job: T.untyped, started_at: Time, attempt: T.nilable(Integer)).returns(Log::ActiveJob::Start) }
56
+ def self.start(job, started_at:, attempt:)
57
+ Log::ActiveJob::Start.new(
58
+ job_id: job.job_id,
59
+ job_class: job.class.to_s,
60
+ queue_name: job.queue_name,
61
+ arguments: safe_arguments(job),
62
+ executions: safe_executions(job),
63
+ provider_job_id: safe_provider_job_id(job),
64
+ started_at: started_at,
65
+ attempt: attempt
66
+ )
67
+ end
68
+
69
+ sig { params(job: T.untyped, duration_ms: Float, finished_at: Time).returns(Log::ActiveJob::Finish) }
70
+ def self.finish(job, duration_ms:, finished_at:)
71
+ Log::ActiveJob::Finish.new(
72
+ job_id: job.job_id,
73
+ job_class: job.class.to_s,
74
+ queue_name: job.queue_name,
75
+ arguments: safe_arguments(job),
76
+ executions: safe_executions(job),
77
+ provider_job_id: safe_provider_job_id(job),
78
+ duration_ms: duration_ms,
79
+ finished_at: finished_at
80
+ )
81
+ end
82
+ end
83
+ end
84
+ end
@@ -10,6 +10,11 @@ module LogStruct
10
10
  module ClassMethods
11
11
  extend T::Sig
12
12
 
13
+ SERVER_COMMAND_ARGS = T.let(["server", "s"].freeze, T::Array[String])
14
+ CONSOLE_COMMAND_ARGS = T.let(["console", "c"].freeze, T::Array[String])
15
+ EMPTY_ARGV = T.let([].freeze, T::Array[String])
16
+ CI_FALSE_VALUES = T.let(["false", "0", "no"].freeze, T::Array[String])
17
+
13
18
  sig { params(block: T.proc.params(config: LogStruct::Configuration).void).void }
14
19
  def configure(&block)
15
20
  yield(config)
@@ -41,18 +46,24 @@ module LogStruct
41
46
  def set_enabled_from_rails_env!
42
47
  # Set enabled based on current Rails environment and the LOGSTRUCT_ENABLED env var.
43
48
  # Precedence:
44
- # 1. Check if LOGSTRUCT_ENABLED env var is defined
45
- # - Sets enabled=true only when value is "true"
46
- # - Sets enabled=false when value is "false" (or any non-"true")
49
+ # 1. Check if LOGSTRUCT_ENABLED env var is defined (not an empty string)
50
+ # - Sets enabled=true only when value is "true", "yes", "1", etc.
51
+ # - Sets enabled=false when value is any other value
47
52
  # 2. Otherwise, check if current Rails environment is in enabled_environments
53
+ # AND one of: Rails::Server is defined, OR test environment with CI=true
54
+ # BUT NOT Rails::Console (to exclude interactive console)
48
55
  # 3. Otherwise, leave as config.enabled (defaults to true)
49
56
 
50
57
  # Then check if LOGSTRUCT_ENABLED env var is set
51
58
  config.enabled = if ENV["LOGSTRUCT_ENABLED"]
52
- # Override to true only if env var is "true"
53
- ENV["LOGSTRUCT_ENABLED"] == "true"
59
+ %w[true t yes y 1].include?(ENV["LOGSTRUCT_ENABLED"]&.strip&.downcase)
54
60
  else
55
- config.enabled_environments.include?(::Rails.env.to_sym)
61
+ is_console = console_process?
62
+ is_server = server_process?
63
+ is_ci = ci_build?
64
+ in_enabled_env = config.enabled_environments.include?(::Rails.env.to_sym)
65
+
66
+ in_enabled_env && !is_console && (is_server || (::Rails.env.test? && is_ci))
56
67
  end
57
68
  end
58
69
 
@@ -72,20 +83,172 @@ module LogStruct
72
83
 
73
84
  rails_filter_params = ::Rails.application.config.filter_parameters
74
85
  return unless rails_filter_params.is_a?(Array)
86
+ return if rails_filter_params.empty?
87
+
88
+ symbol_filters = T.let([], T::Array[Symbol])
89
+ matchers = T.let([], T::Array[ConfigStruct::FilterMatcher])
90
+ leftovers = T.let([], T::Array[T.untyped])
91
+
92
+ rails_filter_params.each do |entry|
93
+ matcher = build_filter_matcher(entry)
94
+
95
+ if matcher
96
+ matchers << matcher
97
+ next
98
+ end
99
+
100
+ normalized_symbol = normalize_filter_symbol(entry)
101
+ if normalized_symbol
102
+ symbol_filters << normalized_symbol
103
+ else
104
+ leftovers << entry
105
+ end
106
+ end
75
107
 
76
- # Convert all Rails filter parameters to symbols and merge with our filter keys
77
- converted_params = rails_filter_params.map do |param|
78
- param.respond_to?(:to_sym) ? param.to_sym : param
108
+ if symbol_filters.any?
109
+ config.filters.filter_keys |= symbol_filters
79
110
  end
80
111
 
81
- # Add Rails filter parameters to our filter keys
82
- config.filters.filter_keys += converted_params
112
+ if matchers.any?
113
+ matchers.each do |matcher|
114
+ existing = config.filters.filter_matchers.any? do |registered|
115
+ registered.label == matcher.label
116
+ end
117
+ config.filters.filter_matchers << matcher unless existing
118
+ end
119
+ end
120
+
121
+ replace_filter_parameters(rails_filter_params, leftovers)
122
+ end
123
+
124
+ private
125
+
126
+ sig { returns(T::Boolean) }
127
+ def console_process?
128
+ return true if defined?(::Rails::Console)
129
+
130
+ current_argv.any? { |arg| CONSOLE_COMMAND_ARGS.include?(arg) }
131
+ end
132
+
133
+ sig { returns(T::Boolean) }
134
+ def server_process?
135
+ return true if logstruct_server_mode?
136
+
137
+ current_argv.any? { |arg| SERVER_COMMAND_ARGS.include?(arg) }
138
+ end
139
+
140
+ sig { returns(T::Boolean) }
141
+ def logstruct_server_mode?
142
+ ::LogStruct.server_mode?
143
+ end
144
+
145
+ sig { returns(T::Array[String]) }
146
+ def current_argv
147
+ raw = ::ARGV
148
+ strings = raw.map { |arg| arg.to_s }
149
+ T.let(strings, T::Array[String])
150
+ rescue NameError
151
+ EMPTY_ARGV
152
+ end
153
+
154
+ sig { returns(T::Boolean) }
155
+ def ci_build?
156
+ value = ENV["CI"]
157
+ return false if value.nil?
158
+
159
+ normalized = value.strip.downcase
160
+ return false if normalized.empty?
161
+
162
+ !CI_FALSE_VALUES.include?(normalized)
163
+ end
164
+
165
+ sig { params(filter: T.untyped).returns(T.nilable(Symbol)) }
166
+ def normalize_filter_symbol(filter)
167
+ return filter if filter.is_a?(Symbol)
168
+ return filter.downcase.to_sym if filter.is_a?(String)
169
+
170
+ return nil unless filter.respond_to?(:to_sym)
171
+
172
+ begin
173
+ sym = filter.to_sym
174
+ sym.is_a?(Symbol) ? sym : nil
175
+ rescue
176
+ nil
177
+ end
178
+ end
179
+
180
+ sig { params(filter: T.untyped).returns(T.nilable(ConfigStruct::FilterMatcher)) }
181
+ def build_filter_matcher(filter)
182
+ case filter
183
+ when ::Regexp
184
+ callable = Kernel.lambda do |key, _value|
185
+ filter.match?(key)
186
+ end
187
+ return ConfigStruct::FilterMatcher.new(callable: callable, label: filter.inspect)
188
+ else
189
+ return build_callable_filter_matcher(filter) if callable_filter?(filter)
190
+ end
191
+
192
+ nil
193
+ end
194
+
195
+ sig { params(filter: T.untyped).returns(T::Boolean) }
196
+ def callable_filter?(filter)
197
+ filter.respond_to?(:call)
198
+ end
199
+
200
+ sig { params(filter: T.untyped).returns(T.nilable(ConfigStruct::FilterMatcher)) }
201
+ def build_callable_filter_matcher(filter)
202
+ callable = Kernel.lambda do |key, value|
203
+ call_args = case arity_for_filter(filter)
204
+ when 0
205
+ []
206
+ when 1
207
+ [key]
208
+ else
209
+ [key, value]
210
+ end
211
+
212
+ result = filter.call(*call_args)
213
+ !!result
214
+ rescue ArgumentError
215
+ begin
216
+ !!filter.call(key)
217
+ rescue => e
218
+ handle_filter_error(e, filter, key)
219
+ false
220
+ end
221
+ rescue => e
222
+ handle_filter_error(e, filter, key)
223
+ false
224
+ end
225
+ ConfigStruct::FilterMatcher.new(callable: callable, label: filter.inspect)
226
+ end
227
+
228
+ sig { params(filter: T.untyped).returns(Integer) }
229
+ def arity_for_filter(filter)
230
+ filter.respond_to?(:arity) ? filter.arity : 2
231
+ end
232
+
233
+ sig { params(filter_params: T::Array[T.untyped], leftovers: T::Array[T.untyped]).void }
234
+ def replace_filter_parameters(filter_params, leftovers)
235
+ filter_params.clear
236
+ filter_params.concat(leftovers)
237
+ end
83
238
 
84
- # Ensure no duplicates
85
- config.filters.filter_keys.uniq!
239
+ sig { params(error: StandardError, filter: T.untyped, key: String).void }
240
+ def handle_filter_error(error, filter, key)
241
+ context = {
242
+ filter: filter.class.name,
243
+ key: key,
244
+ filter_label: begin
245
+ filter.inspect
246
+ rescue
247
+ "unknown"
248
+ end
249
+ }
86
250
 
87
- # Clear Rails filter parameters since we've incorporated them
88
- ::Rails.application.config.filter_parameters.clear
251
+ LogStruct.handle_exception(error, source: Source::Internal, context: context)
89
252
  end
90
253
  end
91
254
  end
@@ -21,12 +21,12 @@ module LogStruct
21
21
  case source
22
22
  when Source::TypeChecking
23
23
  config.error_handling_modes.type_checking_errors
24
- when Source::LogStruct
24
+ when Source::Internal
25
25
  config.error_handling_modes.logstruct_errors
26
26
  when Source::Security
27
27
  config.error_handling_modes.security_errors
28
28
  when Source::Rails, Source::App, Source::Job, Source::Storage, Source::Mailer,
29
- Source::Shrine, Source::CarrierWave, Source::Sidekiq
29
+ Source::Shrine, Source::CarrierWave, Source::Sidekiq, Source::Dotenv, Source::Puma
30
30
  config.error_handling_modes.standard_errors
31
31
  else
32
32
  # Ensures the case statement is exhaustive
@@ -38,11 +38,7 @@ module LogStruct
38
38
  sig { params(error: StandardError, source: Source, context: T.nilable(T::Hash[Symbol, T.untyped])).void }
39
39
  def log_error(error, source:, context: nil)
40
40
  # Create structured log entry
41
- error_log = Log::Error.from_exception(
42
- source,
43
- error,
44
- context || {}
45
- )
41
+ error_log = Log.from_exception(source, error, context || {})
46
42
  LogStruct.error(error_log)
47
43
  end
48
44
 
@@ -3,6 +3,18 @@
3
3
 
4
4
  module LogStruct
5
5
  module ConfigStruct
6
+ class FilterMatcher < T::Struct
7
+ extend T::Sig
8
+
9
+ const :callable, T.proc.params(key: String, value: T.untyped).returns(T::Boolean)
10
+ const :label, String
11
+
12
+ sig { params(key: String, value: T.untyped).returns(T::Boolean) }
13
+ def matches?(key, value)
14
+ callable.call(key, value)
15
+ end
16
+ end
17
+
6
18
  class Filters < T::Struct
7
19
  include Sorbet::SerializeSymbolKeys
8
20
 
@@ -75,6 +87,12 @@ module LogStruct
75
87
  # Filter MAC addresses
76
88
  # Default: false
77
89
  prop :mac_addresses, T::Boolean, default: false
90
+
91
+ # Additional filter matchers built from Rails filter_parameters entries that aren't simple symbols.
92
+ # Each matcher receives the key (String) and optional value, returning true when the pair should be filtered.
93
+ prop :filter_matchers,
94
+ T::Array[FilterMatcher],
95
+ factory: -> { [] }
78
96
  end
79
97
  end
80
98
  end
@@ -60,18 +60,6 @@ module LogStruct
60
60
  # Default: true
61
61
  prop :enable_semantic_logger, T::Boolean, default: true
62
62
 
63
- # Enable colored JSON output in development
64
- # Default: true
65
- prop :enable_color_output, T::Boolean, default: true
66
-
67
- # Color configuration for JSON output
68
- # Default: nil (uses SemanticLogger defaults)
69
- prop :color_map, T.nilable(T::Hash[Symbol, Symbol]), default: nil
70
-
71
- # Filter noisy loggers (ActionView, etc.)
72
- # Default: false
73
- prop :filter_noisy_loggers, T::Boolean, default: false
74
-
75
63
  # Enable SQL query logging through ActiveRecord instrumentation
76
64
  # Default: false (can be resource intensive)
77
65
  prop :enable_sql_logging, T::Boolean, default: false
@@ -92,6 +80,14 @@ module LogStruct
92
80
  # Enable ActiveModelSerializers integration
93
81
  # Default: true (safe no-op unless ActiveModelSerializers is defined)
94
82
  prop :enable_active_model_serializers, T::Boolean, default: true
83
+
84
+ # Enable dotenv-rails integration (convert to structured logs)
85
+ # Default: true
86
+ prop :enable_dotenv, T::Boolean, default: true
87
+
88
+ # Enable Puma integration (convert server lifecycle logs)
89
+ # Default: true
90
+ prop :enable_puma, T::Boolean, default: true
95
91
  end
96
92
  end
97
93
  end
@@ -20,6 +20,19 @@ module LogStruct
20
20
  prop :enabled, T::Boolean, default: true
21
21
  prop :enabled_environments, T::Array[Symbol], factory: -> { [:test, :production] }
22
22
  prop :local_environments, T::Array[Symbol], factory: -> { [:development, :test] }
23
+
24
+ # Prefer production-style JSON in development when LogStruct is enabled
25
+ prop :prefer_json_in_development, T::Boolean, default: true
26
+
27
+ # Enable colorful human formatter in development
28
+ prop :enable_color_output, T::Boolean, default: true
29
+
30
+ # Custom color map for the color formatter
31
+ prop :color_map, T.nilable(T::Hash[Symbol, Symbol]), default: nil
32
+
33
+ # Filter noisy loggers (ActionView, etc.)
34
+ prop :filter_noisy_loggers, T::Boolean, default: false
35
+
23
36
  const :integrations, ConfigStruct::Integrations, factory: -> { ConfigStruct::Integrations.new }
24
37
  const :filters, ConfigStruct::Filters, factory: -> { ConfigStruct::Filters.new }
25
38
 
@@ -26,10 +26,23 @@ module LogStruct
26
26
  Stream = new(:stream)
27
27
  Url = new(:url)
28
28
 
29
+ # Data generation events
30
+ Generate = new(:generate)
31
+
29
32
  # Email events
30
33
  Delivery = new(:delivery)
31
34
  Delivered = new(:delivered)
32
35
 
36
+ # Configuration / boot events
37
+ Load = new(:load)
38
+ Update = new(:update)
39
+ Save = new(:save)
40
+ Restore = new(:restore)
41
+
42
+ # Server lifecycle (e.g., Puma)
43
+ # Start already defined above
44
+ Shutdown = new(:shutdown)
45
+
33
46
  # Security events
34
47
  IPSpoof = new(:ip_spoof)
35
48
  CSRFViolation = new(:csrf_violation)