File: entry.rb

package info (click to toggle)
ruby-dry-logger 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156 kB
  • sloc: ruby: 802; makefile: 4
file content (153 lines) | stat: -rw-r--r-- 2,922 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true

require "time"
require "dry/logger/constants"

module Dry
  module Logger
    # @since 1.0.0
    # @api public
    class Entry
      include Enumerable

      # @since 1.0.0
      # @api public
      attr_reader :progname

      # @since 1.0.0
      # @api public
      attr_reader :severity

      # @since 1.0.0
      # @api public
      attr_reader :tags

      # @since 1.0.0
      # @api public
      attr_reader :level

      # @since 1.0.0
      # @api public
      attr_reader :message

      # @since 1.0.0
      # @api public
      attr_reader :exception

      # @since 1.0.0
      # @api public
      attr_reader :payload

      # @since 1.0.0
      # @api private
      attr_reader :clock

      # @since 1.0.0
      # @api private
      # rubocop:disable Metrics/ParameterLists
      def initialize(clock:, progname:, severity:, tags: EMPTY_ARRAY, message: nil,
                     payload: EMPTY_HASH)
        @clock = clock
        @progname = progname
        @severity = severity.to_s
        @tags = tags
        @level = LEVELS.fetch(severity.to_s)
        @message = message unless message.is_a?(Exception)
        @exception = message if message.is_a?(Exception)
        @payload = build_payload(payload)
      end
      # rubocop:enable Metrics/ParameterLists

      # @since 1.0.0
      # @api public
      def each(&block)
        payload.each(&block)
      end

      # @since 1.0.0
      # @api public
      def [](name)
        payload[name]
      end

      # @since 1.0.0
      # @api public
      def debug?
        level.equal?(DEBUG)
      end

      # @since 1.0.0
      # @api public
      def info?
        level.equal?(INFO)
      end

      # @since 1.0.0
      # @api public
      def warn?
        level.equal?(WARN)
      end

      # @since 1.0.0
      # @api public
      def error?
        level.equal?(ERROR)
      end

      # @since 1.0.0
      # @api public
      def fatal?
        level.equal?(FATAL)
      end

      # @since 1.0.0
      # @api public
      def exception?
        !exception.nil?
      end

      # @since 1.0.0
      # @api public
      def key?(name)
        payload.key?(name)
      end

      # @since 1.0.0
      # @api public
      def tag?(value)
        tags.include?(value)
      end

      # @since 1.0.0
      # @api private
      def meta
        @meta ||= {progname: progname, severity: severity, time: clock.now}
      end

      # @since 1.0.0
      # @api private
      def to_h
        @to_h ||= meta.merge(message: message, **payload)
      end

      # @since 1.0.0
      # @api private
      def filter(filter)
        @payload = filter.call(payload)
        self
      end

      private

      # @since 1.0.0
      # @api private
      def build_payload(payload)
        if exception?
          {exception: exception, **payload}
        else
          payload
        end
      end
    end
  end
end