lib/loba/internal/platform.rb in loba-1.2.1 vs lib/loba/internal/platform.rb in loba-2.0.0
- old
+ new
@@ -1,16 +1,18 @@
module Loba
module Internal
# Internal class for managing logging across Rails and non-Rails applications
class Platform
class << self
- # Returns true if Rails appears to be available
+ # Checks if Rails is present
+ # @return [Boolean] true if Rails appears to be available; otherwise, false
def rails?
defined?(Rails) ? true : false
end
- # Returns true if logging is to be allowed
+ # 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
@@ -18,15 +20,26 @@
rescue StandardError
true # let it attempt to log anyway
end
end
- # Returns a logging mechanism appropriate for the application
+ # 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?
- ->(arg) { Rails.logger.debug arg }
+ lambda do |arg, force_log = false|
+ puts arg
+ return unless force_log
+
+ Rails.logger.debug arg
+ end
else
- ->(arg) { puts arg }
+ ->(arg, _force_log = false) { puts arg }
end
end
end
end
end