-
Notifications
You must be signed in to change notification settings - Fork 26
/
for_json.rb
46 lines (41 loc) · 974 Bytes
/
for_json.rb
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
# frozen_string_literal: true
module Ougai
# The features for JSON formatter
# @attr [Boolean] jsonize Whether log should converts JSON
# @attr [Boolean] with_newline Whether tailing NL should be appended
module Formatters::ForJson
attr_accessor :jsonize, :with_newline
protected
def init_opts_for_json(opts)
@jsonize = opts.fetch(:jsonize, true)
@with_newline = opts.fetch(:with_newline, true)
@serializer = Ougai::Serializer.for_json
end
def to_level(severity)
case severity
when 'TRACE'
10
when 'DEBUG'
20
when 'INFO'
30
when 'WARN'
40
when 'ERROR'
50
when 'FATAL'
60
else
70
end
end
# requires convert_time(data) method
def dump(data)
return data unless @jsonize
convert_time(data)
str = @serializer.serialize(data)
str << "\n" if @with_newline
str
end
end
end