module Lumberjack

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

frozen_string_literals: true

Constants

LINE_SEPARATOR

Public Class Methods

context(&block) click to toggle source

Contexts can be used to store tags that will be attached to all log entries in the block.

If this method is called with a block, it will set a logging context for the scope of a block. If there is already a context in scope, a new one will be created that inherits all the tags of the parent context.

Otherwise, it will return the current context. If one doesn't exist, it will return a new one but that context will not be in any scope.

# File lib/lumberjack.rb, line 56
def context(&block)
  current_context = Thread.current[:lumberjack_context]
  if block
    use_context(Context.new(current_context), &block)
  else
    current_context || Context.new
  end
end
context?() click to toggle source

Return true if inside a context block.

# File lib/lumberjack.rb, line 77
def context?
  !!Thread.current[:lumberjack_context]
end
context_tags() click to toggle source

Return the tags from the current context or nil if there are no tags.

# File lib/lumberjack.rb, line 82
def context_tags
  context = Thread.current[:lumberjack_context]
  context&.tags
end
tag(tags) click to toggle source

Set tags on the current context

# File lib/lumberjack.rb, line 88
def tag(tags)
  context = Thread.current[:lumberjack_context]
  context&.tag(tags)
end
unit_of_work(id = nil) { || ... } click to toggle source

Define a unit of work within a block. Within the block supplied to this method, calling unit_of_work_id will return the same value that can This can then be used for tying together log entries.

You can specify the id for the unit of work if desired. If you don't supply it, a 12 digit hexidecimal number will be automatically generated for you.

For the common use case of treating a single web request as a unit of work, see the Lumberjack::Rack::UnitOfWork class.

# File lib/lumberjack.rb, line 35
def unit_of_work(id = nil)
  id ||= SecureRandom.hex(6)
  context do
    context[:unit_of_work_id] = id
    yield
  end
end
unit_of_work_id() click to toggle source

Get the UniqueIdentifier for the current unit of work.

# File lib/lumberjack.rb, line 44
def unit_of_work_id
  context[:unit_of_work_id]
end
use_context(context) { || ... } click to toggle source

Set the context to use within a block.

# File lib/lumberjack.rb, line 66
def use_context(context, &block)
  current_context = Thread.current[:lumberjack_context]
  begin
    Thread.current[:lumberjack_context] = (context || Context.new)
    yield
  ensure
    Thread.current[:lumberjack_context] = current_context
  end
end