Class: Chalk::Log::Logger
- Inherits:
-
Object
- Object
- Chalk::Log::Logger
- 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
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Class Method Summary collapse
-
.init ⇒ Object
Initialization of the logger backend.
Instance Method Summary collapse
-
#initialize(name) ⇒ Logger
constructor
Create a new logger, and auto-initialize everything.
-
#level ⇒ Object
The level this logger is set to.
-
#level=(level) ⇒ Object
Set the maximum log level.
-
#logging_disabled? ⇒ Boolean
Check whether logging has been globally turned off, either through configatron or LSpace.
- #with_contextual_info(contextual_info = {}, &blk) ⇒ Object
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
#backend ⇒ Object (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
.init ⇒ Object
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
#level ⇒ Object
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.
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.
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 |