Sha256: 4a97b9a560f5899a63917234541a0c33ea07edfde183908e00e84230ba108968

Contents?: true

Size: 864 Bytes

Versions: 7

Compression:

Stored size: 864 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 || self.class == Module
          _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

7 entries across 7 versions & 1 rubygems

Version Path
logging_library-1.3.0 lib/logging_library/mixins/loggable.rb
logging_library-1.2.0 lib/logging_library/mixins/loggable.rb
logging_library-1.1.2 lib/logging_library/mixins/loggable.rb
logging_library-1.1.1 lib/logging_library/mixins/loggable.rb
logging_library-1.0.7 lib/logging_library/mixins/loggable.rb
logging_library-1.0.6 lib/logging_library/mixins/loggable.rb
logging_library-1.0.5 lib/logging_library/mixins/loggable.rb