reidmorrison / rails_semantic_logger

Rails Semantic Logger replaces the Rails default logger with Semantic Logger
https://logger.rocketjob.io/rails
Apache License 2.0
320 stars 114 forks source link

Is there a easy way to turn off semantic logger in development without changes to Gemfile? #213

Closed tomas-stefano closed 2 months ago

tomas-stefano commented 5 months ago

Hi all,

We are using semantic logger in production but we would like to have the option to turn on and turn off semantic logger in development.

In the team I worked with we would like to have the option of any devs wants to use semantic logger in development they can and the ones that prefer Rails default, they also can.

I see that the moment we add on the Gemfile the rails semantic logger engine does a monkey patching on ActiveSupport::Logger, so hence my question.

Is there an easy way to continue to require the gem but have the ability to turn on and turn off at will? The only option I can see is to use the Gemfile and env vars but I want to avoid that because I think the problem is on the require itself and the ability that I can turn off the gem at will.

Sorry to open an Github issue for making a question. I am not sure if here is the best way to make the question.

Expected Behavior

Have the option to turn on or turn off semantic logger in development.

guimon commented 5 months ago

I have the exact same problem. When developing, and even running tests, seeing the regular stack trace is much more productive than the semantic logger output.

aburgel commented 3 months ago

Try adding config.rails_semantic_logger.semantic = false to your development.rb and test.rb environment config files.

guimon commented 3 months ago

Thanks, @aburgel but unfortunately that doesn't do the trick. I wonder if making https://github.com/reidmorrison/rails_semantic_logger/blob/master/lib/rails_semantic_logger.rb look into that flag before autoloading/requiring other modules would be the correct thing to do?

aburgel commented 3 months ago

You can also set

config.rails_semantic_logger.started    = true
config.rails_semantic_logger.processing = true
config.rails_semantic_logger.rendered   = true

But ultimately this (and what I suggested in my earlier comment) will keep semantic logger, just try to get it's output to match what Rails originally provided. If you really want it completely gone, you could use require: false in your Gemfile and then selectively require it in certain environments. But I haven't tried that.

trvsdnn commented 2 months ago

We use a custom formatter for dev. IMO something like this should be available in semantic logger as an optional formatter.

class DevLogFormatter < SemanticLogger::Formatters::Default
  delegate :message, to: :log

  def call(log, logger)
    self.log = log
    self.logger = logger

    [message, payload].compact.join(' ')
  end
end

The problem with not using semantic_logger with all the environments is you lose the ability to use the payloads, exceptions, block args, measurement, etc., logger abilities.

reidmorrison commented 2 months ago

I believe others have done this by using something like this in the Gemfile:

gem "rails_semantic_logger", require: false

And then in the code, when we conditionally want to load rails semantic logger, it can be required manually, for example in dev:

require "rails_semantic_logger"

It is also a way to bypass the automatic patches.

Another alternative is to use Semantic Logger directly without pulling in Rails Semantic Logger and all of its patches to other gems to get them to use structured logging.