Class: Cybertrain::Logger

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/logger.rb

Overview

A leveled logger writing one line per message to standard output (or to the io it was given): debug, info, warn, error, fatal. The line’s shape is the file’s header comment; logger is the one the framework logs through.

Defined Under Namespace

Classes: Scope

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_scope ⇒ Scope

This thread’s Scope, NO_SCOPE when it has none.

Returns:



140
141
142
143
# File 'cybertrain/logger.rb', line 140

def self.current_scope
  value = Thread.current[SCOPE_KEY]
  value.nil? ? NO_SCOPE : value
end

.json_lines ⇒ Boolean

Whether lines are JSON objects (c.log_format = :json, installed by Runtime.apply) rather than text.

Returns:

  • (Boolean)


190
191
192
# File 'cybertrain/logger.rb', line 190

def self.json_lines
  @json_lines
end

.json_lines=(on) ⇒ nil

Turns JSON lines on or off.

Parameters:

  • on (Boolean)

Returns:

  • (nil)


198
199
200
201
# File 'cybertrain/logger.rb', line 198

def self.json_lines=(on)
  @json_lines = on
  nil
end

.reset_scope ⇒ nil

Empties this thread’s slot: the thread is back to NO_SCOPE. The test harness calls it around every case (spec section 20).

Returns:

  • (nil)


161
162
163
164
# File 'cybertrain/logger.rb', line 161

def self.reset_scope
  Thread.current[SCOPE_KEY] = nil
  nil
end

.swap_scope(scope) ⇒ Scope

Makes scope this thread’s Scope (the slot holds nil for NO_SCOPE) and answers the one it replaces. Whoever runs work on a thread swaps its Scope in and the answered one back in an ensure.

Parameters:

Returns:



151
152
153
154
155
# File 'cybertrain/logger.rb', line 151

def self.swap_scope(scope)
  previous = Logger.current_scope
  Thread.current[SCOPE_KEY] = scope.equal?(NO_SCOPE) ? nil : scope
  previous
end

Instance Method Details

#tagged(*tags) ⇒ Object

Adds tags to every line logged on this thread while the block runs, after the request’s own, and restores the previous tags when it ends, also when it raises (spec section 12.4). Every Logger on the thread sees them: they live in the thread’s Scope. Answers the block’s value. There is no block-less form.

Examples:

Cybertrain.logger.tagged("billing") do
  Cybertrain.logger.info("charged 300") # ... INFO [<request id>] [billing] charged 300
end

Parameters:

  • tags (Array<String>)

Returns:

  • (Object) —

    the block’s value



511
# File 'cybertrain/logger.rb', line 511

def tagged(*tags) = Logger.tagged_during(tags) { yield }