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



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

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=



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

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



157
158
159
# File 'lib/semantic_logger/base.rb', line 157

def payload
  Thread.current[:semantic_logger_payload]
end

#pop_tags(quantity = 1) ⇒ Object

Remove specified number of tags from the current tag list



127
128
129
130
# File 'lib/semantic_logger/base.rb', line 127

def pop_tags(quantity=1)
  t = Thread.current[:semantic_logger_tags]
  t.pop(quantity) unless t.nil?
end

#push_tags(*tags) ⇒ Object

Add tags to the current scope



119
120
121
122
123
124
# File 'lib/semantic_logger/base.rb', line 119

def push_tags *tags
  # Need to flatten and reject empties to support calls from Rails 4
  new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
  t = Thread.current[:semantic_logger_tags]
  Thread.current[:semantic_logger_tags] = t.nil? ? new_tags : t.concat(new_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


100
101
102
103
104
105
# File 'lib/semantic_logger/base.rb', line 100

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

#tagsObject

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



112
113
114
115
116
# File 'lib/semantic_logger/base.rb', line 112

def tags
  # Since tags are stored on a per thread basis this list is thread-safe
  t = Thread.current[:semantic_logger_tags]
  t.nil? ? [] : t.clone
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


146
147
148
149
150
151
152
# File 'lib/semantic_logger/base.rb', line 146

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