timber 2.6.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.travis.yml +8 -38
- data/CHANGELOG.md +9 -0
- data/README.md +30 -284
- data/Rakefile +78 -0
- data/lib/timber.rb +6 -6
- data/lib/timber/config.rb +1 -83
- data/lib/timber/config/integrations.rb +1 -47
- data/lib/timber/context.rb +3 -24
- data/lib/timber/contexts.rb +2 -30
- data/lib/timber/contexts/http.rb +16 -36
- data/lib/timber/contexts/release.rb +12 -23
- data/lib/timber/contexts/runtime.rb +9 -36
- data/lib/timber/contexts/session.rb +8 -21
- data/lib/timber/contexts/system.rb +9 -16
- data/lib/timber/contexts/user.rb +13 -33
- data/lib/timber/current_context.rb +16 -78
- data/lib/timber/event.rb +12 -9
- data/lib/timber/events.rb +1 -33
- data/lib/timber/events/controller_call.rb +20 -31
- data/lib/timber/events/error.rb +18 -26
- data/lib/timber/events/exception.rb +1 -0
- data/lib/timber/events/sql_query.rb +14 -24
- data/lib/timber/events/template_render.rb +13 -24
- data/lib/timber/integration.rb +1 -1
- data/lib/timber/integrator.rb +1 -1
- data/lib/timber/log_devices/http.rb +98 -19
- data/lib/timber/log_entry.rb +6 -24
- data/lib/timber/logger.rb +5 -14
- data/lib/timber/util.rb +1 -6
- data/lib/timber/util/non_nil_hash_builder.rb +3 -1
- data/lib/timber/version.rb +1 -1
- data/spec/README.md +2 -8
- data/spec/spec_helper.rb +0 -7
- data/spec/support/timber.rb +1 -3
- data/spec/timber/current_context_spec.rb +12 -50
- data/spec/timber/events/controller_call_spec.rb +4 -4
- data/spec/timber/events/error_spec.rb +4 -9
- data/spec/timber/log_devices/http_spec.rb +26 -2
- data/spec/timber/log_entry_spec.rb +12 -6
- data/spec/timber/logger_spec.rb +27 -68
- data/timber.gemspec +1 -1
- metadata +5 -139
- data/gemfiles/rails-3.0.gemfile +0 -5
- data/gemfiles/rails-3.1.gemfile +0 -5
- data/gemfiles/rails-3.2.gemfile +0 -5
- data/gemfiles/rails-4.0.gemfile +0 -9
- data/gemfiles/rails-4.1.gemfile +0 -9
- data/gemfiles/rails-4.2.gemfile +0 -9
- data/gemfiles/rails-5.0.gemfile +0 -9
- data/gemfiles/rails-5.1.gemfile +0 -9
- data/gemfiles/rails-edge.gemfile +0 -7
- data/lib/timber/cli.rb +0 -60
- data/lib/timber/cli/api.rb +0 -183
- data/lib/timber/cli/api/application.rb +0 -34
- data/lib/timber/cli/config_file.rb +0 -71
- data/lib/timber/cli/file_helper.rb +0 -53
- data/lib/timber/cli/installer.rb +0 -70
- data/lib/timber/cli/installers.rb +0 -102
- data/lib/timber/cli/installers/config_file.rb +0 -51
- data/lib/timber/cli/installers/other.rb +0 -59
- data/lib/timber/cli/installers/rails.rb +0 -225
- data/lib/timber/cli/installers/root.rb +0 -116
- data/lib/timber/cli/io.rb +0 -100
- data/lib/timber/cli/io/ansi.rb +0 -22
- data/lib/timber/cli/io/messages.rb +0 -198
- data/lib/timber/cli/os_helper.rb +0 -74
- data/lib/timber/config/integrations/rack.rb +0 -74
- data/lib/timber/contexts/custom.rb +0 -44
- data/lib/timber/contexts/organization.rb +0 -48
- data/lib/timber/events/custom.rb +0 -53
- data/lib/timber/events/http_request.rb +0 -71
- data/lib/timber/events/http_response.rb +0 -81
- data/lib/timber/frameworks.rb +0 -19
- data/lib/timber/frameworks/rails.rb +0 -27
- data/lib/timber/integrations.rb +0 -29
- data/lib/timber/integrations/action_controller.rb +0 -18
- data/lib/timber/integrations/action_controller/log_subscriber.rb +0 -27
- data/lib/timber/integrations/action_controller/log_subscriber/timber_log_subscriber.rb +0 -46
- data/lib/timber/integrations/action_dispatch.rb +0 -23
- data/lib/timber/integrations/action_dispatch/debug_exceptions.rb +0 -53
- data/lib/timber/integrations/action_view.rb +0 -18
- data/lib/timber/integrations/action_view/log_subscriber.rb +0 -27
- data/lib/timber/integrations/action_view/log_subscriber/timber_log_subscriber.rb +0 -83
- data/lib/timber/integrations/active_record.rb +0 -18
- data/lib/timber/integrations/active_record/log_subscriber.rb +0 -26
- data/lib/timber/integrations/active_record/log_subscriber/timber_log_subscriber.rb +0 -53
- data/lib/timber/integrations/rack.rb +0 -27
- data/lib/timber/integrations/rack/error_event.rb +0 -64
- data/lib/timber/integrations/rack/http_context.rb +0 -27
- data/lib/timber/integrations/rack/http_events.rb +0 -210
- data/lib/timber/integrations/rack/middleware.rb +0 -28
- data/lib/timber/integrations/rack/session_context.rb +0 -65
- data/lib/timber/integrations/rack/user_context.rb +0 -135
- data/lib/timber/integrations/rails.rb +0 -22
- data/lib/timber/integrations/rails/rack_logger.rb +0 -60
- data/lib/timber/overrides.rb +0 -12
- data/lib/timber/overrides/active_support_3_tagged_logging.rb +0 -111
- data/lib/timber/overrides/active_support_buffered_logger.rb +0 -22
- data/lib/timber/overrides/active_support_tagged_logging.rb +0 -66
- data/lib/timber/overrides/lograge.rb +0 -18
- data/lib/timber/overrides/rails_stdout_logging.rb +0 -21
- data/lib/timber/util/active_support_log_subscriber.rb +0 -37
- data/lib/timber/util/attribute_normalizer.rb +0 -89
- data/lib/timber/util/hash.rb +0 -90
- data/lib/timber/util/request.rb +0 -72
- data/lib/timber/util/struct.rb +0 -16
- data/spec/rails/tagged_logging_spec.rb +0 -44
- data/spec/support/action_controller.rb +0 -8
- data/spec/support/active_record.rb +0 -32
- data/spec/support/rails.rb +0 -67
- data/spec/support/rails/templates/_partial.html +0 -1
- data/spec/support/rails/templates/template.html +0 -1
- data/spec/timber/cli/config_file_spec.rb +0 -26
- data/spec/timber/cli/installers/config_file_spec.rb +0 -36
- data/spec/timber/cli/installers/other_spec.rb +0 -49
- data/spec/timber/cli/installers/rails_spec.rb +0 -364
- data/spec/timber/cli/installers/root_spec.rb +0 -73
- data/spec/timber/config_spec.rb +0 -28
- data/spec/timber/contexts/custom_spec.rb +0 -11
- data/spec/timber/contexts/organization_spec.rb +0 -11
- data/spec/timber/contexts/runtime_spec.rb +0 -11
- data/spec/timber/contexts/system_spec.rb +0 -11
- data/spec/timber/contexts/user_spec.rb +0 -11
- data/spec/timber/contexts_spec.rb +0 -49
- data/spec/timber/event_spec.rb +0 -10
- data/spec/timber/events/custom_spec.rb +0 -36
- data/spec/timber/events/http_request_spec.rb +0 -32
- data/spec/timber/events/http_response_spec.rb +0 -12
- data/spec/timber/events_spec.rb +0 -55
- data/spec/timber/integrations/action_controller/log_subscriber_spec.rb +0 -55
- data/spec/timber/integrations/action_dispatch/debug_exceptions_spec.rb +0 -53
- data/spec/timber/integrations/action_view/log_subscriber_spec.rb +0 -115
- data/spec/timber/integrations/active_record/log_subscriber_spec.rb +0 -46
- data/spec/timber/integrations/rack/error_event_spec.rb +0 -63
- data/spec/timber/integrations/rack/http_context_spec.rb +0 -60
- data/spec/timber/integrations/rack/http_events_spec.rb +0 -101
- data/spec/timber/integrations/rack/session_context_spec.rb +0 -62
- data/spec/timber/integrations/rails/rack_logger_spec.rb +0 -58
- data/spec/timber/util/attribute_normalizer_spec.rb +0 -90
- data/spec/timber/util/hash_spec.rb +0 -30
- data/spec/timber/util/request_spec.rb +0 -10
data/lib/timber/events/error.rb
CHANGED
@@ -1,38 +1,20 @@
|
|
1
|
-
require "timber/event"
|
2
1
|
require "timber/util"
|
2
|
+
require "timber/event"
|
3
3
|
|
4
4
|
module Timber
|
5
5
|
module Events
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# @note This event should be installed automatically through integrations,
|
9
|
-
# such as the {Integrations::ActionDispatch::DebugExceptions} integration.
|
6
|
+
# @private
|
10
7
|
class Error < Timber::Event
|
11
|
-
|
12
|
-
MESSAGE_MAX_BYTES = 8192.freeze
|
13
|
-
|
14
|
-
attr_reader :name, :error_message, :backtrace
|
8
|
+
attr_reader :name, :error_message, :backtrace_json
|
15
9
|
|
16
10
|
def initialize(attributes)
|
17
|
-
|
18
|
-
@
|
19
|
-
@error_message = normalizer.fetch(:error_message, :string, :limit => MESSAGE_MAX_BYTES)
|
20
|
-
@backtrace = normalizer.fetch(:backtrace, :array)
|
21
|
-
end
|
11
|
+
@name = attributes[:name]
|
12
|
+
@error_message = attributes[:error_message]
|
22
13
|
|
23
|
-
|
24
|
-
|
25
|
-
h.add(:name, name)
|
26
|
-
h.add(:message, error_message)
|
27
|
-
h.add(:backtrace_json, backtrace, :json_encode => true, :limit => BACKTRACE_JSON_MAX_BYTES)
|
14
|
+
if attributes[:backtrace]
|
15
|
+
@backtrace_json = attributes[:backtrace].to_json
|
28
16
|
end
|
29
17
|
end
|
30
|
-
alias to_h to_hash
|
31
|
-
|
32
|
-
# Builds a hash representation containing simple objects, suitable for serialization (JSON).
|
33
|
-
def as_json(_options = {})
|
34
|
-
{:error => to_hash}
|
35
|
-
end
|
36
18
|
|
37
19
|
def message
|
38
20
|
message = "#{name}"
|
@@ -43,6 +25,16 @@ module Timber
|
|
43
25
|
|
44
26
|
message
|
45
27
|
end
|
28
|
+
|
29
|
+
def to_hash
|
30
|
+
{
|
31
|
+
error: {
|
32
|
+
name: name,
|
33
|
+
message: error_message,
|
34
|
+
backtrace_json: backtrace_json
|
35
|
+
}
|
36
|
+
}
|
37
|
+
end
|
46
38
|
end
|
47
39
|
end
|
48
|
-
end
|
40
|
+
end
|
@@ -1,36 +1,26 @@
|
|
1
|
-
require
|
1
|
+
require 'timber/util'
|
2
|
+
require 'timber/event'
|
2
3
|
|
3
4
|
module Timber
|
4
5
|
module Events
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @note This event should be installed automatically through integrations,
|
8
|
-
# such as the {Integrations::ActiveRecord::LogSubscriber} integration.
|
6
|
+
# @private
|
9
7
|
class SQLQuery < Timber::Event
|
10
|
-
|
11
|
-
SQL_MAX_BYTES = 4096.freeze
|
12
|
-
|
13
|
-
attr_reader :sql, :time_ms, :message
|
8
|
+
attr_reader :sql, :duration_ms, :message
|
14
9
|
|
15
10
|
def initialize(attributes)
|
16
|
-
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@time_ms = normalizer.fetch!(:time_ms, :float, :precision => 6)
|
11
|
+
@sql = attributes[:sql]
|
12
|
+
@duration_ms = attributes[:duration_ms]
|
13
|
+
@message = attributes[:message]
|
20
14
|
end
|
21
15
|
|
22
16
|
def to_hash
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# Builds a hash representation containing simple objects, suitable for serialization (JSON).
|
31
|
-
def as_json(_options = {})
|
32
|
-
{:sql_query => to_hash}
|
17
|
+
{
|
18
|
+
sql_query_executed: Util::NonNilHashBuilder.build do |h|
|
19
|
+
h.add(:sql, sql)
|
20
|
+
h.add(:duration_ms, duration_ms)
|
21
|
+
end
|
22
|
+
}
|
33
23
|
end
|
34
24
|
end
|
35
25
|
end
|
36
|
-
end
|
26
|
+
end
|
@@ -1,36 +1,25 @@
|
|
1
|
+
require "timber/util"
|
1
2
|
require "timber/event"
|
2
|
-
|
3
3
|
module Timber
|
4
4
|
module Events
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @note This event should be installed automatically through integrations,
|
8
|
-
# such as the {Integrations::ActionView::LogSubscriber} integration.
|
5
|
+
# @private
|
9
6
|
class TemplateRender < Timber::Event
|
10
|
-
|
11
|
-
NAME_MAX_BYTES = 1024.freeze
|
12
|
-
|
13
|
-
attr_reader :message, :name, :time_ms
|
7
|
+
attr_reader :message, :name, :duration_ms
|
14
8
|
|
15
9
|
def initialize(attributes)
|
16
|
-
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@time_ms = normalizer.fetch!(:time_ms, :float, :precision => 6)
|
10
|
+
@name = attributes[:name]
|
11
|
+
@duration_ms = attributes[:duration_ms]
|
12
|
+
@message = attributes[:message]
|
20
13
|
end
|
21
14
|
|
22
15
|
def to_hash
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# Builds a hash representation containing simple objects, suitable for serialization (JSON).
|
31
|
-
def as_json(_options = {})
|
32
|
-
{:template_render => to_hash}
|
16
|
+
{
|
17
|
+
template_rendered: Util::NonNilHashBuilder.build do |h|
|
18
|
+
h.add(:name, name)
|
19
|
+
h.add(:duration_ms, duration_ms)
|
20
|
+
end
|
21
|
+
}
|
33
22
|
end
|
34
23
|
end
|
35
24
|
end
|
36
|
-
end
|
25
|
+
end
|
data/lib/timber/integration.rb
CHANGED
data/lib/timber/integrator.rb
CHANGED
@@ -17,9 +17,11 @@ module Timber
|
|
17
17
|
#
|
18
18
|
# See {#initialize} for options and more details.
|
19
19
|
class HTTP
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
TIMBER_STAGING_HOST = "logs-staging.timber.io".freeze
|
21
|
+
TIMBER_PRODUCTION_HOST = "logs.timber.io".freeze
|
22
|
+
TIMBER_HOST = ENV['TIMBER_STAGING'] ? TIMBER_STAGING_HOST : TIMBER_PRODUCTION_HOST
|
23
|
+
TIMBER_PORT = 443
|
24
|
+
TIMBER_SCHEME = "https".freeze
|
23
25
|
CONTENT_TYPE = "application/msgpack".freeze
|
24
26
|
USER_AGENT = "Timber Ruby/#{Timber::VERSION} (HTTP)".freeze
|
25
27
|
|
@@ -58,8 +60,8 @@ module Timber
|
|
58
60
|
# requests new requests will be dropped. If you'd prefer to apply back pressure,
|
59
61
|
# ensuring you do not lose log data, pass a standard {SizedQueue}. See examples for
|
60
62
|
# an example.
|
61
|
-
# @option attributes [Symbol] :
|
62
|
-
# default is set via {
|
63
|
+
# @option attributes [Symbol] :timber_host The Timber host to delivery the log lines to.
|
64
|
+
# The default is set via {TIMBER_HOST}.
|
63
65
|
#
|
64
66
|
# @example Basic usage
|
65
67
|
# Timber::Logger.new(Timber::LogDevices::HTTP.new("my_timber_api_key"))
|
@@ -67,9 +69,21 @@ module Timber
|
|
67
69
|
# @example Apply back pressure instead of dropping messages
|
68
70
|
# http_log_device = Timber::LogDevices::HTTP.new("my_timber_api_key", request_queue: SizedQueue.new(25))
|
69
71
|
# Timber::Logger.new(http_log_device)
|
70
|
-
def initialize(api_key,
|
72
|
+
def initialize(api_key, *args)
|
73
|
+
options = {}
|
74
|
+
|
75
|
+
# Timber 3.0 introduced a second argument `source_id` which is required for the new
|
76
|
+
# Timber API keys. For backwards compability we still support the old source specific
|
77
|
+
# API keys that do not require an explicit source ID.
|
78
|
+
if args.last.is_a?(Hash)
|
79
|
+
options = args.pop
|
80
|
+
end
|
81
|
+
|
71
82
|
@api_key = api_key || raise(ArgumentError.new("The api_key parameter cannot be blank"))
|
72
|
-
@
|
83
|
+
@source_id = args.first
|
84
|
+
@timber_host = options[:timber_host] || ENV['TIMBER_HOST'] || TIMBER_HOST
|
85
|
+
@timber_port = options[:timber_port] || ENV['TIMBER_PORT'] || TIMBER_PORT
|
86
|
+
@timber_scheme = options[:timber_scheme] || ENV['TIMBER_SCHEME'] || TIMBER_SCHEME
|
73
87
|
@batch_size = options[:batch_size] || 1_000
|
74
88
|
@flush_continuously = options[:flush_continuously] != false
|
75
89
|
@flush_interval = options[:flush_interval] || 2 # 2 seconds
|
@@ -121,6 +135,58 @@ module Timber
|
|
121
135
|
@request_outlet_thread.kill if @request_outlet_thread
|
122
136
|
end
|
123
137
|
|
138
|
+
def deliver_one(msg)
|
139
|
+
http = build_http
|
140
|
+
|
141
|
+
begin
|
142
|
+
resp = http.start do |conn|
|
143
|
+
req = build_request([msg])
|
144
|
+
@requests_in_flight += 1
|
145
|
+
conn.request(req)
|
146
|
+
end
|
147
|
+
return resp
|
148
|
+
rescue => e
|
149
|
+
Timber::Config.instance.debug { "error: #{e.message}" }
|
150
|
+
return e
|
151
|
+
ensure
|
152
|
+
http.finish if http.started?
|
153
|
+
@requests_in_flight -= 1
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def verify_delivery!
|
158
|
+
5.times do |i|
|
159
|
+
sleep(2)
|
160
|
+
|
161
|
+
if @last_resp.nil?
|
162
|
+
print "."
|
163
|
+
elsif @last_resp.code == "202"
|
164
|
+
puts "Log delivery successful! View your logs at https://app.timber.io"
|
165
|
+
else
|
166
|
+
raise <<-MESSAGE
|
167
|
+
|
168
|
+
Log delivery failed!
|
169
|
+
|
170
|
+
Status: #{@last_resp.code}
|
171
|
+
Body: #{@last_resp.body}
|
172
|
+
|
173
|
+
You can enable internal Timber debug logging with the following:
|
174
|
+
|
175
|
+
Timber::Config.instance.debug_logger = ::Logger.new(STDOUT)
|
176
|
+
MESSAGE
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
raise <<-MESSAGE
|
181
|
+
|
182
|
+
Log delivery failed! No request was made.
|
183
|
+
|
184
|
+
You can enable internal debug logging with the following:
|
185
|
+
|
186
|
+
Timber::Config.instance.debug_logger = ::Logger.new(STDOUT)
|
187
|
+
MESSAGE
|
188
|
+
end
|
189
|
+
|
124
190
|
private
|
125
191
|
# This is a convenience method to ensure the flush thread are
|
126
192
|
# started. This is called lazily from {#write} so that we
|
@@ -139,15 +205,12 @@ module Timber
|
|
139
205
|
end
|
140
206
|
|
141
207
|
# Builds an HTTP request based on the current messages queued.
|
142
|
-
def build_request
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
req = Net::HTTP::Post.new(@timber_url.path)
|
208
|
+
def build_request(msgs)
|
209
|
+
path = @source_id.nil? ? "/frames" : "/sources/#{@source_id}/frames"
|
210
|
+
req = Net::HTTP::Post.new(path)
|
147
211
|
req['Authorization'] = authorization_payload
|
148
212
|
req['Content-Type'] = CONTENT_TYPE
|
149
213
|
req['User-Agent'] = USER_AGENT
|
150
|
-
|
151
214
|
req.body = msgs.to_msgpack
|
152
215
|
req
|
153
216
|
end
|
@@ -159,7 +222,10 @@ module Timber
|
|
159
222
|
# imposed limit.
|
160
223
|
def flush_async
|
161
224
|
@last_async_flush = Time.now
|
162
|
-
|
225
|
+
msgs = @msg_queue.flush
|
226
|
+
return if msgs.empty?
|
227
|
+
|
228
|
+
req = build_request(msgs)
|
163
229
|
if !req.nil?
|
164
230
|
Timber::Config.instance.debug { "New request placed on queue" }
|
165
231
|
request_attempt = RequestAttempt.new(req)
|
@@ -214,9 +280,9 @@ module Timber
|
|
214
280
|
|
215
281
|
# Builds an `Net::HTTP` object to deliver requests over.
|
216
282
|
def build_http
|
217
|
-
http = Net::HTTP.new(@
|
283
|
+
http = Net::HTTP.new(@timber_host, @timber_port)
|
218
284
|
http.set_debug_output(Config.instance.debug_logger) if Config.instance.debug_logger
|
219
|
-
if @
|
285
|
+
if @timber_scheme == 'https'
|
220
286
|
http.use_ssl = true
|
221
287
|
# Verification on Windows fails despite having a valid certificate.
|
222
288
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
@@ -287,7 +353,16 @@ module Timber
|
|
287
353
|
end
|
288
354
|
|
289
355
|
num_reqs += 1
|
290
|
-
|
356
|
+
|
357
|
+
@last_resp = resp
|
358
|
+
|
359
|
+
Timber::Config.instance.debug do
|
360
|
+
if resp.code == "202"
|
361
|
+
"Logs successfully sent! View your logs at https://app.timber.io"
|
362
|
+
else
|
363
|
+
"Log delivery failed! status: #{resp.code}, body: #{resp.body}"
|
364
|
+
end
|
365
|
+
end
|
291
366
|
end
|
292
367
|
end
|
293
368
|
|
@@ -296,8 +371,12 @@ module Timber
|
|
296
371
|
|
297
372
|
# Builds the `Authorization` header value for HTTP delivery to the Timber API.
|
298
373
|
def authorization_payload
|
299
|
-
@authorization_payload ||=
|
374
|
+
@authorization_payload ||= if @source_id.nil?
|
375
|
+
"Basic #{Base64.urlsafe_encode64(@api_key).chomp}"
|
376
|
+
else
|
377
|
+
"Bearer #{@api_key}"
|
378
|
+
end
|
300
379
|
end
|
301
380
|
end
|
302
381
|
end
|
303
|
-
end
|
382
|
+
end
|
data/lib/timber/log_entry.rb
CHANGED
@@ -11,7 +11,6 @@ module Timber
|
|
11
11
|
BINARY_LIMIT_THRESHOLD = 1_000.freeze
|
12
12
|
DT_PRECISION = 6.freeze
|
13
13
|
MESSAGE_MAX_BYTES = 8192.freeze
|
14
|
-
SCHEMA = "https://raw.githubusercontent.com/timberio/log-event-json-schema/v3.2.0/schema.json".freeze
|
15
14
|
|
16
15
|
attr_reader :context_snapshot, :event, :level, :message, :progname, :tags, :time
|
17
16
|
|
@@ -41,7 +40,7 @@ module Timber
|
|
41
40
|
end
|
42
41
|
|
43
42
|
# Builds a hash representation containing simple objects, suitable for serialization (JSON).
|
44
|
-
def
|
43
|
+
def to_hash(options = {})
|
45
44
|
options ||= {}
|
46
45
|
hash = {
|
47
46
|
:level => level,
|
@@ -54,15 +53,13 @@ module Timber
|
|
54
53
|
end
|
55
54
|
|
56
55
|
if !event.nil?
|
57
|
-
hash
|
56
|
+
hash.merge!(event)
|
58
57
|
end
|
59
58
|
|
60
59
|
if !context_snapshot.nil? && context_snapshot.length > 0
|
61
60
|
hash[:context] = context_snapshot
|
62
61
|
end
|
63
62
|
|
64
|
-
hash[:"$schema"] = SCHEMA
|
65
|
-
|
66
63
|
if options[:only]
|
67
64
|
hash.select do |key, _value|
|
68
65
|
options[:only].include?(key)
|
@@ -81,31 +78,16 @@ module Timber
|
|
81
78
|
end
|
82
79
|
|
83
80
|
def to_json(options = {})
|
84
|
-
|
81
|
+
to_hash.to_json
|
85
82
|
end
|
86
83
|
|
87
84
|
def to_msgpack(*args)
|
88
|
-
|
85
|
+
to_hash.to_msgpack(*args)
|
89
86
|
end
|
90
87
|
|
91
88
|
# This is used when LogEntry objects make it to a non-Timber logger.
|
92
89
|
def to_s
|
93
|
-
|
94
|
-
|
95
|
-
if !event.nil?
|
96
|
-
event_hash = event.as_json
|
97
|
-
event_type = event_hash.keys.first
|
98
|
-
|
99
|
-
event_type = if event.is_a?(Events::Custom)
|
100
|
-
"#{event_type}.#{event.type}"
|
101
|
-
else
|
102
|
-
"#{event_type}"
|
103
|
-
end
|
104
|
-
|
105
|
-
log_message = "#{message} [#{event_type}]"
|
106
|
-
end
|
107
|
-
|
108
|
-
log_message + "\n"
|
90
|
+
message + "\n"
|
109
91
|
end
|
110
92
|
|
111
93
|
private
|
@@ -125,4 +107,4 @@ module Timber
|
|
125
107
|
nil
|
126
108
|
end
|
127
109
|
end
|
128
|
-
end
|
110
|
+
end
|
data/lib/timber/logger.rb
CHANGED
@@ -3,7 +3,6 @@ require "msgpack"
|
|
3
3
|
|
4
4
|
require "timber/config"
|
5
5
|
require "timber/current_context"
|
6
|
-
require "timber/event"
|
7
6
|
require "timber/log_devices"
|
8
7
|
require "timber/log_entry"
|
9
8
|
|
@@ -39,7 +38,7 @@ module Timber
|
|
39
38
|
|
40
39
|
if logged_obj.is_a?(Event)
|
41
40
|
LogEntry.new(level, time, progname, logged_obj.message, context_snapshot, logged_obj,
|
42
|
-
|
41
|
+
tags: tags)
|
43
42
|
elsif logged_obj.is_a?(Hash)
|
44
43
|
# Extract the tags
|
45
44
|
tags = tags.clone
|
@@ -47,11 +46,9 @@ module Timber
|
|
47
46
|
tags.concat(logged_obj.delete(:tags)) if logged_obj.key?(:tags)
|
48
47
|
tags.uniq!
|
49
48
|
|
50
|
-
|
51
|
-
event = Events.build(logged_obj)
|
52
|
-
message = event ? event.message : logged_obj[:message]
|
49
|
+
message = logged_obj.delete(:message)
|
53
50
|
|
54
|
-
LogEntry.new(level, time, progname, message, context_snapshot,
|
51
|
+
LogEntry.new(level, time, progname, message, context_snapshot, logged_obj, tags: tags)
|
55
52
|
else
|
56
53
|
LogEntry.new(level, time, progname, logged_obj, context_snapshot, nil, tags: tags)
|
57
54
|
end
|
@@ -126,12 +123,6 @@ module Timber
|
|
126
123
|
end
|
127
124
|
end
|
128
125
|
|
129
|
-
# These are rails modules that change the logger behavior. We have to
|
130
|
-
# include these if they are present or the logger will not function properly
|
131
|
-
# in a rails environment.
|
132
|
-
include ::ActiveSupport::LoggerThreadSafeLevel if defined?(::ActiveSupport::LoggerThreadSafeLevel)
|
133
|
-
include ::LoggerSilence if defined?(::LoggerSilence)
|
134
|
-
|
135
126
|
# Creates a new Timber::Logger instance where the passed argument is an IO device. That is,
|
136
127
|
# anything that responds to `#write` and `#close`.
|
137
128
|
#
|
@@ -185,7 +176,7 @@ module Timber
|
|
185
176
|
elsif Config.instance.development? || Config.instance.test?
|
186
177
|
self.formatter = MessageOnlyFormatter.new
|
187
178
|
else
|
188
|
-
self.formatter =
|
179
|
+
self.formatter = JSONFormatter.new
|
189
180
|
end
|
190
181
|
|
191
182
|
self.level = environment_level
|
@@ -276,4 +267,4 @@ module Timber
|
|
276
267
|
obj.respond_to?(:debug) && obj.respond_to?(:info) && obj.respond_to?(:warn)
|
277
268
|
end
|
278
269
|
end
|
279
|
-
end
|
270
|
+
end
|