lib/loba/internal/platform.rb in loba-2.0.0 vs lib/loba/internal/platform.rb in loba-2.1.0

- old
+ new

@@ -1,45 +1,26 @@ +require_relative 'platform/formatter' +require_relative 'platform/within_rails' + module Loba module Internal - # Internal class for managing logging across Rails and non-Rails applications - class Platform + # Internal module for managing output and logging across Rails and non-Rails applications + module 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. + # Provides mechanism for appropriate console output and logging. # - # 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 + # @note To avoid doubled output, if a non-Rails logger is to be logged to and +logdev+ is + # set to +$stdout+, then output will be suppressed (i.e., +settings.out+ is +false+). + # Doubled output can still occur; in that case, explicitly use +out: false+. + # + # @param settings [::Loba::Internal::Settings] settings for output control + # @return [lambda {|message| ...}] procedure for presenting output. Takes one argument, + # +message+ (String), for the output to be written + def writer(settings:) + lambda do |message| + puts(message) if settings.out? - Rails.logger.debug arg - end - else - ->(arg, _force_log = false) { puts arg } + settings.logger.debug { message } if settings.log? end end end end end