Sha256: d56c1303def23f746341d5bbfa34bf6a0b50582f1fcc2250d083722440af2728

Contents?: true

Size: 708 Bytes

Versions: 1

Compression:

Stored size: 708 Bytes

Contents

# Custom Json Logger Formatter
class JsonFormatter < Logger::Formatter
  include ActiveSupport::TaggedLogging::Formatter

  def call(severity, time, _progname, msg)
    return if msg.blank?
    log_line = { '@timestamp': time.utc.iso8601(6),
                 level: severity.to_s }.merge(format(msg)).compact
    %(#{log_line.to_json}\n)
  end

  def format(msg)
    case msg
    when ::String
      { message: msg }
    when ::Hash
      msg
    when ::Exception
      format_exception(msg)
    else
      { message: msg.inspect }
    end
  end

  def format_exception(exception)
    {
      exception: exception.class,
      message: exception.message,
      backtrace: exception.backtrace
    }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_stdout_json_logging-0.1.5 lib/rails_stdout_json_logging/json_formatter.rb