Class: Chalk::Log::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/chalk-log/logger.rb

Overview

Thin wrapper over Logging::Logger. This is the per-class object instantiated by the ‘log` method.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Logger

Create a new logger, and auto-initialize everything.



31
32
33
34
35
36
37
38
39
40
# File 'lib/chalk-log/logger.rb', line 31

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

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



4
5
6
# File 'lib/chalk-log/logger.rb', line 4

def backend
  @backend
end

Class Method Details

.initObject

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.



9
10
11
12
13
14
15
16
# File 'lib/chalk-log/logger.rb', line 9

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

Instance Method Details

#levelObject

The level this logger is set to.



19
20
21
# File 'lib/chalk-log/logger.rb', line 19

def level
  @backend.level
end

#level=(level) ⇒ Object

Set the maximum log level.

Parameters:

  • level (Fixnum|String|Symbol)

    A valid Logging::Logger level, e.g. :debug, 0, ‘DEBUG’, etc.



26
27
28
# File 'lib/chalk-log/logger.rb', line 26

def level=(level)
  @backend.level = level
end

#logging_disabled?Boolean

Check whether logging has been globally turned off, either through configatron or LSpace.

Returns:

  • (Boolean)


44
45
46
# File 'lib/chalk-log/logger.rb', line 44

def logging_disabled?
  configatron.chalk.log.disabled || LSpace[:'chalk.log.disabled']
end

#with_contextual_info(contextual_info = {}, &blk) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chalk-log/logger.rb', line 48

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