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"] =~ /mswin/i ? "\r\n" : "\n")

Class Method Summary collapse

Class Method Details

.context(&block) ⇒ Object

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.



56
57
58
59
60
61
62
63
# 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?Boolean

Return true if inside a context block.

Returns:

  • (Boolean)


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

def context?
  !!Thread.current[:lumberjack_context]
end

.context_tagsObject

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



82
83
84
85
# File 'lib/lumberjack.rb', line 82

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

.tag(tags) ⇒ Object

Set tags on the current context



88
89
90
91
# File 'lib/lumberjack.rb', line 88

def tag(tags)
  context = Thread.current[:lumberjack_context]
  context&.tag(tags)
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.



35
36
37
38
39
40
41
# 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_idObject

Get the UniqueIdentifier for the current unit of work.



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

def unit_of_work_id
  context[:unit_of_work_id]
end

.use_context(context, &block) ⇒ Object

Set the context to use within a block.



66
67
68
69
70
71
72
73
74
# 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