Skip to content

Commit

Permalink
WIP: Overhaul some behaviors of this gem
Browse files Browse the repository at this point in the history
1. You should be able to set `enabled=false` and that means you will not
   log anything. Period.
2. You should be able to set `enabled=true` with
   `silence_standard_logging=true` and it will not include any of the
   log subscriber code, but it will allow you to log anything custom.
3. You should be able to recreate existing behavior with a different
   configuration. Example:
   ```
   # directly updating code
   ::LogStasher.logger = ::Logger.new(
     ::Rails.root.join("log", "logstash_#{Rails.env}.log"))

   # via the device factory
   device = ::LogStasher::Device::Factory(type: :file, path:
     ::Rails.root.join("log", "logstash_#{Rails.env}.log"))
   ::LogStasher.logger = ::Logger.new(device)
   ```

NOTE: This is a breaking change, but it might make default behaviors
easier to understand. Right now when `enable=false` it causes logs to be
piped to the logstash log file for rails apps, and that seems very
awkward. Maybe this is worth fixing?
  • Loading branch information
film42 committed Jan 31, 2022
1 parent a0a24a5 commit 58a6aa4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 49 deletions.
11 changes: 8 additions & 3 deletions lib/logstasher.rb
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
Expand Down Expand Up @@ -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?

@logger ||= initialize_logger
end

Expand All @@ -78,4 +83,4 @@ def silence_standard_logging?
end
end

require 'logstasher/railtie' if defined?(Rails)
require "logstasher/railtie" if defined?(Rails)
2 changes: 2 additions & 0 deletions lib/logstasher/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def self.factory(config)
::LogStasher::Device::UDP.new(config)
when "stdout", :stdout then
$stdout
when "file", :file then
config["path"]
else
fail ArgumentError, "Unknown type: #{type}"
end
Expand Down
35 changes: 5 additions & 30 deletions lib/logstasher/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
::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
16 changes: 0 additions & 16 deletions lib/logstasher/silent_logger.rb

This file was deleted.

0 comments on commit 58a6aa4

Please sign in to comment.