Class: SemanticLogger::Context

Inherits:
Data
  • Object
show all
Defined in:
lib/semantic_logger/context.rb,
lib/semantic_logger/context.rb

Overview

The logging context of a fiber: its tags, named tags, and silence level.

An instance is a frozen snapshot of a fiber's context, returned by SemanticLogger.context, for SemanticLogger.with_context to apply in another thread or fiber. It has tags (an Array), named_tags (a Hash), and silence (the level Symbol set by SemanticLogger.silence, or nil).

The class methods are internal. They manage the context of the current fiber, together with the with_level overrides of its loggers, which are not part of a snapshot: like Ruby's Logger#with_level they apply only to the fiber that set them. Only this class knows where that state is kept: in fiber-local variables (Thread.current[]), so that every fiber, including the fibers of a fiber scheduler such as Async, has its own.

The stored lists are never changed in place. Each change stores a new frozen list, and the block forms restore the previous list when the block exits. Code that copies fiber-local variables into another thread, such as Rails' ActionController::Live, therefore hands over lists that Semantic Logger never changes for the other thread. The tags in them are the caller's own objects.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tags: EMPTY, named_tags: EMPTY_HASH, silence: nil) ⇒ Context

Parameters tags: [Array] Positional tags. named_tags: [Hash] Named tags. silence: [Symbol|String|Integer] A level accepted by SemanticLogger.silence, or nil for none.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
# File 'lib/semantic_logger/context.rb', line 36

def initialize(tags: EMPTY, named_tags: EMPTY_HASH, silence: nil)
  raise(ArgumentError, "tags must be an Array") unless tags.is_a?(Array)
  raise(ArgumentError, "named_tags must be a Hash") unless named_tags.is_a?(Hash)

  super(
    tags:       tags.frozen? ? tags : tags.dup.freeze,
    named_tags: named_tags.frozen? ? named_tags : named_tags.dup.freeze,
    silence:    silence.nil? ? nil : Levels.level(Levels.index(silence))
  )
end

Instance Attribute Details

#named_tags ⇒ Object (readonly)

Returns the value of attribute named_tags

Returns:

  • (Object) —

    the current value of named_tags



18
19
20
# File 'lib/semantic_logger/context.rb', line 18

def named_tags
  @named_tags
end

#silence ⇒ Object (readonly)

Returns the value of attribute silence

Returns:

  • (Object) —

    the current value of silence



18
19
20
# File 'lib/semantic_logger/context.rb', line 18

def silence
  @silence
end

#tags ⇒ Object (readonly)

Returns the value of attribute tags

Returns:

  • (Object) —

    the current value of tags



18
19
20
# File 'lib/semantic_logger/context.rb', line 18

def tags
  @tags
end

Class Method Details

.current ⇒ Object

Returns [Context] a frozen snapshot of the current fiber's context.



59
60
61
62
63
64
65
66
# File 'lib/semantic_logger/context.rb', line 59

def current
  tags  = Thread.current[TAGS_KEY]
  named = Thread.current[NAMED_TAGS_KEY]
  index = Thread.current[SILENCE_KEY]
  return NONE if (tags.nil? || tags.empty?) && (named.nil? || named.empty?) && index.nil?

  new(tags: tags || EMPTY, named_tags: merge(named).freeze, silence: index && Levels.level(index))
end

.fast_tag(tag) ⇒ Object

Runs the block with the tag added to the current fiber's tags.

Like every block form below, it restores the previous value when the block exits, whatever the block did to it. Each is written out in full, since this is on the hot path. Each reads the previous value before its begin, so that an exception raised into the thread, such as by Timeout, can never restore a value that was not read yet.



103
104
105
106
107
108
109
110
111
# File 'lib/semantic_logger/context.rb', line 103

def fast_tag(tag)
  previous = Thread.current[TAGS_KEY]
  begin
    Thread.current[TAGS_KEY] = (previous ? previous.dup << tag : [tag]).freeze
    yield
  ensure
    Thread.current[TAGS_KEY] = previous
  end
end

.named_tagged(hash) ⇒ Object

Runs the block with the Hash of named tags added to the current fiber's named tags.



137
138
139
140
141
142
143
144
145
# File 'lib/semantic_logger/context.rb', line 137

def named_tagged(hash)
  previous = Thread.current[NAMED_TAGS_KEY]
  begin
    Thread.current[NAMED_TAGS_KEY] = (previous ? previous.dup << hash : [hash]).freeze
    yield
  ensure
    Thread.current[NAMED_TAGS_KEY] = previous
  end
end

.named_tags(thread = Thread.current) ⇒ Object

Returns [Hash] a new Hash of the named tags of the thread's current fiber.

Each named tags block pushes its Hash onto a list; a later Hash wins on a key that is in both.



93
94
95
# File 'lib/semantic_logger/context.rb', line 93

def named_tags(thread = Thread.current)
  merge(thread[NAMED_TAGS_KEY])
end

.pop_named_tags(quantity) ⇒ Object

Removes the last quantity Hashes of named tags pushed onto the current fiber. Returns [Array] the Hashes removed, or nil when the fiber has no named tags.



154
155
156
# File 'lib/semantic_logger/context.rb', line 154

def pop_named_tags(quantity)
  pop(NAMED_TAGS_KEY, quantity)
end

.pop_tags(quantity) ⇒ Object

Removes the last quantity tags from the current fiber's tags. Returns [Array] the tags removed, or nil when the fiber has no tags.



132
133
134
# File 'lib/semantic_logger/context.rb', line 132

def pop_tags(quantity)
  pop(TAGS_KEY, quantity)
end

.push_named_tags(hash) ⇒ Object

Adds the Hash of named tags to the current fiber's named tags, until it is popped.



148
149
150
# File 'lib/semantic_logger/context.rb', line 148

def push_named_tags(hash)
  Thread.current[NAMED_TAGS_KEY] = (current_named_tags.dup << hash).freeze
end

.push_tags(tags) ⇒ Object

Adds the Array of tags to the current fiber's tags, until they are popped.



126
127
128
# File 'lib/semantic_logger/context.rb', line 126

def push_tags(tags)
  Thread.current[TAGS_KEY] = (current_tags + tags).freeze
end

.silence(index) ⇒ Object

Runs the block with the supplied level index as the current fiber's silence level.



165
166
167
168
169
170
171
172
173
# File 'lib/semantic_logger/context.rb', line 165

def silence(index)
  previous = Thread.current[SILENCE_KEY]
  begin
    Thread.current[SILENCE_KEY] = index
    yield
  ensure
    Thread.current[SILENCE_KEY] = previous
  end
end

.silence_index ⇒ Object

Returns [Integer|nil] the level index set by an enclosing silence block on the current fiber, or nil when there is none.



160
161
162
# File 'lib/semantic_logger/context.rb', line 160

def silence_index
  Thread.current[SILENCE_KEY]
end

.tagged(tags) ⇒ Object

Runs the block with the Array of tags added to the current fiber's tags. Takes ownership of the Array, which it may freeze, so the caller must not keep it.



115
116
117
118
119
120
121
122
123
# File 'lib/semantic_logger/context.rb', line 115

def tagged(tags)
  previous = Thread.current[TAGS_KEY]
  begin
    Thread.current[TAGS_KEY] = (previous ? previous + tags : tags).freeze
    yield
  ensure
    Thread.current[TAGS_KEY] = previous
  end
end

.tags(thread = Thread.current) ⇒ Object

Returns [Array] a new Array of the tags of the thread's current fiber.



85
86
87
88
# File 'lib/semantic_logger/context.rb', line 85

def tags(thread = Thread.current)
  tags = thread[TAGS_KEY]
  tags ? tags.dup : []
end

.use(context) ⇒ Object

Runs the block with the supplied context as the current fiber's context, in place of the fiber's own tags, named tags, and silence level, which are restored when the block exits.

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/semantic_logger/context.rb', line 70

def use(context)
  raise(ArgumentError, "Expected a SemanticLogger::Context, got #{context.class}") unless context.is_a?(Context)

  previous = [Thread.current[TAGS_KEY], Thread.current[NAMED_TAGS_KEY], Thread.current[SILENCE_KEY]]
  begin
    Thread.current[TAGS_KEY]       = context.tags.empty? ? nil : context.tags
    Thread.current[NAMED_TAGS_KEY] = context.named_tags.empty? ? nil : [context.named_tags].freeze
    Thread.current[SILENCE_KEY]    = context.silence && Levels.index(context.silence)
    yield
  ensure
    Thread.current[TAGS_KEY], Thread.current[NAMED_TAGS_KEY], Thread.current[SILENCE_KEY] = previous
  end
end

.with_level(logger, index) ⇒ Object

Runs the block with the supplied level index for the logger on the current fiber.



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/semantic_logger/context.rb', line 182

def with_level(logger, index)
  previous = Thread.current[WITH_LEVEL_KEY]
  begin
    overrides                      = previous ? previous.dup : {}.compare_by_identity
    overrides[logger]              = index
    Thread.current[WITH_LEVEL_KEY] = overrides.freeze
    yield
  ensure
    Thread.current[WITH_LEVEL_KEY] = previous
  end
end

.with_level_index(logger) ⇒ Object

Returns [Integer|nil] the level index set for the logger by an enclosing with_level block on the current fiber, or nil when there is none.



177
178
179
# File 'lib/semantic_logger/context.rb', line 177

def with_level_index(logger)
  Thread.current[WITH_LEVEL_KEY]&.[](logger)
end

Instance Method Details

#with(**changes) ⇒ Object

Returns [Context] a copy with the supplied members changed, checked and frozen as by .new, which Data#with does not call on Ruby 3.2.



49
50
51
# File 'lib/semantic_logger/context.rb', line 49

def with(**changes)
  changes.empty? ? self : self.class.new(**to_h, **changes)
end