logstasher 1.2.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 73fe929a52a1421487fc60f7125a2aa16f9e0ed4
4
- data.tar.gz: a57221921ecff9275dce29953ae63b36d4ce3d19
2
+ SHA256:
3
+ metadata.gz: 5c4f5c6311cd4d8ae8883aaff46a0238f688f456af9f8774abd79619e5cbba28
4
+ data.tar.gz: 654cdd782f29136fa87517fff5468ce45bd4e861783d9d305b344943cfbbacc0
5
5
  SHA512:
6
- metadata.gz: b706c62313108c305c9b2c543e278c1bf4d485a2bed0f29bdb44d1ace6686d7988e522ba5e69b4036460d6ecf40b9d3cbbc6dd4d53a8d831e29904c07ed17ae0
7
- data.tar.gz: e5788207b057d4a21684f51fdc236028ed84448cb4a0114df16c2eb98d5de87faa7f3095138f5e9accd0d88585edbee6109f5b5ec8692650fc5d154adbff3bda
6
+ metadata.gz: efbf0988e5ba529f52bbe17f57088473099b8d9931f147f9c8c572dcd71f5e9a5154e4486ffe381186fb2c5514e9ed34324bb5b3552a4c5f5fbad5eab28b1064
7
+ data.tar.gz: 411ac7f12e116ac6a2b2a2f6db01d08c76df82553eaf8a29bcd93bd2fb94df62e6b0c11c7eabe7748a97a662900b33f5fa0860888d3c557e4c1fa76b6868650c
@@ -6,6 +6,7 @@ require 'logstasher/action_view/log_subscriber' if defined?(ActionView)
6
6
  require 'logstasher/active_job/log_subscriber' if defined?(ActiveJob)
7
7
  require 'logstasher/rails_ext/action_controller/base'
8
8
  require 'logstasher/custom_fields'
9
+ require 'logstasher/event'
9
10
  require 'request_store'
10
11
  require 'active_support/core_ext/module/attribute_accessors'
11
12
  require 'active_support/core_ext/string/inflections'
@@ -17,7 +18,8 @@ module LogStasher
17
18
  REQUEST_CONTEXT_KEY = :logstasher_request_context
18
19
 
19
20
  attr_accessor :logger, :logger_path, :enabled, :log_controller_parameters, :source, :backtrace,
20
- :controller_monkey_patch, :field_renaming
21
+ :controller_monkey_patch, :field_renaming
22
+
21
23
  # Setting the default to 'unknown' to define the default behaviour
22
24
  @source = 'unknown'
23
25
  # By default log the backtrace of exceptions
@@ -26,27 +28,25 @@ module LogStasher
26
28
  def remove_existing_log_subscriptions
27
29
  ::ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
28
30
  case subscriber.class.name
29
- when 'ActionView::LogSubscriber'
30
- unsubscribe(:action_view, subscriber)
31
- when 'ActionController::LogSubscriber'
32
- unsubscribe(:action_controller, subscriber)
33
- when 'ActionMailer::LogSubscriber'
34
- unsubscribe(:action_mailer, subscriber)
35
- when 'ActiveRecord::LogSubscriber'
36
- unsubscribe(:active_record, subscriber)
37
- when 'ActiveJob::Logging::LogSubscriber'
38
- unsubscribe(:active_job, subscriber)
31
+ when 'ActionView::LogSubscriber'
32
+ unsubscribe(:action_view, subscriber)
33
+ when 'ActionController::LogSubscriber'
34
+ unsubscribe(:action_controller, subscriber)
35
+ when 'ActionMailer::LogSubscriber'
36
+ unsubscribe(:action_mailer, subscriber)
37
+ when 'ActiveRecord::LogSubscriber'
38
+ unsubscribe(:active_record, subscriber)
39
+ when 'ActiveJob::Logging::LogSubscriber'
40
+ unsubscribe(:active_job, subscriber)
39
41
  end
40
42
  end
41
43
  end
42
44
 
43
45
  def unsubscribe(component, subscriber)
44
- events = subscriber.public_methods(false).reject{ |method| method.to_s == 'call' }
46
+ events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' }
45
47
  events.each do |event|
46
48
  ::ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
47
- if listener.instance_variable_get('@delegate') == subscriber
48
- ::ActiveSupport::Notifications.unsubscribe listener
49
- end
49
+ ::ActiveSupport::Notifications.unsubscribe listener if listener.instance_variable_get('@delegate') == subscriber
50
50
  end
51
51
  end
52
52
  end
@@ -56,14 +56,14 @@ module LogStasher
56
56
  payload[:route] = "#{request.params[:controller]}##{request.params[:action]}"
57
57
  payload[:request_id] = request.env['action_dispatch.request_id']
58
58
  LogStasher::CustomFields.add(:ip, :route, :request_id)
59
- if self.log_controller_parameters
59
+ if log_controller_parameters
60
60
  payload[:parameters] = payload[:params].except(*::ActionController::LogSubscriber::INTERNAL_PARAMS)
61
61
  LogStasher::CustomFields.add(:parameters)
62
62
  end
63
63
  end
64
64
 
65
65
  def add_custom_fields(&block)
66
- wrapped_block = Proc.new do |fields|
66
+ wrapped_block = proc do |fields|
67
67
  LogStasher::CustomFields.add(*LogStasher.store.keys)
68
68
  instance_exec(fields, &block)
69
69
  end
@@ -72,7 +72,7 @@ module LogStasher
72
72
  end
73
73
 
74
74
  def add_custom_fields_to_request_context(&block)
75
- wrapped_block = Proc.new do |fields|
75
+ wrapped_block = proc do |fields|
76
76
  instance_exec(fields, &block)
77
77
  LogStasher::CustomFields.add(*fields.keys)
78
78
  end
@@ -89,7 +89,6 @@ module LogStasher
89
89
  end
90
90
 
91
91
  def setup_before(config)
92
- require 'logstash-event'
93
92
  self.enabled = config.enabled
94
93
  LogStasher::ActiveSupport::LogSubscriber.attach_to :action_controller if config.controller_enabled
95
94
  LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer if config.mailer_enabled
@@ -100,27 +99,27 @@ module LogStasher
100
99
 
101
100
  def setup(config)
102
101
  # Path instrumentation class to insert our hook
103
- if (! config.controller_monkey_patch && config.controller_monkey_patch != false) || config.controller_monkey_patch == true
102
+ if (!config.controller_monkey_patch && config.controller_monkey_patch != false) || config.controller_monkey_patch == true
104
103
  require 'logstasher/rails_ext/action_controller/metal/instrumentation'
105
104
  end
106
- self.suppress_app_logs(config)
105
+ suppress_app_logs(config)
107
106
  self.logger_path = config.logger_path || "#{Rails.root}/log/logstash_#{Rails.env}.log"
108
- self.logger = config.logger || new_logger(self.logger_path)
109
- self.logger.level = config.log_level || Logger::WARN
107
+ self.logger = config.logger || new_logger(logger_path)
108
+ logger.level = config.log_level || Logger::WARN
110
109
  self.source = config.source unless config.source.nil?
111
- self.log_controller_parameters = !! config.log_controller_parameters
112
- self.backtrace = !! config.backtrace unless config.backtrace.nil?
113
- self.set_data_for_rake
114
- self.set_data_for_console
110
+ self.log_controller_parameters = !!config.log_controller_parameters
111
+ self.backtrace = !!config.backtrace unless config.backtrace.nil?
112
+ set_data_for_rake
113
+ set_data_for_console
115
114
  self.field_renaming = Hash(config.field_renaming)
116
115
  end
117
116
 
118
117
  def set_data_for_rake
119
- self.request_context['request_id'] = ::Rake.application.top_level_tasks if self.called_as_rake?
118
+ request_context['request_id'] = ::Rake.application.top_level_tasks if called_as_rake?
120
119
  end
121
120
 
122
121
  def set_data_for_console
123
- self.request_context['request_id'] = "#{Process.pid}" if self.called_as_console?
122
+ request_context['request_id'] = Process.pid.to_s if called_as_console?
124
123
  end
125
124
 
126
125
  def called_as_rake?
@@ -132,7 +131,7 @@ module LogStasher
132
131
  end
133
132
 
134
133
  def has_active_job?
135
- Rails::VERSION::MAJOR > 4 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR >= 2)
134
+ defined?(ActiveJob)
136
135
  end
137
136
 
138
137
  def suppress_app_logs(config)
@@ -158,10 +157,10 @@ module LogStasher
158
157
  # LogStasher.info("message", tags:["tag1", "tag2"])
159
158
  # LogStasher.info("message", timing:1234)
160
159
  # LogStasher.info(custom1:"yes", custom2:"no")
161
- def log(severity, message, additional_fields={})
162
- if self.logger && self.logger.send("#{severity}?")
160
+ def log(severity, message, additional_fields = {})
161
+ if logger && logger.send("#{severity}?")
163
162
 
164
- data = {'level' => severity}
163
+ data = { 'level' => severity }
165
164
  if message.respond_to?(:to_hash)
166
165
  data.merge!(message.to_hash)
167
166
  else
@@ -172,16 +171,16 @@ module LogStasher
172
171
  tags = Array(additional_fields.delete(:tags) || 'log')
173
172
 
174
173
  data.merge!(additional_fields)
175
- self.logger << build_logstash_event(data, tags).to_json + "\n"
174
+ logger << build_logstash_event(data, tags).to_json + "\n"
176
175
 
177
176
  end
178
177
  end
179
178
 
180
179
  def build_logstash_event(data, tags)
181
180
  field_renaming.each do |old_name, new_name|
182
- data[new_name] = data.delete(old_name) if data.key?(old_name)
181
+ data[new_name] = data.delete(old_name) if data.key?(old_name)
183
182
  end
184
- ::LogStash::Event.new(data.merge('source' => self.source, 'tags' => tags))
183
+ Event.new(data.merge('source' => source, 'tags' => tags))
185
184
  end
186
185
 
187
186
  def store
@@ -204,7 +203,7 @@ module LogStasher
204
203
  end
205
204
  end
206
205
 
207
- %w( fatal error warn info debug unknown ).each do |severity|
206
+ %w[fatal error warn info debug unknown].each do |severity|
208
207
  eval <<-EOM, nil, __FILE__, __LINE__ + 1
209
208
  def #{severity}(message=nil, additional_fields={})
210
209
  self.log(:#{severity}, message, additional_fields)
@@ -213,7 +212,7 @@ module LogStasher
213
212
  end
214
213
 
215
214
  def enabled?
216
- self.enabled || false
215
+ enabled || false
217
216
  end
218
217
 
219
218
  private
@@ -10,8 +10,8 @@ module LogStasher
10
10
  def render_template(event)
11
11
  logstash_event(event)
12
12
  end
13
- alias :render_partial :render_template
14
- alias :render_collection :render_template
13
+ alias render_partial render_template
14
+ alias render_collection render_template
15
15
 
16
16
  def logger
17
17
  LogStasher.logger
@@ -35,7 +35,7 @@ module LogStasher
35
35
  end
36
36
 
37
37
  def extract_data(data)
38
- { identifier: from_rails_root(data[:identifier]) }
38
+ { identifier: from_rails_root(data[:identifier]) }
39
39
  end
40
40
 
41
41
  def request_context
@@ -49,16 +49,15 @@ module LogStasher
49
49
  def event_data(event)
50
50
  {
51
51
  name: event.name,
52
- transaction_id: event.transaction_id,
52
+ transaction_id: event.transaction_id
53
53
  }
54
54
  end
55
55
 
56
56
  def runtimes(event)
57
57
  {
58
- duration: event.duration,
59
- }.inject({}) do |runtimes, (name, runtime)|
58
+ duration: event.duration
59
+ }.each_with_object({}) do |(name, runtime), runtimes|
60
60
  runtimes[name] = runtime.to_f.round(2) if runtime
61
- runtimes
62
61
  end
63
62
  end
64
63
  end
@@ -4,89 +4,89 @@ begin
4
4
  rescue LoadError
5
5
  end
6
6
 
7
- module LogStasher
8
- module ActiveJob
9
- class LogSubscriber < ::ActiveJob::Logging::LogSubscriber
10
-
11
- def enqueue(event)
12
- process_event(event, 'enqueue')
13
- end
14
-
15
- def enqueue_at(event)
16
- process_event(event, 'enqueue_at')
17
- end
18
-
19
- def perform(event)
20
- process_event(event, 'perform')
21
-
22
- # Revert the request id back, in the event that the inline adapter is being used or a
23
- # perform_now was used.
24
- LogStasher.request_context[:request_id] = Thread.current[:old_request_id]
25
- Thread.current[:old_request_id] = nil
26
- end
27
-
28
- def perform_start(event)
29
- # Use the job_id as the request id, so that any custom logging done for a job
30
- # shares a request id, and has the job id in each log line.
31
- #
32
- # It's not being set when the job is enqueued, so enqueuing a job will have it's default
33
- # request_id. In a lot of cases, it will be because of a web request.
34
- #
35
- # Hang onto the old request id, so we can revert after the job is done being performed.
36
- Thread.current[:old_request_id] = LogStasher.request_context[:request_id]
37
- LogStasher.request_context[:request_id] = event.payload[:job].job_id
38
-
39
- process_event(event, 'perform_start')
40
- end
41
-
42
- def logger
43
- LogStasher.logger
44
- end
45
-
46
- private
47
-
48
- def process_event(event, type)
49
- data = extract_metadata(event)
50
- data.merge! extract_exception(event)
51
- data.merge! extract_scheduled_at(event) if type == 'enqueue_at'
52
- data.merge! extract_duration(event) if type == 'perform'
53
- data.merge! request_context
54
-
55
- tags = ['job', type]
56
- tags.push('exception') if data[:exception]
57
- logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
7
+ if defined?(::ActiveJob::Logging::LogSubscriber)
8
+ module LogStasher
9
+ module ActiveJob
10
+ class LogSubscriber < ::ActiveJob::Logging::LogSubscriber
11
+ def enqueue(event)
12
+ process_event(event, 'enqueue')
13
+ end
14
+
15
+ def enqueue_at(event)
16
+ process_event(event, 'enqueue_at')
17
+ end
18
+
19
+ def perform(event)
20
+ process_event(event, 'perform')
21
+
22
+ # Revert the request id back, in the event that the inline adapter is being used or a
23
+ # perform_now was used.
24
+ LogStasher.request_context[:request_id] = Thread.current[:old_request_id]
25
+ Thread.current[:old_request_id] = nil
26
+ end
27
+
28
+ def perform_start(event)
29
+ # Use the job_id as the request id, so that any custom logging done for a job
30
+ # shares a request id, and has the job id in each log line.
31
+ #
32
+ # It's not being set when the job is enqueued, so enqueuing a job will have it's default
33
+ # request_id. In a lot of cases, it will be because of a web request.
34
+ #
35
+ # Hang onto the old request id, so we can revert after the job is done being performed.
36
+ Thread.current[:old_request_id] = LogStasher.request_context[:request_id]
37
+ LogStasher.request_context[:request_id] = event.payload[:job].job_id
38
+
39
+ process_event(event, 'perform_start')
40
+ end
41
+
42
+ def logger
43
+ LogStasher.logger
44
+ end
45
+
46
+ private
47
+
48
+ def process_event(event, type)
49
+ data = extract_metadata(event)
50
+ data.merge! extract_exception(event)
51
+ data.merge! extract_scheduled_at(event) if type == 'enqueue_at'
52
+ data.merge! extract_duration(event) if type == 'perform'
53
+ data.merge! request_context
54
+
55
+ tags = ['job', type]
56
+ tags.push('exception') if data[:exception]
57
+ logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
58
+ end
59
+
60
+ def extract_metadata(event)
61
+ {
62
+ job_id: event.payload[:job].job_id,
63
+ queue_name: queue_name(event),
64
+ job_class: event.payload[:job].class.to_s,
65
+ job_args: args_info(event.payload[:job])
66
+ }
67
+ end
68
+
69
+ def extract_duration(event)
70
+ { duration: event.duration.to_f.round(2) }
71
+ end
72
+
73
+ def extract_exception(event)
74
+ event.payload.slice(:exception)
75
+ end
76
+
77
+ def extract_scheduled_at(event)
78
+ { scheduled_at: scheduled_at(event) }
79
+ end
80
+
81
+ def request_context
82
+ LogStasher.request_context
83
+ end
84
+
85
+ # The default args_info makes a string. We need objects to turn into JSON.
86
+ def args_info(job)
87
+ job.arguments.map { |arg| arg.try(:to_global_id).try(:to_s) || arg }
88
+ end
58
89
  end
59
-
60
- def extract_metadata(event)
61
- {
62
- :job_id => event.payload[:job].job_id,
63
- :queue_name => queue_name(event),
64
- :job_class => event.payload[:job].class.to_s,
65
- :job_args => args_info(event.payload[:job]),
66
- }
67
- end
68
-
69
- def extract_duration(event)
70
- { :duration => event.duration.to_f.round(2) }
71
- end
72
-
73
- def extract_exception(event)
74
- event.payload.slice(:exception)
75
- end
76
-
77
- def extract_scheduled_at(event)
78
- { :scheduled_at => scheduled_at(event) }
79
- end
80
-
81
- def request_context
82
- LogStasher.request_context
83
- end
84
-
85
- # The default args_info makes a string. We need objects to turn into JSON.
86
- def args_info(job)
87
- job.arguments.map { |arg| arg.try(:to_global_id).try(:to_s) || arg }
88
- end
89
-
90
90
  end
91
91
  end
92
- end if defined?(::ActiveJob::Logging::LogSubscriber)
92
+ end
@@ -9,11 +9,9 @@ module LogStasher
9
9
 
10
10
  def identity(event)
11
11
  lsevent = logstash_event(event)
12
- if logger && lsevent
13
- logger << lsevent.to_json + "\n"
14
- end
12
+ logger << lsevent.to_json + "\n" if logger && lsevent
15
13
  end
16
- alias :sql :identity
14
+ alias sql identity
17
15
 
18
16
  def logger
19
17
  LogStasher.logger
@@ -22,6 +20,7 @@ module LogStasher
22
20
  private
23
21
 
24
22
  def logstash_event(event)
23
+ self.class.runtime += event.duration
25
24
  data = event.payload
26
25
 
27
26
  return if 'SCHEMA' == data[:name]
@@ -32,7 +31,7 @@ module LogStasher
32
31
  data.merge! LogStasher.store
33
32
  data.merge! extract_custom_fields(data)
34
33
 
35
- tags = [ 'request' ]
34
+ tags = ['request']
36
35
  tags.push('exception') if data[:exception]
37
36
  LogStasher.build_logstash_event(data, tags)
38
37
  end
@@ -45,7 +44,7 @@ module LogStasher
45
44
  if event.duration
46
45
  { duration: event.duration.to_f.round(2) }
47
46
  else
48
- { }
47
+ {}
49
48
  end
50
49
  end
51
50
 
@@ -44,7 +44,7 @@ module LogStasher
44
44
  end
45
45
 
46
46
  def extract_path(payload)
47
- payload[:path].split("?").first
47
+ payload[:path].split('?').first
48
48
  end
49
49
 
50
50
  def extract_format(payload)
@@ -57,27 +57,26 @@ module LogStasher
57
57
 
58
58
  def extract_status(payload)
59
59
  if payload[:status]
60
- { :status => payload[:status].to_i }
60
+ { status: payload[:status].to_i }
61
61
  else
62
- { :status => 0 }
62
+ { status: 0 }
63
63
  end
64
64
  end
65
65
 
66
66
  def runtimes(event)
67
67
  {
68
- :duration => event.duration,
69
- :view => event.payload[:view_runtime],
70
- :db => event.payload[:db_runtime]
71
- }.inject({}) do |runtimes, (name, runtime)|
68
+ duration: event.duration,
69
+ view: event.payload[:view_runtime],
70
+ db: event.payload[:db_runtime]
71
+ }.each_with_object({}) do |(name, runtime), runtimes|
72
72
  runtimes[name] = runtime.to_f.round(2) if runtime
73
- runtimes
74
73
  end
75
74
  end
76
75
 
77
- def location(event)
76
+ def location(_event)
78
77
  if location = Thread.current[:logstasher_location]
79
78
  Thread.current[:logstasher_location] = nil
80
- { :location => location }
79
+ { location: location }
81
80
  else
82
81
  {}
83
82
  end
@@ -88,13 +87,13 @@ module LogStasher
88
87
  if payload[:exception]
89
88
  exception, message = payload[:exception]
90
89
  status = ::ActionDispatch::ExceptionWrapper.status_code_for_exception(exception)
91
- if LogStasher.backtrace
92
- backtrace = $!.backtrace.join("\n")
93
- else
94
- backtrace = $!.backtrace.first
95
- end
90
+ backtrace = if LogStasher.backtrace
91
+ $!.backtrace.join("\n")
92
+ else
93
+ $!.backtrace.first
94
+ end
96
95
  message = "#{exception}\n#{message}\n#{backtrace}"
97
- { :status => status, :error => message }
96
+ { status: status, error: message }
98
97
  else
99
98
  {}
100
99
  end
@@ -4,18 +4,18 @@ require 'active_support/log_subscriber'
4
4
  module LogStasher
5
5
  module ActiveSupport
6
6
  class MailerLogSubscriber < ::ActiveSupport::LogSubscriber
7
- MAILER_FIELDS = [:mailer, :action, :message_id, :from, :to]
7
+ MAILER_FIELDS = %i[mailer action message_id from to].freeze
8
8
 
9
9
  def deliver(event)
10
- process_event(event, ['mailer', 'deliver'])
10
+ process_event(event, %w[mailer deliver])
11
11
  end
12
12
 
13
13
  def receive(event)
14
- process_event(event, ['mailer', 'receive'])
14
+ process_event(event, %w[mailer receive])
15
15
  end
16
16
 
17
17
  def process(event)
18
- process_event(event, ['mailer', 'process'])
18
+ process_event(event, %w[mailer process])
19
19
  end
20
20
 
21
21
  def logger
@@ -5,7 +5,7 @@ module LogStasher
5
5
  (!CustomFields.custom_fields.empty? && data.extract!(*CustomFields.custom_fields)) || {}
6
6
  end
7
7
  end
8
-
8
+
9
9
  def self.clear
10
10
  Thread.current[:logstasher_custom_fields] = []
11
11
  end
@@ -3,7 +3,6 @@ require 'redis'
3
3
  module LogStasher
4
4
  module Device
5
5
  class Redis
6
-
7
6
  attr_reader :options, :redis
8
7
 
9
8
  def initialize(options = {})
@@ -23,7 +22,7 @@ module LogStasher
23
22
  def redis_options
24
23
  unless @redis_options
25
24
  default_keys = default_options.keys
26
- @redis_options = options.select { |k, v| !default_keys.include?(k) }
25
+ @redis_options = options.select { |k, _v| !default_keys.include?(k) }
27
26
  end
28
27
 
29
28
  @redis_options
@@ -36,7 +35,7 @@ module LogStasher
36
35
  when 'channel'
37
36
  redis.publish(key, log)
38
37
  else
39
- fail "Unknown data type #{data_type}"
38
+ raise "Unknown data type #{data_type}"
40
39
  end
41
40
  end
42
41
 
@@ -51,12 +50,12 @@ module LogStasher
51
50
  end
52
51
 
53
52
  def default_options
54
- { key: 'logstash', data_type: 'list' }
53
+ { key: 'logstash', data_type: 'list' }
55
54
  end
56
55
 
57
56
  def validate_options
58
- unless ['list', 'channel'].include?(options[:data_type])
59
- fail 'Expected :data_type to be either "list" or "channel"'
57
+ unless %w[list channel].include?(options[:data_type])
58
+ raise 'Expected :data_type to be either "list" or "channel"'
60
59
  end
61
60
  end
62
61
  end
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'time'
3
+ require 'date'
4
+
5
+ module LogStasher
6
+ class Event
7
+ def initialize(data = {})
8
+ @cancelled = false
9
+
10
+ @data = data
11
+ if data.include?('@timestamp')
12
+ t = data['@timestamp']
13
+ data['@timestamp'] = Time.parse(t).gmtime.iso8601(3) if t.is_a?(String)
14
+ else
15
+ data['@timestamp'] = ::Time.now.utc.iso8601(3)
16
+ end
17
+ data['@version'] = '1' unless @data.include?('@version')
18
+ end
19
+
20
+ def to_s
21
+ to_json.to_s
22
+ end
23
+
24
+ def to_json(*args)
25
+ @data.to_json(*args)
26
+ end
27
+
28
+ def [](key)
29
+ @data[key]
30
+ end
31
+ end
32
+ end
@@ -9,17 +9,17 @@ module LogStasher
9
9
  super(*args)
10
10
  LogStasher::CustomFields.clear
11
11
  end
12
-
12
+
13
13
  private
14
14
 
15
15
  # this method is called from within super of process_action.
16
16
  def append_info_to_payload(payload) #:nodoc:
17
17
  LogStasher.add_default_fields_to_payload(payload, request)
18
- if self.respond_to?(:logstasher_add_custom_fields_to_request_context)
18
+ if respond_to?(:logstasher_add_custom_fields_to_request_context)
19
19
  logstasher_add_custom_fields_to_request_context(LogStasher.request_context)
20
20
  end
21
21
 
22
- if self.respond_to?(:logstasher_add_custom_fields_to_payload)
22
+ if respond_to?(:logstasher_add_custom_fields_to_payload)
23
23
  before_keys = payload.keys.clone
24
24
  logstasher_add_custom_fields_to_payload(payload)
25
25
  after_keys = payload.keys
@@ -38,7 +38,7 @@ module LogStasher
38
38
  payload[key] = value
39
39
  end
40
40
  end
41
-
41
+
42
42
  def add_custom_fields_to_store
43
43
  LogStasher.store[:ip] = request.remote_ip
44
44
  LogStasher.store[:route] = "#{request.params[:controller]}##{request.params[:action]}"
@@ -1,14 +1,18 @@
1
1
  module ActionController
2
2
  module Instrumentation
3
- alias :orig_process_action :process_action
3
+ alias orig_process_action process_action
4
4
  def process_action(*args)
5
5
  raw_payload = {
6
- :controller => self.class.name,
7
- :action => self.action_name,
8
- :params => request.filtered_parameters,
9
- :format => request.format.try(:ref),
10
- :method => request.method,
11
- :path => (request.fullpath rescue "unknown")
6
+ controller: self.class.name,
7
+ action: action_name,
8
+ params: request.filtered_parameters,
9
+ format: request.format.try(:ref),
10
+ method: request.method,
11
+ path: begin
12
+ request.fullpath
13
+ rescue StandardError
14
+ 'unknown'
15
+ end
12
16
  }
13
17
 
14
18
  LogStasher.add_default_fields_to_payload(raw_payload, request)
@@ -16,14 +20,14 @@ module ActionController
16
20
  LogStasher.clear_request_context
17
21
  LogStasher.add_default_fields_to_request_context(request)
18
22
 
19
- ActiveSupport::Notifications.instrument("start_processing.action_controller", raw_payload.dup)
23
+ ActiveSupport::Notifications.instrument('start_processing.action_controller', raw_payload.dup)
20
24
 
21
- ActiveSupport::Notifications.instrument("process_action.action_controller", raw_payload) do |payload|
22
- if self.respond_to?(:logstasher_add_custom_fields_to_request_context)
25
+ ActiveSupport::Notifications.instrument('process_action.action_controller', raw_payload) do |payload|
26
+ if respond_to?(:logstasher_add_custom_fields_to_request_context)
23
27
  logstasher_add_custom_fields_to_request_context(LogStasher.request_context)
24
28
  end
25
29
 
26
- if self.respond_to?(:logstasher_add_custom_fields_to_payload)
30
+ if respond_to?(:logstasher_add_custom_fields_to_payload)
27
31
  before_keys = raw_payload.keys.clone
28
32
  logstasher_add_custom_fields_to_payload(raw_payload)
29
33
  after_keys = raw_payload.keys
@@ -45,6 +49,6 @@ module ActionController
45
49
  result
46
50
  end
47
51
  end
48
- alias :logstasher_process_action :process_action
52
+ alias logstasher_process_action process_action
49
53
  end
50
54
  end
@@ -17,8 +17,7 @@ module Rails
17
17
  end
18
18
 
19
19
  # Overwrites Rails 3.0/3.1 code that logs new requests
20
- def before_dispatch(env)
21
- end
20
+ def before_dispatch(env); end
22
21
  end
23
22
  end
24
23
  end
@@ -17,12 +17,12 @@ module LogStasher
17
17
 
18
18
  # Try loading the config/logstasher.yml if present
19
19
  env = Rails.env.to_sym || :development
20
- config_file = File.expand_path "./config/logstasher.yml"
20
+ config_file = File.expand_path './config/logstasher.yml'
21
21
 
22
22
  # Load and ERB templating of YAML files
23
- LOGSTASHER = File.exists?(config_file) ? YAML.load(ERB.new(File.read(config_file)).result).symbolize_keys : nil
23
+ LOGSTASHER = File.exist?(config_file) ? YAML.load(ERB.new(File.read(config_file)).result).symbolize_keys : nil
24
24
 
25
- initializer :logstasher, :before => :load_config_initializers do |app|
25
+ initializer :logstasher, before: :load_config_initializers do |app|
26
26
  if LOGSTASHER.present?
27
27
  # process common configs
28
28
  LogStasher.process_config(app.config.logstasher, LOGSTASHER)
@@ -30,7 +30,8 @@ module LogStasher
30
30
  LogStasher.process_config(app.config.logstasher, LOGSTASHER[env].symbolize_keys) if LOGSTASHER.key? env
31
31
  end
32
32
 
33
- app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
33
+ app.config.action_dispatch.rack_cache[:verbose] = false if rack_cache_hashlike?(app)
34
+
34
35
  LogStasher.setup_before(app.config.logstasher) if app.config.logstasher.enabled
35
36
  end
36
37
 
@@ -39,6 +40,10 @@ module LogStasher
39
40
  LogStasher.setup(config.logstasher) if config.logstasher.enabled
40
41
  end
41
42
  end
43
+
44
+ def rack_cache_hashlike?(app)
45
+ app.config.action_dispatch.rack_cache && app.config.action_dispatch.rack_cache.respond_to?(:[]=)
46
+ end
42
47
  end
43
48
 
44
49
  def default_source
@@ -74,5 +79,8 @@ module LogStasher
74
79
  config.backtrace = yml_config[:backtrace] if yml_config.key? :backtrace
75
80
  config.logger_path = yml_config[:logger_path] if yml_config.key? :logger_path
76
81
  config.log_level = yml_config[:log_level] if yml_config.key? :log_level
82
+ if yml_config.key? :log_controller_parameters
83
+ config.log_controller_parameters = yml_config[:log_controller_parameters]
84
+ end
77
85
  end
78
86
  end
@@ -1,3 +1,3 @@
1
1
  module LogStasher
2
- VERSION = "1.2.1"
2
+ VERSION = '2.0.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstasher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shadab Ahmed
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-06 00:00:00.000000000 Z
11
+ date: 2020-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: logstash-event
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.2.0
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.2.0
26
+ version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: request_store
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,33 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activesupport
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '4.0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '4.0'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
42
+ name: activerecord
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - ">="
60
46
  - !ruby/object:Gem::Version
61
- version: '2.14'
47
+ version: '5.0'
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - ">="
67
53
  - !ruby/object:Gem::Version
68
- version: '2.14'
54
+ version: '5.0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: bundler
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -86,28 +72,28 @@ dependencies:
86
72
  requirements:
87
73
  - - ">="
88
74
  - !ruby/object:Gem::Version
89
- version: '4.0'
75
+ version: '5.0'
90
76
  type: :development
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
80
  - - ">="
95
81
  - !ruby/object:Gem::Version
96
- version: '4.0'
82
+ version: '5.0'
97
83
  - !ruby/object:Gem::Dependency
98
- name: activerecord
84
+ name: rspec
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
87
  - - ">="
102
88
  - !ruby/object:Gem::Version
103
- version: '4.0'
89
+ version: '2.14'
104
90
  type: :development
105
91
  prerelease: false
106
92
  version_requirements: !ruby/object:Gem::Requirement
107
93
  requirements:
108
94
  - - ">="
109
95
  - !ruby/object:Gem::Version
110
- version: '4.0'
96
+ version: '2.14'
111
97
  description: Awesome rails logs
112
98
  email:
113
99
  - shadab.ansari@gmail.com
@@ -123,6 +109,7 @@ files:
123
109
  - lib/logstasher/active_support/mailer_log_subscriber.rb
124
110
  - lib/logstasher/custom_fields.rb
125
111
  - lib/logstasher/device/redis.rb
112
+ - lib/logstasher/event.rb
126
113
  - lib/logstasher/rails_ext/action_controller/base.rb
127
114
  - lib/logstasher/rails_ext/action_controller/metal/instrumentation.rb
128
115
  - lib/logstasher/rails_ext/rack/logger.rb
@@ -132,7 +119,7 @@ homepage: https://github.com/shadabahmed/logstasher
132
119
  licenses:
133
120
  - MIT
134
121
  metadata: {}
135
- post_install_message:
122
+ post_install_message:
136
123
  rdoc_options: []
137
124
  require_paths:
138
125
  - lib
@@ -147,9 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
134
  - !ruby/object:Gem::Version
148
135
  version: '0'
149
136
  requirements: []
150
- rubyforge_project:
151
- rubygems_version: 2.5.1
152
- signing_key:
137
+ rubygems_version: 3.0.3
138
+ signing_key:
153
139
  specification_version: 4
154
140
  summary: Awesome rails logs
155
141
  test_files: []