Module: Lumberjack

Defined in:
lib/lumberjack.rb,
lib/lumberjack/rack.rb,
lib/lumberjack/tags.rb,
lib/lumberjack/utils.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/tag_context.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/tagged_message.rb,
lib/lumberjack/formatter/round_formatter.rb,
lib/lumberjack/formatter/strip_formatter.rb,
lib/lumberjack/formatter/object_formatter.rb,
lib/lumberjack/formatter/redact_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/multiply_formatter.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, Utils Classes: Context, Device, Formatter, LogEntry, Logger, TagContext, 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. The context will apply to all Lumberjack loggers that are used within 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:



71
72
73
74
75
76
77
78
# File 'lib/lumberjack.rb', line 71

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)


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

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)


104
105
106
107
# File 'lib/lumberjack.rb', line 104

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.



113
114
115
116
# File 'lib/lumberjack.rb', line 113

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

.unit_of_work(id = nil) ⇒ void

Deprecated.

Use tags instead. This will be removed in version 2.0.

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.



42
43
44
45
46
47
48
49
50
# File 'lib/lumberjack.rb', line 42

def unit_of_work(id = nil)
  Lumberjack::Utils.deprecated("Lumberjack.unit_of_work", "Lumberjack.unit_of_work will be removed in version 2.0. Use Lumberjack::Logger#tag(unit_of_work: id) instead.") do
    id ||= SecureRandom.hex(6)
    context do
      context[:unit_of_work_id] = id
      yield
    end
  end
end

.unit_of_work_idString?

Deprecated.

Use tags instead. This will be removed in version 2.0.

Get the UniqueIdentifier for the current unit of work.

Returns:

  • (String, nil)

    The id for the current unit of work.



56
57
58
# File 'lib/lumberjack.rb', line 56

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.



84
85
86
87
88
89
90
91
92
# File 'lib/lumberjack.rb', line 84

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