Module: Lumberjack

Defined in:
lib/lumberjack/rack.rb,
lib/lumberjack.rb,
lib/lumberjack/tags.rb,
lib/lumberjack/device.rb,
lib/lumberjack/logger.rb,
lib/lumberjack/context.rb,
lib/lumberjack/severity.rb,
lib/lumberjack/template.rb,
lib/lumberjack/formatter.rb,
lib/lumberjack/log_entry.rb,
lib/lumberjack/device/null.rb,
lib/lumberjack/device/multi.rb,
lib/lumberjack/rack/context.rb,
lib/lumberjack/device/writer.rb,
lib/lumberjack/tag_formatter.rb,
lib/lumberjack/tagged_logging.rb,
lib/lumberjack/device/log_file.rb,
lib/lumberjack/rack/request_id.rb,
lib/lumberjack/rack/unit_of_work.rb,
lib/lumberjack/tagged_logger_support.rb,
lib/lumberjack/formatter/id_formatter.rb,
lib/lumberjack/device/rolling_log_file.rb,
lib/lumberjack/formatter/strip_formatter.rb,
lib/lumberjack/formatter/object_formatter.rb,
lib/lumberjack/formatter/string_formatter.rb,
lib/lumberjack/formatter/inspect_formatter.rb,
lib/lumberjack/device/date_rolling_log_file.rb,
lib/lumberjack/device/size_rolling_log_file.rb,
lib/lumberjack/formatter/date_time_formatter.rb,
lib/lumberjack/formatter/exception_formatter.rb,
lib/lumberjack/formatter/structured_formatter.rb,
lib/lumberjack/formatter/pretty_print_formatter.rb

Overview

frozen_string_literals: true

Defined Under Namespace

Modules: Rack, Severity, TaggedLoggerSupport, TaggedLogging Classes: Context, Device, Formatter, LogEntry, Logger, TagFormatter, Tags, Template

Constant Summary collapse

LINE_SEPARATOR =
(RbConfig::CONFIG['host_os'].match(/mswin/i) ? "\r\n" : "\n")

Class Method Summary collapse

Class Method Details

.contextObject

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lumberjack.rb', line 57

def context
  current_context = Thread.current[:lumberjack_context]
  if block_given?
    Thread.current[:lumberjack_context] = Context.new(current_context)
    begin
      yield
    ensure
      Thread.current[:lumberjack_context] = current_context
    end
  else
    current_context || Context.new
  end
end

.context_tagsObject

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



72
73
74
75
# File 'lib/lumberjack.rb', line 72

def context_tags
  context = Thread.current[:lumberjack_context]
  context.tags if context
end

.tag(tags) ⇒ Object

Set tags on the current context



78
79
80
81
# File 'lib/lumberjack.rb', line 78

def tag(tags)
  context = Thread.current[:lumberjack_context]
  context.tag(tags) if context
end

.unit_of_work(id = nil) ⇒ Object

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.



36
37
38
39
40
41
42
# File 'lib/lumberjack.rb', line 36

def unit_of_work(id = nil)
  id ||= SecureRandom.hex(6)
  context do
    context[:unit_of_work_id] = id
    yield
  end
end

.unit_of_work_idObject

Get the UniqueIdentifier for the current unit of work.



45
46
47
# File 'lib/lumberjack.rb', line 45

def unit_of_work_id
  context[:unit_of_work_id]
end