-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Overhaul some behaviors of this gem #13
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
require 'logger' | ||
require 'logstash-event' | ||
require "logger" | ||
require "logstash-event" | ||
|
||
module LogStasher | ||
class << self | ||
SILENT_LOGGER = ::Logger.new("/dev/null", level: :unknown) | ||
|
||
attr_reader :append_fields_callback | ||
attr_writer :enabled | ||
attr_writer :include_parameters | ||
|
@@ -61,6 +63,9 @@ def log_as_json(payload, as_logstash_event: false) | |
end | ||
|
||
def logger | ||
# Return a /dev/null logger if logstasher is enabled or silencing was enabled. | ||
return SILENT_LOGGER if !enabled? || silence_standard_logging? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't check silence_standard_logging? here, because then it kills direct logging too. |
||
|
||
@logger ||= initialize_logger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid we can't change this without potentially breaking someone, but I really think If anyone is actually calling it from outside this class, due to its name they might think calling it actually changes @logger, which it doesn't. So..., given that, maybe it is a good thing if renaming that and making it private breaks anyone who was trying to call it. Note also that as part of making this private, it doesn't really need configurable params. |
||
end | ||
|
||
|
@@ -78,4 +83,4 @@ def silence_standard_logging? | |
end | ||
end | ||
|
||
require 'logstasher/railtie' if defined?(Rails) | ||
require "logstasher/railtie" if defined?(Rails) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,45 +16,20 @@ class Railtie < ::Rails::Railtie | |
::LogStasher.include_parameters = options.include_parameters | ||
::LogStasher.serialize_parameters = options.serialize_parameters | ||
::LogStasher.silence_standard_logging = options.silence_standard_logging | ||
::LogStasher.logger = options.logger || default_logger | ||
::LogStasher.logger = options.logger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure the order of execution of these three places:
Just in case 1 or 2 could happen before 3, should we change ::LogStasher.logger ||= options.logger That way anyone who explicitly sets (If you are confident that this file will always get executed earlier than 1 or 2, then we could leave it as is.) |
||
::LogStasher.logger.level = options.log_level | ||
::LogStasher.metadata = options.metadata | ||
end | ||
|
||
initializer 'logstasher.load' do | ||
if ::LogStasher.enabled? | ||
config.after_initialize do | ||
if ::LogStasher.enabled? && !::LogStasher.silence_standard_logging? | ||
::ActiveSupport.on_load(:action_controller) do | ||
require 'logstasher/log_subscriber' | ||
require 'logstasher/context_wrapper' | ||
require "logstasher/log_subscriber" | ||
require "logstasher/context_wrapper" | ||
|
||
include ::LogStasher::ContextWrapper | ||
end | ||
end | ||
end | ||
|
||
config.after_initialize do | ||
if ::LogStasher.enabled? && ::LogStasher.silence_standard_logging? | ||
require 'logstasher/silent_logger' | ||
|
||
::Rails::Rack::Logger.send(:include, ::LogStasher::SilentLogger) | ||
|
||
::ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber| | ||
if subscriber.is_a?(::ActiveSupport::LogSubscriber) | ||
subscriber.class.send(:include, ::LogStasher::SilentLogger) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def default_logger | ||
unless @default_logger | ||
path = ::Rails.root.join('log', "logstash_#{::Rails.env}.log") | ||
::FileUtils.touch(path) # prevent autocreate messages in log | ||
|
||
@default_logger = ::Logger.new(path) | ||
end | ||
|
||
@default_logger | ||
end | ||
end | ||
end |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this constant be inside the `class << self``?
Being inside that means I can't refer to it normally like
LogStasher::SILENT_LOGGER
. I would have to writeLogStasher.singleton_class::SILENT_LOGGER