Sha256: 64b18685a249213e04aeb7181301ff8a47547867494cef8de9dcdde5132fd322

Contents?: true

Size: 1.6 KB

Versions: 14

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require "logger"

module Dry
  module System
    module Plugins
      module Logging
        # @api private
        def self.extended(system)
          system.instance_eval do
            setting :logger, reader: true

            setting :log_dir, default: "log"

            setting :log_levels, default: {
              development: Logger::DEBUG,
              test: Logger::DEBUG,
              production: Logger::ERROR
            }

            setting :logger_class, default: ::Logger, reader: true
          end

          system.after(:configure, &:register_logger)

          super
        end

        # Set a logger
        #
        # This is invoked automatically when a container is being configured
        #
        # @return [self]
        #
        # @api private
        def register_logger
          if registered?(:logger)
            self
          elsif config.logger
            register(:logger, config.logger)
          else
            config.logger = config.logger_class.new(log_file_path)
            config.logger.level = log_level

            register(:logger, config.logger)
            self
          end
        end

        # @api private
        def log_level
          config.log_levels.fetch(config.env, Logger::ERROR)
        end

        # @api private
        def log_dir_path
          root.join(config.log_dir).realpath
        end

        # @api private
        def log_file_path
          log_dir_path.join(log_file_name)
        end

        # @api private
        def log_file_name
          "#{config.env}.log"
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
dry-system-1.1.1 lib/dry/system/plugins/logging.rb
dry-system-1.1.0 lib/dry/system/plugins/logging.rb
dry-system-1.1.0.beta2 lib/dry/system/plugins/logging.rb
dry-system-1.1.0.beta1 lib/dry/system/plugins/logging.rb
dry-system-1.0.1 lib/dry/system/plugins/logging.rb
dry-system-1.0.0 lib/dry/system/plugins/logging.rb
dry-system-1.0.0.rc1 lib/dry/system/plugins/logging.rb
dry-system-0.27.2 lib/dry/system/plugins/logging.rb
dry-system-0.27.1 lib/dry/system/plugins/logging.rb
dry-system-0.27.0 lib/dry/system/plugins/logging.rb
dry-system-0.26.0 lib/dry/system/plugins/logging.rb
dry-system-0.25.0 lib/dry/system/plugins/logging.rb
dry-system-0.24.0 lib/dry/system/plugins/logging.rb
dry-system-0.23.0 lib/dry/system/plugins/logging.rb