File: json_spec.rb

package info (click to toggle)
ruby-dry-logger 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 444 kB
  • sloc: ruby: 2,170; makefile: 4; sh: 4
file content (81 lines) | stat: -rw-r--r-- 1,906 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

RSpec.describe Dry::Logger::Formatters::JSON do
  include_context "stream"

  subject(:logger) do
    Dry.Logger(:test, stream: stream, formatter: :json)
  end

  it "when passed as a symbol, it has JSON format for string messages" do
    logger.info("foo")

    expected_json = {
      "progname" => "test",
      "severity" => "INFO",
      "time" => "2017-01-15T15:00:23Z",
      "message" => "foo"
    }

    expect(output).to eql("#{JSON.dump(expected_json)}\n")
    expect(JSON.parse(output)).to eql(expected_json)
  end

  it "has JSON format for string messages" do
    logger.info("foo")

    expect(JSON.parse(output)).to eql(
      "progname" => "test",
      "severity" => "INFO",
      "time" => "2017-01-15T15:00:23Z",
      "message" => "foo"
    )
  end

  it "has JSON format for string messages with payloads" do
    logger.info("foo", test: true)

    expect(JSON.parse(output)).to eql(
      "progname" => "test",
      "severity" => "INFO",
      "time" => "2017-01-15T15:00:23Z",
      "message" => "foo",
      "test" => true
    )
  end

  it "has JSON format for error messages" do
    logger.error(Exception.new("foo"))

    expect(JSON.parse(output)).to eql(
      "progname" => "test",
      "severity" => "ERROR",
      "time" => "2017-01-15T15:00:23Z",
      "message" => "foo",
      "backtrace" => [],
      "exception" => "Exception"
    )
  end

  it "has JSON format for hash messages" do
    logger.info(foo: :bar)

    expect(JSON.parse(output)).to eql(
      "progname" => "test",
      "severity" => "INFO",
      "time" => "2017-01-15T15:00:23Z",
      "foo" => "bar"
    )
  end

  it "has JSON format for not string messages" do
    logger.info(["foo"])

    expect(JSON.parse(output)).to eql(
      "progname" => "test",
      "severity" => "INFO",
      "time" => "2017-01-15T15:00:23Z",
      "message" => ["foo"]
    )
  end
end