Module: ActiveSupport::TaggedLogging::FormatterExtension

Extended by:
T::Helpers, T::Sig
Defined in:
lib/log_struct/monkey_patches/active_support/tagged_logging/formatter.rb

Instance Method Summary collapse

Instance Method Details

#call(severity, time, progname, data) ⇒ String

Parameters:

  • severity (String, Symbol)
  • time (Time)
  • progname (T.untyped)
  • data (T.untyped)

Returns:

  • (String)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/log_struct/monkey_patches/active_support/tagged_logging/formatter.rb', line 30

def call(severity, time, progname, data)
  # Preserve original Rails behavior when LogStruct is disabled
  return super unless ::LogStruct.enabled?

  # Get current tags
  tags = T.unsafe(self).respond_to?(:current_tags) ? current_tags : (Thread.current[:activesupport_tagged_logging_tags] || [])

  # Add tags to data as hash entry (not text prefix)
  data_with_tags = if data.is_a?(Hash)
    tags.present? ? data.merge(tags: tags) : data
  elsif data.is_a?(::LogStruct::Log::Interfaces::CommonFields) || (data.is_a?(T::Struct) && data.respond_to?(:serialize))
    hash = T.unsafe(data).serialize
    tags.present? ? hash.merge(tags: tags) : hash
  else
    base = {LogStruct::LogField::Message.serialize => data.to_s}
    tags.present? ? base.merge(tags: tags) : base
  end

  # Delegate to LogStruct::Formatter for JSON serialization with filtering
  logstruct_formatter.call(severity, time, progname, data_with_tags)
end