Module: LogStasher
- Extended by:
- LogStasher
- Included in:
- LogStasher
- Defined in:
- lib/logstasher.rb,
lib/logstasher/railtie.rb,
lib/logstasher/version.rb,
lib/logstasher/device/redis.rb,
lib/logstasher/action_view/log_subscriber.rb,
lib/logstasher/active_record/log_subscriber.rb,
lib/logstasher/active_support/log_subscriber.rb,
lib/logstasher/rails_ext/action_controller/base.rb,
lib/logstasher/active_support/mailer_log_subscriber.rb
Defined Under Namespace
Modules: ActionController, ActionView, ActiveRecord, ActiveSupport, Device
Classes: Railtie
Constant Summary
collapse
- STORE_KEY =
:logstasher_data
- REQUEST_CONTEXT_KEY =
:logstasher_request_context
- VERSION =
"0.8.6"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
17
18
19
|
# File 'lib/logstasher.rb', line 17
def backtrace
@backtrace
end
|
#controller_monkey_patch ⇒ Object
Returns the value of attribute controller_monkey_patch.
17
18
19
|
# File 'lib/logstasher.rb', line 17
def controller_monkey_patch
@controller_monkey_patch
end
|
#enabled ⇒ Object
Returns the value of attribute enabled.
17
18
19
|
# File 'lib/logstasher.rb', line 17
def enabled
@enabled
end
|
#log_controller_parameters ⇒ Object
Returns the value of attribute log_controller_parameters.
17
18
19
|
# File 'lib/logstasher.rb', line 17
def log_controller_parameters
@log_controller_parameters
end
|
#logger ⇒ Object
Returns the value of attribute logger.
17
18
19
|
# File 'lib/logstasher.rb', line 17
def logger
@logger
end
|
#logger_path ⇒ Object
Returns the value of attribute logger_path.
17
18
19
|
# File 'lib/logstasher.rb', line 17
def logger_path
@logger_path
end
|
#source ⇒ Object
Returns the value of attribute source.
17
18
19
|
# File 'lib/logstasher.rb', line 17
def source
@source
end
|
Instance Method Details
#add_custom_fields(&block) ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/logstasher.rb', line 61
def add_custom_fields(&block)
wrapped_block = Proc.new do |fields|
LogStasher.custom_fields.concat(LogStasher.store.keys)
instance_exec(fields, &block)
end
::ActionController::Metal.send(:define_method, :logtasher_add_custom_fields_to_payload, &wrapped_block)
::ActionController::Base.send(:define_method, :logtasher_add_custom_fields_to_payload, &wrapped_block)
end
|
#add_custom_fields_to_request_context(&block) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/logstasher.rb', line 70
def add_custom_fields_to_request_context(&block)
wrapped_block = Proc.new do |fields|
instance_exec(fields, &block)
LogStasher.custom_fields.concat(fields.keys)
end
::ActionController::Metal.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block)
::ActionController::Base.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block)
end
|
#add_default_fields_to_payload(payload, request) ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/logstasher.rb', line 50
def add_default_fields_to_payload(payload, request)
payload[:ip] = request.remote_ip
payload[:route] = "#{request.params[:controller]}##{request.params[:action]}"
payload[:request_id] = request.env['action_dispatch.request_id']
self.custom_fields += [:ip, :route, :request_id]
if self.log_controller_parameters
payload[:parameters] = payload[:params].except(*::ActionController::LogSubscriber::INTERNAL_PARAMS)
self.custom_fields += [:parameters]
end
end
|
#add_default_fields_to_request_context(request) ⇒ Object
79
80
81
|
# File 'lib/logstasher.rb', line 79
def add_default_fields_to_request_context(request)
request_context[:request_id] = request.env['action_dispatch.request_id']
end
|
#build_logstash_event(data, tags) ⇒ Object
178
179
180
|
# File 'lib/logstasher.rb', line 178
def build_logstash_event(data, tags)
::LogStash::Event.new(data.merge('source' => self.source, 'tags' => tags))
end
|
#called_as_console? ⇒ Boolean
124
125
126
|
# File 'lib/logstasher.rb', line 124
def called_as_console?
defined?(Rails::Console) && true || false
end
|
#called_as_rake? ⇒ Boolean
120
121
122
|
# File 'lib/logstasher.rb', line 120
def called_as_rake?
File.basename($0) == 'rake'
end
|
#clear_request_context ⇒ Object
83
84
85
|
# File 'lib/logstasher.rb', line 83
def clear_request_context
request_context.clear
end
|
135
136
137
138
|
# File 'lib/logstasher.rb', line 135
def configured_to_suppress_app_logs?(config)
!!(config.suppress_app_log.nil? ? config.supress_app_log : config.suppress_app_log)
end
|
#custom_fields ⇒ Object
140
141
142
|
# File 'lib/logstasher.rb', line 140
def custom_fields
Thread.current[:logstasher_custom_fields] ||= []
end
|
#custom_fields=(val) ⇒ Object
144
145
146
|
# File 'lib/logstasher.rb', line 144
def custom_fields=(val)
Thread.current[:logstasher_custom_fields] = val
end
|
#enabled? ⇒ Boolean
210
211
212
|
# File 'lib/logstasher.rb', line 210
def enabled?
self.enabled || false
end
|
#log(severity, message, additional_fields = {}) ⇒ Object
Log an arbitrary message.
Usually invoked by the level-based wrapper methods defined below.
Examples
LogStasher.info("message")
LogStasher.info("message", tags:"tag1")
LogStasher.info("message", tags:["tag1", "tag2"])
LogStasher.info("message", timing:1234)
LogStasher.info(custom1:"yes", custom2:"no")
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/logstasher.rb', line 159
def log(severity, message, additional_fields={})
if self.logger && self.logger.send("#{severity}?")
data = {'level' => severity}
if message.respond_to?(:to_hash)
data.merge!(message.to_hash)
else
data['message'] = message
end
tags = Array(additional_fields.delete(:tags) || 'log')
data.merge!(additional_fields)
self.logger << build_logstash_event(data, tags).to_json + "\n"
end
end
|
#remove_existing_log_subscriptions ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/logstasher.rb', line 24
def remove_existing_log_subscriptions
::ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
case subscriber.class.name
when 'ActionView::LogSubscriber'
unsubscribe(:action_view, subscriber)
when 'ActionController::LogSubscriber'
unsubscribe(:action_controller, subscriber)
when 'ActionMailer::LogSubscriber'
unsubscribe(:action_mailer, subscriber)
when 'ActiveRecord::LogSubscriber'
unsubscribe(:active_record, subscriber)
end
end
end
|
#request_context ⇒ Object
190
191
192
|
# File 'lib/logstasher.rb', line 190
def request_context
RequestStore.store[REQUEST_CONTEXT_KEY] ||= {}
end
|
#set_data_for_console ⇒ Object
116
117
118
|
# File 'lib/logstasher.rb', line 116
def set_data_for_console
self.request_context['request_id'] = "#{Process.pid}" if self.called_as_console?
end
|
#set_data_for_rake ⇒ Object
112
113
114
|
# File 'lib/logstasher.rb', line 112
def set_data_for_rake
self.request_context['request_id'] = ::Rake.application.top_level_tasks if self.called_as_rake?
end
|
#setup(config) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/logstasher.rb', line 96
def setup(config)
if (! config.controller_monkey_patch && config.controller_monkey_patch != false) || config.controller_monkey_patch == true
require 'logstasher/rails_ext/action_controller/metal/instrumentation'
end
self.suppress_app_logs(config)
self.logger_path = config.logger_path || "#{Rails.root}/log/logstash_#{Rails.env}.log"
self.logger = config.logger || new_logger(self.logger_path)
self.logger.level = config.log_level || Logger::WARN
self.source = config.source unless config.source.nil?
self.log_controller_parameters = !! config.log_controller_parameters
self.backtrace = !! config.backtrace unless config.backtrace.nil?
self.set_data_for_rake
self.set_data_for_console
end
|
#setup_before(config) ⇒ Object
#store ⇒ Object
182
183
184
185
186
187
188
|
# File 'lib/logstasher.rb', line 182
def store
if RequestStore.store[STORE_KEY].nil?
RequestStore.store[STORE_KEY] = Hash.new { |hash, key| hash[key] = {} }
end
RequestStore.store[STORE_KEY]
end
|
#suppress_app_logs(config) ⇒ Object
128
129
130
131
132
133
|
# File 'lib/logstasher.rb', line 128
def suppress_app_logs(config)
if configured_to_suppress_app_logs?(config)
require 'logstasher/rails_ext/rack/logger'
LogStasher.remove_existing_log_subscriptions
end
end
|
#unsubscribe(component, subscriber) ⇒ Object
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/logstasher.rb', line 39
def unsubscribe(component, subscriber)
events = subscriber.public_methods(false).reject{ |method| method.to_s == 'call' }
events.each do |event|
::ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
if listener.instance_variable_get('@delegate') == subscriber
::ActiveSupport::Notifications.unsubscribe listener
end
end
end
end
|
#watch(event, opts = {}, &block) ⇒ Object
194
195
196
197
198
199
200
|
# File 'lib/logstasher.rb', line 194
def watch(event, opts = {}, &block)
event_group = opts[:event_group] || event
::ActiveSupport::Notifications.subscribe(event) do |*args|
block.call(*args, store[event_group])
end
end
|