Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Specify the log event schema (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Feb 18, 2017
1 parent 0e4e223 commit 40c1be5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/timber/log_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Timber
# `Logger` and the log device that you set it up with.
class LogEntry #:nodoc:
DT_PRECISION = 6.freeze
SCHEMA = "https://raw.githubusercontent.com/timberio/log-event-json-schema/1.2.2/schema.json".freeze

attr_reader :level, :time, :progname, :message, :context_snapshot, :event

Expand Down Expand Up @@ -32,7 +33,7 @@ def initialize(level, time, progname, message, context_snapshot, event)

def as_json(options = {})
options ||= {}
hash = {level: level, dt: formatted_dt, message: message}
hash = {:level => level, :dt => formatted_dt, :message => message}

if !event.nil?
hash[:event] = event
Expand All @@ -42,6 +43,8 @@ def as_json(options = {})
hash[:context] = context_snapshot
end

hash[:"$schema"] = SCHEMA

if options[:only]
hash.select do |key, _value|
options[:only].include?(key)
Expand Down
4 changes: 2 additions & 2 deletions spec/timber/log_devices/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
request_queue = http.instance_variable_get(:@request_queue)
request = request_queue.deq
expect(request).to be_kind_of(Net::HTTP::Post)
expect(request.body).to start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT"))
expect(request.body).to start_with("\x92\x85\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1\xA7context\x81".force_encoding("ASCII-8BIT"))

message_queue = http.instance_variable_get(:@msg_queue)
expect(message_queue.size).to eq(0)
Expand Down Expand Up @@ -98,7 +98,7 @@
it "should start a intervaled flush thread and flush on an interval" do
stub = stub_request(:post, "https://logs.timber.io/frames").
with(
:body => start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")),
:body => start_with("\x92\x85\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1\xA7context\x81\xA6system".force_encoding("ASCII-8BIT")),
:headers => {
'Accept' => 'application/json',
'Authorization' => 'Basic TVlLRVk=',
Expand Down
2 changes: 1 addition & 1 deletion spec/timber/log_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
context = {custom: Timber::Contexts::Custom.new(type: :context_type, data: {b: 1})}
log_entry = described_class.new("INFO", time, nil, "log message", context, event)
msgpack = log_entry.to_msgpack
expect(msgpack).to start_with("\x85\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xABlog message".force_encoding("ASCII-8BIT"))
expect(msgpack).to start_with("\x86\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xABlog message\xA5event".force_encoding("ASCII-8BIT"))
end
end
end

0 comments on commit 40c1be5

Please sign in to comment.