Sha256: 99c572d00da1eae7dc6108e8f1298daf90dd5b8de7da5790e2b7ba86416ec9eb

Contents?: true

Size: 840 Bytes

Versions: 1

Compression:

Stored size: 840 Bytes

Contents

module LoggingLibrary
  module Mixins
    #
    # Mixin for adding a lazily-instantiated `logger` attribute to your class, with reasonable defaults based on the class name.
    #
    module Loggable
      def logger
        @logger ||= LoggerFactory.create(_logger_name)
      end

      private

      def _logger_name
        # Handle both `extend` and `include` use cases.
        if self.class == Class
          _short_class_name(to_s)
        else
          _short_class_name(self.class.name)
        end
      end

      def _short_class_name(full_name)
        # ClassThatIncludesLoggable vs Ecraft::LoggingLibrary::ClassThatIncludesLoggable. The latter feels overly detailed;
        # there are hopefully not that many classes with the same name in an application...
        full_name.split('::').last
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logging_library-1.0.4 lib/logging_library/mixins/loggable.rb