Skip to content

Commit be7f55e

Browse files
committed
remove log level from log structs, use conventional logging interface - .info, .warning, etc.
1 parent 20366cf commit be7f55e

File tree

16 files changed

+214
-55
lines changed

16 files changed

+214
-55
lines changed

lib/log_struct/concerns/error_handling.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def log_error(error, source:, context: nil)
4343
error,
4444
context || {}
4545
)
46-
LogStruct.log(error_log)
46+
LogStruct.error(error_log)
4747
end
4848

4949
# Report an error using the configured handler or MultiErrorReporter

lib/log_struct/concerns/logging.rb

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# typed: strict
22
# frozen_string_literal: true
33

4-
require_relative "../enums/level"
54
require_relative "../log"
65

76
module LogStruct
@@ -11,26 +10,34 @@ module Logging
1110
module ClassMethods
1211
extend T::Sig
1312

14-
# Type-safe interface for Rails.logger
13+
# Log a log struct at debug level
1514
sig { params(log: Log::Interfaces::CommonFields).void }
16-
def log(log)
17-
level = log.level
18-
case level
19-
when Level::Debug
20-
Rails.logger.debug(log)
21-
when Level::Info
22-
Rails.logger.info(log)
23-
when Level::Warn
24-
Rails.logger.warn(log)
25-
when Level::Error
26-
Rails.logger.error(log)
27-
when Level::Fatal
28-
Rails.logger.fatal(log)
29-
when Level::Unknown
30-
Rails.logger.error(log) # Log unknown severity as error
31-
else
32-
T.absurd(level)
33-
end
15+
def debug(log)
16+
Rails.logger.debug(log)
17+
end
18+
19+
# Log a log struct at info level
20+
sig { params(log: Log::Interfaces::CommonFields).void }
21+
def info(log)
22+
Rails.logger.info(log)
23+
end
24+
25+
# Log a log struct at warn level
26+
sig { params(log: Log::Interfaces::CommonFields).void }
27+
def warn(log)
28+
Rails.logger.warn(log)
29+
end
30+
31+
# Log a log struct at error level
32+
sig { params(log: Log::Interfaces::CommonFields).void }
33+
def error(log)
34+
Rails.logger.error(log)
35+
end
36+
37+
# Log a log struct at fatal level
38+
sig { params(log: Log::Interfaces::CommonFields).void }
39+
def fatal(log)
40+
Rails.logger.fatal(log)
3441
end
3542
end
3643
end

lib/log_struct/formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def call(severity, time, progname, log_value)
193193
data[:src] ||= Source::App
194194
data[:evt] ||= Event::Log
195195
data[:ts] ||= time.iso8601(3)
196-
data[:lvl] ||= level_enum # Just a fallback, Log structs store their own level field
196+
data[:lvl] = level_enum # Set level from severity parameter
197197
data[:prog] = progname if progname.present?
198198

199199
generate_json(data)

lib/log_struct/integrations/action_mailer/error_handling.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def log_structured_error(mailer, error, message)
5858
)
5959

6060
# Log the structured error
61-
LogStruct.log(exception_data)
61+
LogStruct.error(exception_data)
6262
end
6363

6464
# Log when email delivery fails
@@ -106,7 +106,7 @@ def handle_error_notifications(error, notify, report, reraise)
106106
)
107107

108108
# Log the exception with structured data
109-
LogStruct.log(exception_data)
109+
LogStruct.error(exception_data)
110110

111111
# Call the error handler
112112
LogStruct.handle_exception(error, source: Source::Mailer, context: context)
@@ -131,7 +131,7 @@ def log_notification_event(error)
131131
)
132132

133133
# Log the error at info level since it's not a critical error
134-
LogStruct.log(exception_data)
134+
LogStruct.info(exception_data)
135135
end
136136

137137
sig { params(error: StandardError).returns(String) }

lib/log_struct/integrations/action_mailer/event_logging.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ def log_mailer_event(event_type, level = :info, additional_data = {})
6565
# Create a structured log entry
6666
log_data = Log::ActionMailer.new(
6767
event: event_type,
68-
level: Level::Info,
6968
to: to,
7069
from: from,
7170
subject: subject,
7271
additional_data: data
7372
)
74-
LogStruct.log(log_data)
73+
LogStruct.info(log_data)
7574
log_data
7675
end
7776

lib/log_struct/integrations/active_storage.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def self.process_active_storage_event(event, config)
8585
)
8686

8787
# Log structured data
88-
LogStruct.log(log_data)
88+
LogStruct.info(log_data)
8989
end
9090
end
9191
end

lib/log_struct/integrations/rack_error_handler/middleware.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def call(env)
9797
referer: request.referer,
9898
request_id: request.request_id
9999
)
100-
LogStruct.log(security_log)
100+
LogStruct.error(security_log)
101101

102102
# Report to error reporting service and/or re-raise
103103
context = extract_request_context(env)
@@ -116,7 +116,7 @@ def call(env)
116116
error,
117117
context
118118
)
119-
LogStruct.log(exception_log)
119+
LogStruct.error(exception_log)
120120

121121
# Re-raise any standard errors to let Rails or error reporter handle it.
122122
# Rails will also log the request details separately

lib/log_struct/log/interfaces/common_fields.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
require_relative "../../enums/source"
55
require_relative "../../enums/event"
6-
require_relative "../../enums/level"
76

87
module LogStruct
98
module Log
@@ -27,10 +26,6 @@ def event; end
2726
sig { abstract.returns(Time) }
2827
def timestamp; end
2928

30-
# The log level of the log entry (JSON property: lvl)
31-
sig { abstract.returns(Level) }
32-
def level; end
33-
3429
# All logs must define a custom serialize method
3530
# If the class is a T::Struct that responds to serialize then we can be sure
3631
# we're getting symbols as keys and don't need to call #serialize.deep_symbolize_keys

lib/log_struct/log/plain.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class Plain < T::Struct
2929
const :source, Source, default: T.let(Source::App, Source)
3030
const :event, PlainEvent, default: T.let(Event::Log, PlainEvent)
3131
const :timestamp, Time, factory: -> { Time.now }
32-
const :level, Level, default: T.let(Level::Info, Level)
3332

3433
# Plain log messages can be any type (String, Number, Array, Hash, etc.)
3534
# Developers might do something like Rails.logger.info(123) or Rails.logger.info(@variable)

lib/log_struct/log/shared/serialize_common.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ def serialize_common(strict = true)
2020
{
2121
LOG_KEYS.fetch(:source) => source.serialize,
2222
LOG_KEYS.fetch(:event) => event.serialize,
23-
LOG_KEYS.fetch(:timestamp) => timestamp.iso8601(3),
24-
LOG_KEYS.fetch(:level) => level.serialize
23+
LOG_KEYS.fetch(:timestamp) => timestamp.iso8601(3)
2524
}
2625
end
2726
end

0 commit comments

Comments
 (0)