Module: Logsly::Logging182::NestedDiagnosticContext

Extended by:
NestedDiagnosticContext
Included in:
NestedDiagnosticContext
Defined in:
lib/logsly/logging182/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 ‘Logsly::Logging182.ndc.push`

  • When leaving a context, call ‘Logsly::Logging182.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'.freeze

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.



201
202
203
204
# File 'lib/logsly/logging182/diagnostic_context.rb', line 201

def clear
  context.clear if Thread.current[NAME]
  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.



232
233
234
# File 'lib/logsly/logging182/diagnostic_context.rb', line 232

def context
  Thread.current[NAME] ||= Array.new
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.



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/logsly/logging182/diagnostic_context.rb', line 214

def inherit( obj )
  case obj
  when Array
    Thread.current[NAME] = obj.dup
  when Thread
    return if Thread.current == obj
    DIAGNOSTIC_MUTEX.synchronize {
      Thread.current[NAME] = obj[NAME].dup if obj[NAME]
    }
  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.



191
192
193
# File 'lib/logsly/logging182/diagnostic_context.rb', line 191

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.



180
181
182
# File 'lib/logsly/logging182/diagnostic_context.rb', line 180

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.



167
168
169
170
# File 'lib/logsly/logging182/diagnostic_context.rb', line 167

def push( message )
  context.push(message)
  self
end