Module: Lumberjack

Defined in:
lib/lumberjack.rb,
lib/lumberjack/rack.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/truncate_formatter.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

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) ⇒ Lumberjack::Context

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.

Returns:



64
65
66
67
68
69
70
71
# File 'lib/lumberjack.rb', line 64

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)


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

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

.context_tagsHash?

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

Returns:

  • (Hash, nil)


97
98
99
100
# File 'lib/lumberjack.rb', line 97

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

.tag(tags) ⇒ void

This method returns an undefined value.

Set tags on the current context

Parameters:

  • tags (Hash)

    The tags to set.



106
107
108
109
# File 'lib/lumberjack.rb', line 106

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

.unit_of_work(id = nil) ⇒ void

This method returns an undefined value.

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.

Parameters:

  • id (String) (defaults to: nil)

    The id for the unit of work.



39
40
41
42
43
44
45
# File 'lib/lumberjack.rb', line 39

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

.unit_of_work_idString?

Get the UniqueIdentifier for the current unit of work.

Returns:

  • (String, nil)

    The id for the current unit of work.



50
51
52
# File 'lib/lumberjack.rb', line 50

def unit_of_work_id
  context[:unit_of_work_id]
end

.use_context(context, &block) ⇒ Object

Set the context to use within a block.

Parameters:

Returns:

  • (Object)

    The result of the block.



77
78
79
80
81
82
83
84
85
# File 'lib/lumberjack.rb', line 77

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