Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

film42
Copy link

@film42 film42 commented Jan 31, 2022

  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?

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?
@@ -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?
Copy link

@sshock sshock Jan 31, 2022

Choose a reason for hiding this comment

The 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.


module LogStasher
class << self
SILENT_LOGGER = ::Logger.new("/dev/null", level: :unknown)
Copy link

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 write LogStasher.singleton_class::SILENT_LOGGER

@@ -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
Copy link

Choose a reason for hiding this comment

The 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 initialize_logger should be named default_logger or backup_logger or something and ought to be private.

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.

@@ -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
Copy link

@sshock sshock Jan 31, 2022

Choose a reason for hiding this comment

The 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:

  1. The config/environments/*.rb file in the consuming project.
  2. The config/initializers/logstasher.rb in the consuming project.
  3. This railtie.rb file here.

Just in case 1 or 2 could happen before 3, should we change = to ||= here to avoid a problem?

::LogStasher.logger ||= options.logger

That way anyone who explicitly sets ::LogStasher.logger from 1 or 2 won't have to worry about it getting overridden here.

(If you are confident that this file will always get executed earlier than 1 or 2, then we could leave it as is.)

@quixoten
Copy link

quixoten commented Jan 31, 2022 via email

@quixoten
Copy link

I need to make an addendum to my previous comment (dusting off some old memories here). I think what I said about silence_standard_logging holds true. It is meant to disable the default rails logging, and not meant to act as a switch for what LogStasher itself logs; however, enabled is a bit trickier, since LogStasher has been co-opted to be more than just a rails request logger. In the current (before this change) implementation, LogStasher effectively supports three "modes" (even though there are four permutations of enabled and silence_standard_logging):

enabled setting silence_standard_logging setting resulting mode
true false log rails requests through logstasher and default rails logging
true true log rails requests through logstasher only
false false log rails requests through default rails logging only
false true

Neither of these settings modify what LogStasher does when you write to it directly, which I suspect, was a use-case outside of the scope of the original implementation. Perhaps this confusion could be avoided by aliasing enabled as log_rails_requests_through_logstasher, which more clearly reflects the scope of what the setting effects. There's probably a clearer name for silence_standard_logging as well. Alternatively, maybe both of these settings just need to be clearly scoped to the railtie, i.e., railtie_enabled, railtie_silence_standard_logging, since that's all they're intended to modify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants