Skip to content

Commit

Permalink
Merge pull request #99 from MasteryConnect/master
Browse files Browse the repository at this point in the history
YAML config, enable/disable log_subscriber's, 1 bug fix
  • Loading branch information
shadabahmed committed Feb 18, 2016
2 parents a327870 + 40b68fe commit 9a41842
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ In your Gemfile:
# Enable the logstasher logs for the current environment
config.logstasher.enabled = true

# Each of the following lines are optional. If you want to selectively disable log subscribers.
config.logstasher.controller_enabled = false
config.logstasher.mailer_enabled = false
config.logstasher.record_enabled = false
config.logstasher.view_enabled = false

# This line is optional if you do not want to suppress app logs in your <environment>.log
config.logstasher.suppress_app_log = false

Expand All @@ -66,10 +72,27 @@ In your Gemfile:

# This line is optional if you do not want to log the backtrace of exceptions
config.logstasher.backtrace = false

# This line is optional, defaults to log/logstasher_<environment>.log
config.logstasher.logger_path = 'log/logstasher.log'

## Optionally use config/logstasher.yml (overrides `<environment>.rb`)

Has the same optional fields as the `<environment>.rb`. You can specify common configurations that are then overriden by environment specific configurations:

controller_enabled: true
mailer_enabled: false
record_enabled: false
view_enabled: true
suppress_app_log: false
development:
enabled: true
record_enabled: true
production:
enabled: true
mailer_enabled: true
view_enabled: false

## Logging params hash

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)
Expand Down
12 changes: 6 additions & 6 deletions lib/logstasher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module LogStasher
STORE_KEY = :logstasher_data
REQUEST_CONTEXT_KEY = :logstasher_request_context

attr_accessor :logger, :logger_path, :enabled, :log_controller_parameters, :source, :backtrace,
attr_accessor :logger, :logger_path, :enabled, :log_controller_parameters, :source, :backtrace,
:controller_monkey_patch
# Setting the default to 'unknown' to define the default behaviour
@source = 'unknown'
Expand Down Expand Up @@ -87,15 +87,15 @@ def clear_request_context
def setup_before(config)
require 'logstash-event'
self.enabled = config.enabled
LogStasher::ActiveSupport::LogSubscriber.attach_to :action_controller
LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer
LogStasher::ActiveRecord::LogSubscriber.attach_to :active_record
LogStasher::ActionView::LogSubscriber.attach_to :action_view
LogStasher::ActiveSupport::LogSubscriber.attach_to :action_controller if config.controller_enabled
LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer if config.mailer_enabled
LogStasher::ActiveRecord::LogSubscriber.attach_to :active_record if config.record_enabled
LogStasher::ActionView::LogSubscriber.attach_to :action_view if config.view_enabled
end

def setup(config)
# Path instrumentation class to insert our hook
if (! config.controller_monkey_patch && config.controller_monkey_patch != false) || config.controller_monkey_patch == true
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)
Expand Down
2 changes: 0 additions & 2 deletions lib/logstasher/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def logger
def logstash_event(event)
data = event.payload

return unless logger.debug?
return if 'SCHEMA' == data[:name]

data.merge! runtimes(event)
Expand Down Expand Up @@ -52,7 +51,6 @@ def extract_sql(data)

def extract_custom_fields(data)
custom_fields = (!LogStasher.custom_fields.empty? && data.extract!(*LogStasher.custom_fields)) || {}
LogStasher.custom_fields.clear
custom_fields
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/logstasher/active_support/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def extract_exception(payload)

def extract_custom_fields(payload)
custom_fields = (!LogStasher.custom_fields.empty? && payload.extract!(*LogStasher.custom_fields)) || {}
LogStasher.custom_fields.clear
custom_fields
end
end
Expand Down
39 changes: 39 additions & 0 deletions lib/logstasher/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
require 'rails/railtie'
require 'action_view/log_subscriber'
require 'action_controller/log_subscriber'
require 'socket'

module LogStasher
class Railtie < Rails::Railtie
config.logstasher = ::ActiveSupport::OrderedOptions.new
config.logstasher.enabled = false

# Set up the default logging options
config.logstasher.controller_enabled = true
config.logstasher.mailer_enabled = true
config.logstasher.record_enabled = false
config.logstasher.view_enabled = true

# Try loading the config/logstasher.yml if present
env = Rails.env.to_sym || :development
config_file = File.expand_path "./config/logstasher.yml"

# Load and ERB templating of YAML files
LOGSTASHER = File.exists?(config_file) ? YAML.load(ERB.new(File.read(config_file)).result).symbolize_keys : nil

initializer :logstasher, :before => :load_config_initializers do |app|
if LOGSTASHER.present?
# process common configs
LogStasher.process_config(app.config.logstasher, LOGSTASHER)
# process environment specific configs
LogStasher.process_config(app.config.logstasher, LOGSTASHER[env].symbolize_keys) if LOGSTASHER.key? env
end

app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
LogStasher.setup_before(app.config.logstasher) if app.config.logstasher.enabled
end
Expand All @@ -18,4 +39,22 @@ class Railtie < Rails::Railtie
end
end
end

def process_config(config, yml_config)
# Enable the logstasher logs for the current environment
config.enabled = yml_config[:enabled] if yml_config.key? :enabled
config.controller_enabled = yml_config[:controller_enabled] if yml_config.key? :controller_enabled
config.mailer_enabled = yml_config[:mailer_enabled] if yml_config.key? :mailer_enabled
config.record_enabled = yml_config[:record_enabled] if yml_config.key? :record_enabled
config.view_enabled = yml_config[:view_enabled] if yml_config.key? :view_enabled
#
# # This line is optional if you do not want to suppress app logs in your <environment>.log
config.suppress_app_log = yml_config[:suppress_app_log] if yml_config.key? :suppress_app_log
#
# # This line is optional, it allows you to set a custom value for the @source field of the log event
config.source = yml_config[:source].present? ? yml_config[:source] : IPSocket.getaddress(Socket.gethostname)

config.backtrace = yml_config[:backtrace] if yml_config.key? :backtrace
config.logger_path = yml_config[:logger_path] if yml_config.key? :logger_path
end
end
13 changes: 13 additions & 0 deletions sample_logstash_configurations/logstasher.yml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
controller_enabled: true
mailer_enabled: false
record_enabled: false
view_enabled: true
suppress_app_log: false
development:
enabled: true
record_enabled: true
production:
enabled: true
mailer_enabled: true
view_enabled: false

19 changes: 10 additions & 9 deletions spec/lib/logstasher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@
expect(LogStasher::ActionView::LogSubscriber).to receive(:attach_to).with(:action_view)
expect(LogStasher).to receive(:require).with('logstash-event')
end

end
shared_examples 'setup' do
let(:logstasher_source) { nil }
let(:logstasher_config) { double(:enabled => true,
let(:logstasher_config) { double(:enabled => true,
:logger => logger, :log_level => 'warn', :log_controller_parameters => nil,
:source => logstasher_source, :logger_path => logger_path, :backtrace => true,
:controller_monkey_patch => true) }
:controller_monkey_patch => true, :controller_enabled => true,
:mailer_enabled => true, :record_enabled => false, :view_enabled => true) }
let(:config) { double(:logstasher => logstasher_config) }
let(:app) { double(:config => config) }
before do
Expand Down Expand Up @@ -301,7 +302,7 @@
end
end
end

describe ".enabled?" do
it "returns false if not enabled" do
expect(LogStasher).to receive(:enabled).and_return(false)
Expand All @@ -311,12 +312,12 @@
expect(LogStasher.enabled?).to be true
end
end

describe ".called_as_rake?" do
it "returns false if not called as rake" do
expect(LogStasher.called_as_rake?).to be false
end

it "returns true if called as rake" do
expect(File).to receive(:basename).with($0).and_return('rake')
expect(LogStasher.called_as_rake?).to be true
Expand All @@ -327,7 +328,7 @@
it "does not touch request_context if not called as rake" do
expect(LogStasher.request_context).to be_empty
end

it "sets request_context accordingly if called as rake" do
expect(LogStasher).to receive(:called_as_rake?).and_return(true)
expect(Rake.application).to receive(:top_level_tasks).and_return(['mytask'])
Expand All @@ -341,7 +342,7 @@
it "returns false if not called as console" do
expect(LogStasher.called_as_console?).to be false
end

it "returns true if called as rake" do
require 'rails/commands/console'
expect(LogStasher.called_as_console?).to be true
Expand All @@ -352,7 +353,7 @@
it "does not touch request_context if not called as console" do
expect(LogStasher.request_context).to be_empty
end

it "sets request_context accordingly if called as console" do
require 'rails/commands/console'
expect(LogStasher).to receive(:called_as_console?).and_return(true)
Expand Down

0 comments on commit 9a41842

Please sign in to comment.