Sha256: 38a15ad1af1a79d61e16375f534fbc1f91455e4efd4798f602915f59b8e1bf89

Contents?: true

Size: 1.96 KB

Versions: 13

Compression:

Stored size: 1.96 KB

Contents

module VagrantCloud
  module Logger

    @@lock = Mutex.new

    # @return [Log4r::Logger] default logger
    def self.default
      @@lock.synchronize do
        if !@logger
          # Require Log4r and define the levels we'll be using
          require 'log4r/config'
          Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)

          level = nil
          begin
            level = Log4r.const_get(ENV.fetch("VAGRANT_CLOUD_LOG", "FATAL").upcase)
          rescue NameError
            # This means that the logging constant wasn't found,
            # which is fine. We just keep `level` as `nil`. But
            # we tell the user.
            level = nil
          end

          # Some constants, such as "true" resolve to booleans, so the
          # above error checking doesn't catch it. This will check to make
          # sure that the log level is an integer, as Log4r requires.
          level = nil if !level.is_a?(Integer)

          # Only override the log output format if the default is set
          if Log4r::Outputter.stderr.formatter.is_a?(Log4r::DefaultFormatter)
            base_formatter = Log4r::PatternFormatter.new(
              pattern: "%d [%5l] %m",
              date_pattern: "%F %T"
            )
            Log4r::Outputter.stderr.formatter = base_formatter
          end

          logger = Log4r::Logger.new("vagrantcloud")
          logger.outputters = Log4r::Outputter.stderr
          logger.level = level
          @logger = logger
        end
      end
      @logger
    end

    def self.included(klass)
      klass.class_variable_set(:@@logger, Log4r::Logger.new(klass.name.downcase))
      klass.class_eval { define_method(:logger) { self.class.class_variable_get(:@@logger) } }
    end

    # @return [Log4r::Logger] logger instance for current context
    def logger
      @@lock.synchronize do
        if !@logger
          @logger = Log4r::Logger.new(self.class.name.downcase)
        end
        @logger
      end
    end
  end

  Logger.default
end

Version data entries

13 entries across 12 versions & 2 rubygems

Version Path
vagrant_cloud-3.1.2 lib/vagrant_cloud/logger.rb
vagrant_cloud-3.1.1 lib/vagrant_cloud/logger.rb
vagrant_cloud-3.1.0 lib/vagrant_cloud/logger.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/vagrant_cloud-3.0.5/lib/vagrant_cloud/logger.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/vagrant_cloud-3.0.5/lib/vagrant_cloud/logger.rb
vagrant_cloud-3.0.5 lib/vagrant_cloud/logger.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/vagrant_cloud-3.0.2/lib/vagrant_cloud/logger.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/vagrant_cloud-3.0.4/lib/vagrant_cloud/logger.rb
vagrant_cloud-3.0.4 lib/vagrant_cloud/logger.rb
vagrant_cloud-3.0.3 lib/vagrant_cloud/logger.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/vagrant_cloud-3.0.2/lib/vagrant_cloud/logger.rb
vagrant_cloud-3.0.2 lib/vagrant_cloud/logger.rb
vagrant_cloud-3.0.1 lib/vagrant_cloud/logger.rb