How to customize Rails log messages to JSON format [closed] How to customize Rails log messages to JSON format [closed] json json

How to customize Rails log messages to JSON format [closed]


You can configure rails to specify your own log formatter:

config.log_formatter defines the formatter of the Rails logger. This option defaults to an instance of ActiveSupport::Logger::SimpleFormatter for all modes except production, where it defaults to Logger::Formatter.

You can provide your own class to output the log information:

class MySimpleFormatter < ActiveSupport::Logger::SimpleFormatter  def call(severity, timestamp, _progname, message)    {       type: severity,      time: timestamp,      message: message    }.to_json  endend

To configure your new class you'd need to add a config line:

config.log_formatter = MySimpleFormatter.new