Sha256: b5a61b08edd82bac55de6796ec1fd9f158d0a382ac57cc6a85e7ba85673d9bdb

Contents?: true

Size: 1.38 KB

Versions: 48

Compression:

Stored size: 1.38 KB

Contents

require 'test_helper'
require 'json'
require 'ansi/code'

class JsonLogFormatterTest < ActiveSupport::TestCase

  def setup
    @fmt = Incline::JsonLogFormatter.new
  end

  test 'should create valid JSON' do
    t = Time.now
    s = @fmt.call(Logger::WARN, t, :ignored, 'Something happened!')
    assert_not s.blank?

    # ends with CRLF?
    assert s =~ /\r\n\Z/

    h = JSON.parse(s.strip) rescue nil
    assert h.is_a?(::Hash)
    assert_equal 'WARN',                          h['level']
    assert_equal t.strftime('%Y-%m-%d %H:%M:%S'), h['time']
    assert_equal 'Something happened!',           h['message']
    assert_equal Rails.application.app_name,      h['app_name']
    assert_equal Rails.application.app_version,   h['app_version']
    assert_equal Process.pid,                     h['process_id']
  end

  test 'should log all CRLF as LF' do
    h = JSON.parse(@fmt.call(Logger::INFO, Time.now, :ignored, "This message has \r\n CRLF \r\n sequences in it!\r\n").strip)
    assert_not h['message'] =~ /\r\n/
    assert h['message'] =~ /\n/
  end

  test 'should strip out ANSI formatting' do
    test = 'This text has ' + ANSI.ansi('red', :bright, :red) + ' and ' + ANSI.ansi('green', :green) + ' text.'
    clean = 'This text has red and green text.'
    assert_not_equal clean, test
    h = JSON.parse(@fmt.call(Logger::INFO, Time.now, :ignored, test))
    assert_equal clean, h['message']
  end

end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
incline-0.3.14 test/lib/json_log_formatter_test.rb
incline-0.3.13 test/lib/json_log_formatter_test.rb
incline-0.3.12 test/lib/json_log_formatter_test.rb
incline-0.3.11 test/lib/json_log_formatter_test.rb
incline-0.3.10 test/lib/json_log_formatter_test.rb
incline-0.3.9 test/lib/json_log_formatter_test.rb
incline-0.3.8 test/lib/json_log_formatter_test.rb
incline-0.3.7 test/lib/json_log_formatter_test.rb
incline-0.3.6 test/lib/json_log_formatter_test.rb
incline-0.3.5 test/lib/json_log_formatter_test.rb
incline-0.3.4 test/lib/json_log_formatter_test.rb
incline-0.3.3 test/lib/json_log_formatter_test.rb
incline-0.3.2 test/lib/json_log_formatter_test.rb
incline-0.3.1 test/lib/json_log_formatter_test.rb
incline-0.3.0 test/lib/json_log_formatter_test.rb
incline-0.2.36 test/lib/json_log_formatter_test.rb
incline-0.2.35 test/lib/json_log_formatter_test.rb
incline-0.2.34 test/lib/json_log_formatter_test.rb
incline-0.2.28 test/lib/json_log_formatter_test.rb
incline-0.2.27 test/lib/json_log_formatter_test.rb