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.



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

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

Instance Method Details

#encode_record(record) ⇒ Object



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

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,
  }

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

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

#iso8601_timestamps?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/steno/codec/json.rb', line 46

def iso8601_timestamps?
  @iso8601_timestamps
end