Class: Steno::Codec::Json

Inherits:
Base show all
Defined in:
lib/steno/codec/json.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Json

Returns a new instance of Json.



11
12
13
# File 'lib/steno/codec/json.rb', line 11

def initialize(opts = {})
  @iso8601_timestamps = opts[:iso8601_timestamps] || false
end

Instance Method Details

#encode_record(record) ⇒ Object



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
# File 'lib/steno/codec/json.rb', line 15

def encode_record(record)
  msg =
    if record.message.valid_encoding?
      record.message
    else
      # Treat the message as an arbitrary sequence of bytes.
      escape_nonprintable_ascii(record.message.dup.force_encoding('BINARY'))
    end

  h = {
    'timestamp' => record.timestamp.to_f,
    'message' => msg,
    'log_level' => record.log_level.to_s,
    'source' => record.source,
    'data' => record.data,
    'thread_id' => record.thread_id,
    'fiber_id' => record.fiber_id,
    'process_id' => record.process_id,
    'file' => record.file,
    'lineno' => record.lineno,
    'method' => record.method
  }

  h['timestamp'] = Time.at(record.timestamp).utc.iso8601(6) if iso8601_timestamps?

  Yajl::Encoder.encode(h) + "\n"
end

#iso8601_timestamps?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/steno/codec/json.rb', line 43

def iso8601_timestamps?
  @iso8601_timestamps
end