Module: Logging::NestedDiagnosticContext

Extended by:
NestedDiagnosticContext
Included in:
NestedDiagnosticContext
Defined in:
lib/logging/diagnostic_context.rb

Overview

A Nested Diagnostic Context, or NDC in short, is an instrument to distinguish interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.

Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. This is where NDCs come into play.

The NDC is a stack of contextual messages that are pushed and popped by the client as different contexts are encountered in the application. When a new context is entered, the client will ‘push` a new message onto the NDC stack. This message appears in all log messages. When this context is exited, the client will call `pop` to remove the message.

  • Contexts can be nested

  • When entering a context, call ‘Logging.ndc.push`

  • When leaving a context, call ‘Logging.ndc.pop`

  • Configure the PatternLayout to log context information

There is no penalty for forgetting to match each push operation with a corresponding pop, except the obvious mismatch between the real application context and the context set in the NDC.

When configured to do so, PatternLayout instance will automatically retrieve the nested diagnostic context for the current thread with out any user intervention. This context information can be used to track user sessions in a Rails application, for example.

Note that NDCs are managed on a per thread basis. NDC operations such as ‘push`, `pop`, and `clear` affect the NDC of the current thread only. NDCs of other threads remain unaffected.

By default, when a new thread is created it will inherit the context of its parent thread. However, the ‘inherit` method may be used to inherit context for any other thread in the application.

Constant Summary collapse

NAME =

The name used to retrieve the NDC from thread-local storage.

:logging_nested_diagnostic_context

Instance Method Summary collapse

Instance Method Details

#clearObject

Public: Clear all nested diagnostic information if any. This method is useful in cases where the same thread can be potentially used over and over in different unrelated contexts.

Returns the NestedDiagnosticContext.



332
333
334
335
# File 'lib/logging/diagnostic_context.rb', line 332

def clear
  Thread.current.thread_variable_set(NAME, nil)
  self
end

#contextObject

Returns the Array acting as the storage stack for this NestedDiagnosticContext. A new storage Array is created for each Thread running in the application.



363
364
365
366
367
368
369
370
# File 'lib/logging/diagnostic_context.rb', line 363

def context
  c = Thread.current.thread_variable_get(NAME)
  if c.nil?
    c = Array.new
    Thread.current.thread_variable_set(NAME, c)
  end
  return c
end

#inherit(obj) ⇒ Object

Public: Inherit the diagnostic context of another thread. In the vast majority of cases the other thread will the parent that spawned the current thread. The diagnostic context from the parent thread is cloned before being inherited; the two diagnostic contexts can be changed independently.

Returns the NestedDiagnosticContext.



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/logging/diagnostic_context.rb', line 345

def inherit( obj )
  case obj
  when Array
    Thread.current.thread_variable_set(NAME, obj.dup)
  when Thread
    return if Thread.current == obj
    DIAGNOSTIC_MUTEX.synchronize do
      Thread.current.thread_variable_set(NAME, obj.thread_variable_get(NAME).dup) if obj.thread_variable_get(NAME)
    end
  end

  self
end

#peekObject

Public: Looks at the last diagnostic context at the top of this NDC without removing it. The returned value is the last pushed message. If no context is available then ‘nil` is returned.

Returns the last pushed diagnostic message String or nil if no messages exist.



322
323
324
# File 'lib/logging/diagnostic_context.rb', line 322

def peek
  context.last
end

#popObject

Public: Clients should call this method before leaving a diagnostic context. The returned value is the last pushed message. If no context is available then ‘nil` is returned.

Returns the last pushed diagnostic message String or nil if no messages exist.



311
312
313
# File 'lib/logging/diagnostic_context.rb', line 311

def pop
  context.pop
end

#push(message) ⇒ Object Also known as: <<

Public: Push new diagnostic context information for the current thread. The contents of the message parameter is determined solely by the client.

message - The message String to add to the current context.

Returns the current NestedDiagnosticContext.



291
292
293
294
295
296
297
298
299
300
301
# File 'lib/logging/diagnostic_context.rb', line 291

def push( message )
  context.push(message)
  if block_given?
    begin
      yield
    ensure
      context.pop
    end
  end
  self
end