aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/clean_logger_test.rb
blob: 970e97d492c3fb365272a6280a73c6a5cecd95e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'abstract_unit'
require 'stringio'
require 'active_support/core_ext/logger'
require 'active_support/testing/deprecation'

class CleanLoggerTest < Test::Unit::TestCase
  include ActiveSupport::Testing::Deprecation

  def setup
    @out = StringIO.new
    @logger = Logger.new(@out)
  end

  def test_format_message
    @logger.error 'error'
    assert_equal "error\n", @out.string
  end

  def test_silence
    assert_deprecated do
      # Without yielding self.
      @logger.silence do
        @logger.debug  'debug'
        @logger.info   'info'
        @logger.warn   'warn'
        @logger.error  'error'
        @logger.fatal  'fatal'
      end
    end

    assert_deprecated do
      # Yielding self.
      @logger.silence do |logger|
        logger.debug  'debug'
        logger.info   'info'
        logger.warn   'warn'
        logger.error  'error'
        logger.fatal  'fatal'
      end
    end

    # Silencer off.
    Logger.silencer = false
    assert_deprecated do
      @logger.silence do |logger|
        logger.warn   'unsilenced'
      end
    end
    Logger.silencer = true

    assert_equal "error\nfatal\nerror\nfatal\nunsilenced\n", @out.string
  end

  def test_datetime_format
    @logger.formatter = Logger::Formatter.new
    @logger.datetime_format = "%Y-%m-%d"
    @logger.debug 'debug'
    assert_equal "%Y-%m-%d", @logger.datetime_format
    assert_match(/D, \[\d\d\d\d-\d\d-\d\d#\d+\] DEBUG -- : debug/, @out.string)
  end

  def test_nonstring_formatting
    an_object = [1, 2, 3, 4, 5]
    @logger.debug an_object
    assert_equal("#{an_object.inspect}\n", @out.string)
  end
end