Sha256: c57581f204a4d4326ba0ad22596b079c130af0b4396cc55df3cec0e5cfd98256

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 KB

Contents

# Thin wrapper over Logging::Logger. This is the per-class object
# instantiated by the `log` method.
class Chalk::Log::Logger
  attr_reader :backend

  # Initialization of the logger backend. It does the actual creation
  # of the various logger methods. Will be called automatically upon
  # your first `log` method call.
  def self.init
    Chalk::Log::LEVELS.each do |level|
      define_method(level) do |*data, &blk|
        return if logging_disabled?
        @backend.send(level, data, &blk)
      end
    end
  end

  # The level this logger is set to.
  def level
    @backend.level
  end

  # Set the maximum log level.
  #
  # @param level [Fixnum|String|Symbol] A valid Logging::Logger level, e.g. :debug, 0, 'DEBUG', etc.
  def level=(level)
    @backend.level = level
  end

  # Create a new logger, and auto-initialize everything.
  def initialize(name)
    # It's generally a bad pattern to auto-init, but we want
    # Chalk::Log to be usable anytime during the boot process, which
    # requires being a little bit less explicit than we usually like.
    Chalk::Log.init
    @backend = ::Logging::Logger.new(name)
    if level = configatron.chalk.log.default_level
      @backend.level = level
    end
  end

  # Check whether logging has been globally turned off, either through
  # configatron or LSpace.
  def logging_disabled?
    configatron.chalk.log.disabled || LSpace[:'chalk.log.disabled']
  end

  def with_contextual_info(contextual_info={}, &blk)
    unless blk
      raise ArgumentError.new("Must pass a block to #{__method__}")
    end
    unless contextual_info.is_a?(Hash)
      raise TypeError.new(
        "contextual_info must be a Hash, but got #{contextual_info.class}"
      )
    end
    existing_context = LSpace[:'chalk.log.contextual_info'] || {}
    LSpace.with(
      :'chalk.log.contextual_info' => contextual_info.merge(existing_context),
      &blk
    )
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
chalk-log-0.1.7 lib/chalk-log/logger.rb
chalk-log-0.1.6 lib/chalk-log/logger.rb
chalk-log-0.1.5 lib/chalk-log/logger.rb
chalk-log-0.1.4 lib/chalk-log/logger.rb
chalk-log-0.1.3 lib/chalk-log/logger.rb
chalk-log-0.1.2 lib/chalk-log/logger.rb