logstasher 0.4.9 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjQ0MTI1YmFiNDhhZWU4ODczZmQ2MzUwOTJlYzJlY2Q5NmE0MDFjMQ==
4
+ ZGM0Y2IzY2FhZmU1OGI4ZGY3NmRhMWI4ZjBlMjY0ZGI4YTdjNDYzOA==
5
5
  data.tar.gz: !binary |-
6
- MGU5ZGIwYWRhYTgwZDUxMDcxNTg1OGNlM2EzMDRkNjA4Yzk4ZDVhNA==
6
+ Y2NjYTljN2IwMGJhYjYwYjQ0ZDExMWU5OWYzNDcyZTRkOWI5YjViOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzY2ZGU0NWQxNjk3YjExNjY2YWZlMjQ0MDJhNjBiMjg4YWE3NDUwOTIxZDc2
10
- OTkzNzk1ZTU0ZDM5MTM1YTE0ZDgyY2MxODZhNTFiNjJlZjIxYjQ3ZTFkYjUy
11
- ZmFmYTBhNTc3MDIxZjkwYTk5NDMxZjIwNzdiNTZhOGQyOTE4NzY=
9
+ NjE1NmIxYzcwZDU4ODc4MGM3NmVlNjE4NzlhMzFiMjZjMTk3OTY4N2E3NDk1
10
+ Yjk0YzAyZjU5NDY4ZjY1YzE4MzljYjU3MTM1YzJiMTE4ZjgyNmQzMzBjZDVj
11
+ NTA5NzlmNWNmMjczYmM4YjdhYTQyMDVhMDFkNWU4MjVhMmY2N2E=
12
12
  data.tar.gz: !binary |-
13
- YWNkNzc5OGEwMDVlN2Y0ZmYwMTUyM2U2NTY0NDZhY2ZmNzA3ZjI4M2NhZDIy
14
- OWVlM2ZjZDVkMjAzYWU1ZjQ1ODI0ZTczYTBjNWRlNThhY2ViNzNiYTBjOGIx
15
- ODNiZTdlYTg2MDVlMjEyN2JiOTM2N2Y3ZGI3YzUwOGU3OTJjNmI=
13
+ MDI0NDAwMGI4ZmYxNjJmMGRjYzRhMTM2NzhkOTgwOTFkOTg1MzQ1OWRlNTAw
14
+ YThiNWM0YzA5NjQ2NDZiYzAyMmU2NmRhYTdjNDQwNzEzYzc3YThmNjZlYjBj
15
+ NDU3NTFjZjkxYzg1MGM5NTIyODlhYjkwNDlkZGFkNjE2YWU2NDM=
data/README.md CHANGED
@@ -59,6 +59,9 @@ In your Gemfile:
59
59
  # This line is optional if you do not want to suppress app logs in your <environment>.log
60
60
  config.logstasher.suppress_app_log = false
61
61
 
62
+ # This line is optional, it allows you to set a custom value for the @source field of the log event
63
+ config.logstasher.source = 'your.arbitrary.source'
64
+
62
65
  ## Logging params hash
63
66
 
64
67
  Logstasher can be configured to log the contents of the params hash. When enabled, the contents of the params hash (minus the ActionController internal params)
@@ -79,7 +82,7 @@ Since some fields are very specific to your application for e.g. *user_name*, so
79
82
 
80
83
  if LogStasher.enabled
81
84
  LogStasher.add_custom_fields do |fields|
82
- # This block is run in application_controller context,
85
+ # This block is run in application_controller context,
83
86
  # so you have access to all controller methods
84
87
  fields[:user] = current_user && current_user.mail
85
88
  fields[:site] = request.path =~ /^\/api/ ? 'api' : 'user'
data/Rakefile CHANGED
@@ -39,4 +39,12 @@ else
39
39
  ENV['COVERAGE'] = "true"
40
40
  Rake::Task['spec'].execute
41
41
  end
42
- end
42
+ end
43
+
44
+ task :console do
45
+ require 'irb'
46
+ require 'irb/completion'
47
+ require 'logstasher'
48
+ ARGV.clear
49
+ IRB.start
50
+ end
data/lib/logstasher.rb CHANGED
@@ -6,7 +6,9 @@ require 'active_support/ordered_options'
6
6
 
7
7
  module LogStasher
8
8
  extend self
9
- attr_accessor :logger, :enabled, :log_controller_parameters
9
+ attr_accessor :logger, :enabled, :log_controller_parameters, :source
10
+ # Setting the default to 'unknown' to define the default behaviour
11
+ @source = 'unknown'
10
12
 
11
13
  def remove_existing_log_subscriptions
12
14
  ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
@@ -54,6 +56,7 @@ module LogStasher
54
56
  LogStasher::RequestLogSubscriber.attach_to :action_controller
55
57
  self.logger = app.config.logstasher.logger || new_logger("#{Rails.root}/log/logstash_#{Rails.env}.log")
56
58
  self.logger.level = app.config.logstasher.log_level || Logger::WARN
59
+ self.source = app.config.logstasher.source unless app.config.logstasher.source.nil?
57
60
  self.enabled = true
58
61
  self.log_controller_parameters = !! app.config.logstasher.log_controller_parameters
59
62
  end
@@ -78,10 +81,9 @@ module LogStasher
78
81
  Thread.current[:logstasher_custom_fields] = val
79
82
  end
80
83
 
81
-
82
84
  def log(severity, msg)
83
85
  if self.logger && self.logger.send("#{severity}?")
84
- event = LogStash::Event.new('@fields' => {:message => msg, :level => severity},'@tags' => ['log'])
86
+ event = LogStash::Event.new('@source' => self.source, '@fields' => {:message => msg, :level => severity}, '@tags' => ['log'])
85
87
  self.logger.send severity, event.to_json
86
88
  end
87
89
  end
@@ -15,7 +15,7 @@ module LogStasher
15
15
 
16
16
  tags = ['request']
17
17
  tags.push('exception') if payload[:exception]
18
- event = LogStash::Event.new('@fields' => data, '@tags' => tags)
18
+ event = LogStash::Event.new('@source' => LogStasher.source, '@fields' => data, '@tags' => tags)
19
19
  LogStasher.logger << event.to_json + "\n"
20
20
  end
21
21
 
@@ -11,18 +11,20 @@ module ActionController
11
11
  }
12
12
 
13
13
  LogStasher.add_default_fields_to_payload(raw_payload, request)
14
- if self.respond_to?(:logtasher_add_custom_fields_to_payload)
15
- before_keys = raw_payload.keys.clone
16
- logtasher_add_custom_fields_to_payload(raw_payload)
17
- after_keys = raw_payload.keys
18
- # Store all extra keys added to payload hash in payload itself. This is a thread safe way
19
- LogStasher.custom_fields += after_keys - before_keys
20
- end
21
14
 
22
15
  ActiveSupport::Notifications.instrument("start_processing.action_controller", raw_payload.dup)
23
16
 
24
17
  ActiveSupport::Notifications.instrument("process_action.action_controller", raw_payload) do |payload|
25
18
  result = super
19
+
20
+ if self.respond_to?(:logtasher_add_custom_fields_to_payload)
21
+ before_keys = raw_payload.keys.clone
22
+ logtasher_add_custom_fields_to_payload(raw_payload)
23
+ after_keys = raw_payload.keys
24
+ # Store all extra keys added to payload hash in payload itself. This is a thread safe way
25
+ LogStasher.custom_fields += after_keys - before_keys
26
+ end
27
+
26
28
  payload[:status] = response.status
27
29
  append_info_to_payload(payload)
28
30
  result
@@ -30,4 +32,4 @@ module ActionController
30
32
  end
31
33
 
32
34
  end
33
- end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module LogStasher
2
- VERSION = "0.4.9"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -66,14 +66,16 @@ describe LogStasher do
66
66
  end
67
67
  end
68
68
 
69
- describe '.setup' do
69
+ shared_examples 'setup' do
70
70
  let(:logger) { double }
71
- let(:logstasher_config) { double(:logger => logger,:log_level => 'warn',:log_controller_parameters => nil) }
71
+ let(:logstasher_config) { double(:logger => logger, :log_level => 'warn', :log_controller_parameters => nil, :source => logstasher_source) }
72
72
  let(:config) { double(:logstasher => logstasher_config) }
73
73
  let(:app) { double(:config => config) }
74
74
  before do
75
+ @previous_source = LogStasher.source
75
76
  config.stub(:action_dispatch => double(:rack_cache => false))
76
77
  end
78
+ after { LogStasher.source = @previous_source } # Need to restore old source for specs
77
79
  it 'defines a method in ActionController::Base' do
78
80
  LogStasher.should_receive(:require).with('logstasher/rails_ext/action_controller/metal/instrumentation')
79
81
  LogStasher.should_receive(:require).with('logstash-event')
@@ -81,12 +83,25 @@ describe LogStasher do
81
83
  LogStasher::RequestLogSubscriber.should_receive(:attach_to).with(:action_controller)
82
84
  logger.should_receive(:level=).with('warn')
83
85
  LogStasher.setup(app)
86
+ LogStasher.source.should == (logstasher_source || 'unknown')
84
87
  LogStasher.enabled.should be_true
85
88
  LogStasher.custom_fields.should == []
86
89
  LogStasher.log_controller_parameters.should == false
87
90
  end
88
91
  end
89
92
 
93
+ describe '.setup' do
94
+ describe 'with source set' do
95
+ let(:logstasher_source) { 'foo' }
96
+ it_behaves_like 'setup'
97
+ end
98
+
99
+ describe 'without source set (default behaviour)' do
100
+ let(:logstasher_source) { nil }
101
+ it_behaves_like 'setup'
102
+ end
103
+ end
104
+
90
105
  describe '.suppress_app_logs' do
91
106
  let(:logstasher_config){ double(:logstasher => double(:suppress_app_log => true))}
92
107
  let(:app){ double(:config => logstasher_config)}
@@ -140,6 +155,16 @@ describe LogStasher do
140
155
  logger.should_receive(:send).with('warn',"{\"@source\":\"unknown\",\"@tags\":[\"log\"],\"@fields\":{\"message\":\"WARNING\",\"level\":\"warn\"},\"@timestamp\":\"timestamp\"}")
141
156
  LogStasher.log('warn', 'WARNING')
142
157
  end
158
+ context 'with a source specified' do
159
+ before :each do
160
+ LogStasher.source = 'foo'
161
+ end
162
+ it 'sets the correct source' do
163
+ logger.should_receive(:send).with('warn?').and_return(true)
164
+ logger.should_receive(:send).with('warn',"{\"@source\":\"foo\",\"@tags\":[\"log\"],\"@fields\":{\"message\":\"WARNING\",\"level\":\"warn\"},\"@timestamp\":\"timestamp\"}")
165
+ LogStasher.log('warn', 'WARNING')
166
+ end
167
+ end
143
168
  end
144
169
 
145
170
  %w( fatal error warn info debug unknown ).each do |severity|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstasher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shadab Ahmed
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-21 00:00:00.000000000 Z
11
+ date: 2014-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-event