File: json.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 (50 lines) | stat: -rw-r--r-- 1,077 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
# frozen_string_literal: true

require "json"

require "dry/logger/constants"
require "dry/logger/formatters/structured"

module Dry
  module Logger
    module Formatters
      # JSON formatter.
      #
      # This formatter returns log entries in JSON format.
      #
      # @since 0.1.0
      # @api public
      class JSON < Structured
        # @since 0.1.0
        # @api private
        def format(entry)
          hash = format_values(entry).compact
          hash.update(hash.delete(:exception)) if entry.exception?
          ::JSON.dump(hash)
        end

        # @since 0.1.0
        # @api private
        def format_severity(value)
          value.upcase
        end

        # @since 0.1.0
        # @api private
        def format_exception(value)
          {
            exception: value.class,
            message: value.message,
            backtrace: value.backtrace || EMPTY_ARRAY
          }
        end

        # @since 0.1.0
        # @api private
        def format_time(value)
          value.getutc.iso8601
        end
      end
    end
  end
end