lib/null_logger.rb in null-logger-0.1.4 vs lib/null_logger.rb in null-logger-0.1.5
- old
+ new
@@ -1,32 +1,92 @@
# frozen_string_literal: true
-# Null logger class
-# Is used when logger is not defined
+# Null logger class. This is essentially the same as sending data down the
+# `/dev/null` blackhole.
+#
+# @example Basic Usage
+#
+# logger = NullLogger.new
+# Rails.logger = logger
+#
+#
+# @example Basic Pattern Usage
+# class SomeService
+# def initialize(options = {})
+# @logger = options[:logger] || NullLogger.new
+# end
+#
+# def perform
+# @logger.debug -> { "do some work here" }
+# # .. ..
+# @logger.info -> { "finished working" }
+# end
+# end
+#
+# service = SomeService.new(logger: Logger.new(STDOUT))
+# service.perform
+#
+# silent = SomeService.new(logger: NullLogger.new
+# silent.perform
+#
class NullLogger
- # Possible log levels from ruby Logger::Severity class
- LOG_LEVELS = %w[
- unknown
- fatal
- error
- warn
- info
- debug
- ].freeze
+ # @param _args Anything that we want to ignore
+ # @return [nil]
+ def unknown(*_args)
+ nil
+ end
- # @return [Boolean] true if we can handle this missing method, otherwise false
- # @param method_name [String, Symbol] method name
- # @param include_private [Boolean] should we include private methods as well
- def respond_to_missing?(method_name, include_private = false)
- LOG_LEVELS.include?(method_name.to_s) || super
+ # @param _args Anything that we want to ignore
+ # @return [nil]
+ def fatal(*_args)
+ nil
end
- # Returns nil for any method call from LOG_LEVELS array
- # Instead raise NoMethodError
- # @example:
- # NullLogger.new.fatal -> return nil
- # NullLogger.new.wrong_method -> raise NoMethodError
- def method_missing(method_name, *args, &block)
- return nil if LOG_LEVELS.include?(method_name.to_s)
- super
+ # @return [FALSE]
+ def fatal?
+ false
+ end
+
+ # @param _args Anything that we want to ignore
+ # @return [nil]
+ def error(*_args)
+ nil
+ end
+
+ # @return [FALSE]
+ def error?
+ false
+ end
+
+ # @param _args Anything that we want to ignore
+ # @return [nil]
+ def warn(*_args)
+ nil
+ end
+
+ # @return [FALSE]
+ def warn?
+ false
+ end
+
+ # @param _args Anything that we want to ignore
+ # @return [nil]
+ def info(*_args)
+ nil
+ end
+
+ # @return [FALSE]
+ def info?
+ false
+ end
+
+ # @param _args Anything that we want to ignore
+ # @return [nil]
+ def debug(*_args)
+ nil
+ end
+
+ # @return [FALSE]
+ def debug?
+ false
end
end