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

Commit

Permalink
Add tags properly
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Feb 19, 2017
1 parent 791da67 commit 9016002
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 80 deletions.
1 change: 0 additions & 1 deletion lib/timber/contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require "timber/contexts/organization"
require "timber/contexts/runtime"
require "timber/contexts/system"
require "timber/contexts/tags"
require "timber/contexts/user"

module Timber
Expand Down
22 changes: 0 additions & 22 deletions lib/timber/contexts/tags.rb

This file was deleted.

13 changes: 8 additions & 5 deletions lib/timber/log_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
attr_reader :context_snapshot, :event, :level, :message, :progname, :tags, :time

# Creates a log entry suitable to be sent to the Timber API.
# @param severity [Integer] the log level / severity
Expand All @@ -17,11 +17,12 @@ class LogEntry #:nodoc:
# @param event [Timber.Event] structured data representing the log line event. This should be
# an instance of `Timber.Event`.
# @return [LogEntry] the resulting LogEntry object
def initialize(level, time, progname, message, context_snapshot, event)
def initialize(level, time, progname, message, context_snapshot, event, tags)
@level = level
@time = time.utc
@progname = progname
@message = message
@tags = tags

context_snapshot = {} if context_snapshot.nil?
system_context = Contexts::System.new(pid: Process.pid)
Expand All @@ -33,7 +34,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, :tags => tags}

if !event.nil?
hash[:event] = event
Expand All @@ -45,7 +46,7 @@ def as_json(options = {})

hash[:"$schema"] = SCHEMA

if options[:only]
hash = if options[:only]
hash.select do |key, _value|
options[:only].include?(key)
end
Expand All @@ -56,10 +57,12 @@ def as_json(options = {})
else
hash
end

Util::Hash.compact(hash)
end

def to_json(options = {})
Util::Hash.compact(as_json(options)).to_json
as_json(options).to_json
end

def to_msgpack(*args)
Expand Down
14 changes: 12 additions & 2 deletions lib/timber/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,23 @@ class Formatter
def build_log_entry(severity, time, progname, msg)
level = SEVERITY_MAP.fetch(severity)
context_snapshot = CurrentContext.instance.snapshot
tags = extract_active_support_tagged_logging_tags
tags += msg.delete(:tags) if msg.is_a?(Hash) && msg.key?(:tags)
event = Events.build(msg)

if event
LogEntry.new(level, time, progname, event.message, context_snapshot, event)
LogEntry.new(level, time, progname, event.message, context_snapshot, event, tags)
else
LogEntry.new(level, time, progname, msg, context_snapshot, nil)
LogEntry.new(level, time, progname, msg, context_snapshot, nil, tags)
end
end

# Because of all the crazy ways Rails has attempted this we need this crazy method.
def extract_active_support_tagged_logging_tags
Thread.current[:activesupport_tagged_logging_tags] ||
Thread.current["activesupport_tagged_logging_tags:#{object_id}"] ||
[]
end
end

# Structures your log messages into Timber's hybrid format, which makes
Expand Down
43 changes: 0 additions & 43 deletions lib/timber/probes/active_support_tagged_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,13 @@ def call(severity, timestamp, progname, msg)
super(severity, timestamp, progname, "#{tags_text}#{msg}")
end
end

def push_tags(*tags)
_timber_original_push_tags(*tags).tap do
if current_tags.size > 0
context = Contexts::Tags.new(values: current_tags)
CurrentContext.add(context)
end
end
end

def pop_tags(size = 1)
_timber_original_pop_tags(size).tap do
if current_tags.size == 0
CurrentContext.remove(Contexts::Tags)
else
context = Contexts::Tags.new(values: current_tags)
CurrentContext.add(context)
end
end
end
end
end
end

module LoggerMethods
def self.included(klass)
klass.class_eval do
alias_method :_timber_original_push_tags, :push_tags
alias_method :_timber_original_pop_tags, :pop_tags

def push_tags(*tags)
_timber_original_push_tags(*tags).tap do
if current_tags.size > 0
context = Contexts::Tags.new(values: current_tags)
CurrentContext.add(context)
end
end
end

def pop_tags(size = 1)
_timber_original_pop_tags(size).tap do
if current_tags.size == 0
CurrentContext.remove(Contexts::Tags)
else
context = Contexts::Tags.new(values: current_tags)
CurrentContext.add(context)
end
end
end

def add(severity, message = nil, progname = nil, &block)
if message.nil?
if block_given?
Expand Down
8 changes: 4 additions & 4 deletions spec/timber/log_devices/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@

it "should add a request to the queue" do
http = described_class.new("MYKEY", threads: false)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 1", nil, nil)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 1", nil, nil, [])
http.write(log_entry)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 2", nil, nil)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 2", nil, nil, [])
http.write(log_entry)
http.send(:flush)
request_queue = http.instance_variable_get(:@request_queue)
Expand Down Expand Up @@ -109,9 +109,9 @@
to_return(:status => 200, :body => "", :headers => {})

http = described_class.new("MYKEY", flush_interval: 0.1)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 1", nil, nil)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 1", nil, nil, [])
http.write(log_entry)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 2", nil, nil)
log_entry = Timber::LogEntry.new("INFO", time, nil, "test log message 2", nil, nil, [])
http.write(log_entry)
sleep 0.3

Expand Down
4 changes: 2 additions & 2 deletions spec/timber/log_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
it "should encode properly with an event and context" do
event = Timber::Events::Custom.new(type: :event_type, message: "event_message", data: {a: 1})
context = {custom: Timber::Contexts::Custom.new(type: :context_type, data: {b: 1})}
log_entry = described_class.new("INFO", time, nil, "log message", context, event)
log_entry = described_class.new("INFO", time, nil, "log message", context, event, [])
msgpack = log_entry.to_msgpack
expect(msgpack).to start_with("\x86\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xABlog message\xA5event".force_encoding("ASCII-8BIT"))
expect(msgpack).to start_with("\x86\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z".force_encoding("ASCII-8BIT"))
end
end
end
2 changes: 1 addition & 1 deletion spec/timber/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
logger.tagged("tag") do
logger.info(message)
end
expect(io.string).to include("\"context\":{\"tags\":[\"tag\"]")
expect(io.string).to include("\"tags\":[\"tag\"]")
end
end
end
Expand Down

0 comments on commit 9016002

Please sign in to comment.