Sha256: 12ba6f7a30acb020c675ee29238212b82db24eea27515939479347c4b7d00fd0

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require_relative "../base"

module Rsg
  module Logging
    class CommonGenerator < Rsg::Generators::Base
      def banner
        say "Improving basic logging configs"
      end

      def log_level_env_var
       inject_into_file "config/application.rb", <<-CODE, before: /^  end$/
\n    # Sane default log levels per env, but able to tweak with env var
    # NOTE: Settings on config/environments/*.rb take precedence over this
    config.log_level = ENV.fetch('LOG_LEVEL') { Rails.env.development? ? 'debug' : 'info' }.to_sym

    # Log to stdout most of the time and allow use of JSON logs in dev by using an env var
    config.x.log_to_stdout = %w[1 true yes].include?(ENV.fetch('RAILS_LOG_TO_STDOUT', '1'))
       CODE
      end

      def stdout_logging_in_dev
       inject_into_file "config/environments/development.rb", <<-CODE, before: /^end$/
\n  # Configure stdout logging in dev as well
  if config.x.log_to_stdout
    logger = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger = ActiveSupport::TaggedLogging.new(logger)
  end
     CODE
      end

      def adjust_prod
        comment_lines "config/environments/production.rb", /^(\s*)(config\.log_level .+)$/
        gsub_file "config/environments/production.rb",
                  'ENV["RAILS_LOG_TO_STDOUT"].present?',
                  "config.x.log_to_stdout"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rsg-0.0.1 lib/rsg/generators/logging/common_generator.rb