Class: SemanticLogger::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/base.rb

Direct Known Subclasses

Appender::Base, Logger

Defined Under Namespace

Classes: Log

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#levelObject

Returns the value of attribute level.



14
15
16
# File 'lib/semantic_logger/base.rb', line 14

def level
  @level
end

#nameObject

Class name to be logged



12
13
14
# File 'lib/semantic_logger/base.rb', line 12

def name
  @name
end

Class Method Details

.default_levelObject

DEPRECATED See SemanticLogger.default_level



224
225
226
227
# File 'lib/semantic_logger/base.rb', line 224

def self.default_level
  warn "[DEPRECATION] `SemanticLogger::Logger.default_level` is deprecated.  Please use `SemanticLogger.default_level` instead."
  SemanticLogger.default_level
end

.default_level=(level) ⇒ Object

DEPRECATED See SemanticLogger.default_level=



218
219
220
221
# File 'lib/semantic_logger/base.rb', line 218

def self.default_level=(level)
  warn "[DEPRECATION] `SemanticLogger::Logger.default_level=` is deprecated.  Please use `SemanticLogger.default_level=` instead."
  SemanticLogger.default_level = level
end

Instance Method Details

#payloadObject

Returns [Hash] payload to be added to every log entry in the current scope on this thread. Returns nil if no payload is currently set



205
206
207
# File 'lib/semantic_logger/base.rb', line 205

def payload
  Thread.current[:semantic_logger_payload]
end

#pop_tags(quantity = 1) ⇒ Object

Remove specified number of tags from the current tag list

To support: ActiveSupport::TaggedLogging V4 and above


176
177
178
# File 'lib/semantic_logger/base.rb', line 176

def pop_tags(quantity=1)
  tags.pop(quantity)
end

#push_tags(*tags) ⇒ Object

Add tags to the current scope

To support: ActiveSupport::TaggedLogging V4 and above


169
170
171
172
# File 'lib/semantic_logger/base.rb', line 169

def push_tags *tags
  # Check for nil tags
  Thread.current[:semantic_logger_tags] = self.tags.concat(tags)
end

#tagged(*tags) ⇒ Object Also known as: with_tags

Add the supplied tags to the list of tags to log for this thread whilst the supplied block is active Returns nil if no tags are currently set

To support: ActiveSupport::TaggedLogging V3 and above


151
152
153
154
155
156
# File 'lib/semantic_logger/base.rb', line 151

def tagged(*tags)
  push_tags(*tags)
  yield
ensure
  pop_tags(tags.size)
end

#tagsObject

Returns [Array] of [String] tags currently active for this thread Returns nil if no tags are set



163
164
165
# File 'lib/semantic_logger/base.rb', line 163

def tags
  Thread.current[:semantic_logger_tags] ||= []
end

#with_payload(payload) ⇒ Object

Thread specific context information to be logged with every log entry

Add a payload to all log calls on This Thread within the supplied block

logger.with_payload(:tracking_number=>12345) do
  logger.debug('Hello World')
end

If a log call already includes a pyload, this payload will be merged with the supplied payload, with the supplied payload taking precedence

logger.with_payload(:tracking_number=>12345) do
  logger.debug('Hello World', :result => 'blah')
end


194
195
196
197
198
199
200
# File 'lib/semantic_logger/base.rb', line 194

def with_payload(payload)
  current_payload = self.payload
  Thread.current[:semantic_logger_payload] = current_payload ? current_payload.merge(payload) : payload
  yield
ensure
  Thread.current[:semantic_logger_payload] = current_payload
end