zormandi / logcraft

A zero-configuration structured logging solution for pure Ruby or Ruby on Rails projects.
MIT License
36 stars 2 forks source link

Conditionally disable Logcraft w/ Rails #4

Closed kavinphan closed 1 year ago

kavinphan commented 1 year ago

In a Rails project, is there a way to disable Logcraft conditionally? Would love to use Logcraft in prod environments, but for local development I'd prefer the default Rails logger.

zormandi commented 1 year ago

No, not really. To be honest, I haven't considered wanting to see different logs in development and production so there isn't an easy mechanism for switching. You would basically have to revert the setup that's in the railtie.rb.

Do you want to switch because of the readability or are you missing data (e.g. view render time)? If it's just about the format then you can use the logcraft.layout_options.formatter configuration option to output nicer looking messages specifically in the development environment.

kavinphan commented 1 year ago

It was mainly for readability, but I ended up doing what you suggested and formatted the logs to look about the same. Also needed to mute the SQL SCHEMA events-- those don't seem to appear in Rails's default logging. Thanks!

incubus commented 5 months ago

@kphan32 could you please share your formatter? Thank you!

kavinphan commented 5 months ago
# Adjust Logcraft verbosity by environment.
module LogVerbosity
  module ByEnvironment
    private

    def background_of(event)
      super.slice(*context_filter)
    end

    def context_filter
      @context_filter ||= case Rails.env
                          when "production", "staging"
                            %w[timestamp level dd]
                          when "development", "test"
                            %w[level]
                          end
    end
  end
end

Logcraft::LogLayout.prepend(LogVerbosity::ByEnvironment)