Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Created March 10, 2017 08:59
Show Gist options
  • Save elvisgiv/3cb757b8fe33206c665ffce9a30d8692 to your computer and use it in GitHub Desktop.
Save elvisgiv/3cb757b8fe33206c665ffce9a30d8692 to your computer and use it in GitHub Desktop.

for .log to json

../app/lib/my_simple_formatter.rb

class MySimpleFormatter < ActiveSupport::Logger::SimpleFormatter
  def call(severity, timestamp, _progname, message)
    d = {
        level: severity,
        datetime: timestamp.getutc,
        source: 'rails',
        host: 'karlovich',
        log_type: 'rails_log',
        message: message
    }.to_json + "\n"
  end
end

..app/config/environments/development.rb

Rails.application.configure do
  ...
  # for development.log to json
  ## this delete rendering logs
  config.action_view.logger = nil
  ## this allow see .log from info to emergency
  config.log_level = :info
  ## this construct json string
  config.log_formatter = MySimpleFormatter.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment