reidmorrison / rails_semantic_logger

Rails Semantic Logger replaces the Rails default logger with Semantic Logger
https://logger.rocketjob.io/rails
Apache License 2.0
338 stars 124 forks source link

Could not log "process.action_mailer" event. JSON::GeneratorError: source sequence is illegal/malformed utf-8 #231

Open srajan-dadhich opened 4 months ago

srajan-dadhich commented 4 months ago

Environment

Provide at least:

Could not log "process.action_mailer" event. JSON::GeneratorError: source sequence is illegal/malformed utf-8 ["/usr/local/bundle/ruby/3.1.0/gems/json-2.6.3/lib/json/common.rb:406:ingenerate'", "/usr/local/bundle/ruby/3.1.0/gems/json-2.6.3/lib/json/common.rb:406:in pretty_generate'", "/usr/local/bundle/ruby/3.1.0/gems/rails_semantic_logger-4.14.0/lib/rails_semantic_logger/action_mailer/log_subscriber.rb:96:informatted_args'", "/usr/local/bundle/ruby/3.1.0/gems/rails_semantic_logger-4.14.0/lib/rails_semantic_logger/action_mailer/log_subscriber.rb:68:in block in payload'", "<internal:kernel>:90:intap'", "/usr/local/bundle/ruby/3.1.0/gems/rails_semantic_logger-4.14.0/lib/rails_semantic_logger/action_mailer/log_subscriber.rb:55:in payload'", "/usr/local/bundle/ruby/3.1.0/gems/rails_semantic_logger-4.14.0/lib/rails_semantic_logger/action_mailer/log_subscriber.rb:125:inlog_with_formatter'", "/usr/local/bundle/ruby/3.1.0/gems/rails_semantic_logger-4.14.0/lib/rails_semantic_logger/action_mailer/log_subscriber.rb:37:in process'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/subscriber.rb:149:infinish'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/log_subscriber.rb:115:in finish'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/fanout.rb:211:infinish'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/fanout.rb:76:in block in finish'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/fanout.rb:91:inblock in iterate_guarding_exceptions'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/fanout.rb:90:in each'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/fanout.rb:90:initerate_guarding_exceptions'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/fanout.rb:76:in finish'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/instrumenter.rb:49:infinish_with_state'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications/instrumenter.rb:30:in instrument'", "/usr/local/bundle/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/notifications.rb:206:ininstrument'", "/usr/local/bundle/ruby/3.1.0/gems/actionmailer-7.0.4.3/lib/action_mailer/base.rb:645:in process'", "/usr/local/bundle/ruby/3.1.0/gems/actionmailer-7.0.4.3/lib/action_mailer/message_delivery.rb:128:inblock in processed_mailer'"....`

Note:

Expected Behavior

The format method should handle illegal/malformed UTF-8 input for String argument. For Rails application logs we have a patch to handle a similar behaviour, `module SemanticLoggerExtensions class CustomLogFormatter < SemanticLogger::Formatters::Json def message if log.message encoded_message = encode_message(log.message) log.message = encoded_message if encoded_message hash[:message] = log.cleansed_message end end

  private

  def encode_message(message)
    return message unless message && (message.encoding != Encoding.default_internal)
    message.encode(Encoding.default_internal, invalid: :replace, undef: :replace)
  end
end

end`

Actual Behavior

Pull Request

reidmorrison commented 2 months ago

Would be worthwhile to submit a pull request with this enhancement, along with a config option to disable the behavior? Enabling it by default sounds like a good option, allowing people to disable it if they need to.