summaryrefslogtreecommitdiff
path: root/spec/ruby/library/logger/logger/datetime_format_spec.rb
blob: 582b34bfdafd34a0076155e0f99798d42bca13d1 (plain)
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
require_relative '../../../spec_helper'
require_relative '../fixtures/common'

describe "Logger#datetime_format" do
  before :each do
    @path = tmp("test_log.log")
    @log_file = File.open(@path, "w+")
    @logger = Logger.new(@path)
  end

  after :each do
    @logger.close
    @log_file.close unless @log_file.closed?
    rm_r @path
  end

  it "returns the date format used for the logs" do
    format = "%Y-%d"
    @logger.datetime_format = format
    @logger.datetime_format.should == format
  end

  it "returns nil logger is using the default date format" do
    @logger.datetime_format.should == nil
  end
end

describe "Logger#datetime_format=" do
  before :each do
    @path = tmp("test_log.log")
    @log_file = File.open(@path, "w+")
    @logger = Logger.new(@path)
  end

  after :each do
    @logger.close
    @log_file.close unless @log_file.closed?
    rm_r @path
  end

  it "sets the date format for the logs" do
    @logger.datetime_format = "%Y"
    @logger.datetime_format.should == "%Y"
    @logger.add(Logger::WARN, "Test message")
    @log_file.rewind

    regex = /2[0-9]{3}.*Test message/
    @log_file.readlines.first.should =~ regex
  end

  it "follows the Time#strftime format" do
    -> { @logger.datetime_format = "%Y-%m" }.should_not raise_error

    regex = /\d{4}-\d{2}-\d{2}oo-\w+ar/
    @logger.datetime_format = "%Foo-%Bar"
    @logger.add(nil, "Test message")
    @log_file.rewind
    @log_file.readlines.first.should =~ regex
  end
end