Sha256: 176f650b700059dbbf782dcaaf802f764200033156d20c45de7f734120907f39

Contents?: true

Size: 1.47 KB

Versions: 27

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid

  # Contains logging behavior.
  module Loggable

    # Get the logger.
    #
    # @note Will try to grab Rails' logger first before creating a new logger
    #   with stderr.
    #
    # @example Get the logger.
    #   Loggable.logger
    #
    # @return [ Logger ] The logger.
    #
    # @since 3.0.0
    def logger
      return @logger if defined?(@logger)
      @logger = rails_logger || default_logger
    end

    # Set the logger.
    #
    # @example Set the logger.
    #   Loggable.logger = Logger.new(STDERR)
    #
    # @param [ Logger ] logger The logger to set.
    #
    # @return [ Logger ] The new logger.
    #
    # @since 3.0.0
    def logger=(logger)
      @logger = logger
    end

    private

    # Gets the default Mongoid logger - stderr.
    #
    # @api private
    #
    # @example Get the default logger.
    #   Loggable.default_logger
    #
    # @return [ Logger ] The default logger.
    #
    # @since 3.0.0
    def default_logger
      logger = Logger.new(STDERR)
      logger.level = Mongoid::Config.log_level
      logger
    end

    # Get the Rails logger if loaded in a Rails application, otherwise nil.
    #
    # @api private
    #
    # @example Get Rails' logger.
    #   Loggable.rails_logger
    #
    # @return [ Logger ] The Rails logger.
    #
    # @since 3.0.0
    def rails_logger
      if defined?(::Rails)
        ::Rails.logger
      else
        nil
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/loggable.rb
mongoid-7.3.4 lib/mongoid/loggable.rb
mongoid-7.1.11 lib/mongoid/loggable.rb
mongoid-7.2.6 lib/mongoid/loggable.rb
mongoid-7.3.3 lib/mongoid/loggable.rb
mongoid-7.3.2 lib/mongoid/loggable.rb
mongoid-7.2.5 lib/mongoid/loggable.rb
mongoid-7.1.10 lib/mongoid/loggable.rb
mongoid-7.1.9 lib/mongoid/loggable.rb
mongoid-7.2.4 lib/mongoid/loggable.rb
mongoid-7.3.1 lib/mongoid/loggable.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/loggable.rb
mongoid-7.3.0 lib/mongoid/loggable.rb
mongoid-7.2.3 lib/mongoid/loggable.rb
mongoid-7.1.8 lib/mongoid/loggable.rb
mongoid-7.2.2 lib/mongoid/loggable.rb
mongoid-7.2.1 lib/mongoid/loggable.rb
mongoid-7.1.7 lib/mongoid/loggable.rb
mongoid-7.2.0 lib/mongoid/loggable.rb
mongoid-7.1.6 lib/mongoid/loggable.rb