Sha256: ff833d0f75d2c1cb6ed3bd89511b1588145f5716d705d4ba8ec21338966c77c7

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Loba
  module Internal
    # Internal class for managing logging across Rails and non-Rails applications
    class Platform
      class << self
        # Checks if Rails is present
        # @return [Boolean] true if Rails appears to be available; otherwise, false
        def rails?
          defined?(Rails) ? true : false
        end

        # Checks if logging output is permitted.
        # @return [Boolean] true if logging is to be allowed; otherwise, false
        def logging_ok?(force_true = false)
          return true if force_true
          return true unless rails?

          begin
            !Rails.env.production?
          rescue StandardError
            true # let it attempt to log anyway
          end
        end

        # Provides logging mechanism appropriate in the application.
        #
        # Returned lambda takes 2 arguments:
        #   * arg [String] value to be output (and potentially logged)
        #   * force_log [Boolean] when false (default), never logs to Rails.logger;
        #       when true, logs to Rails.logger if present
        # @return [Lambda] procedure for logging output
        def logger
          if rails? && Rails.logger.present?
            lambda do |arg, force_log = false|
              puts arg
              return unless force_log

              Rails.logger.debug arg
            end
          else
            ->(arg, _force_log = false) { puts arg }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
loba-2.0.0 lib/loba/internal/platform.rb