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

Constant Summary collapse

@@default_level =

Initial default Level for all new instances of SemanticLogger::Logger

:info

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

Returns the global default log level for new Logger instances



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

def self.default_level
  @@default_level
end

.default_level=(level) ⇒ Object

Allow for setting the global default log level This change only applies to new loggers, existing logger levels will not be changed in any way



213
214
215
# File 'lib/semantic_logger/base.rb', line 213

def self.default_level=(level)
  @@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



195
196
197
# File 'lib/semantic_logger/base.rb', line 195

def payload
  Thread.current[:semantic_logger_payload]
end

#tagsObject

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



166
167
168
# File 'lib/semantic_logger/base.rb', line 166

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


184
185
186
187
188
189
190
# File 'lib/semantic_logger/base.rb', line 184

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

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

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



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

def with_tags(*tags)
  current_tags = self.tags
  # Check for nil tags
  if tags
    Thread.current[:semantic_logger_tags] = current_tags ? current_tags + tags : tags
  end
  yield
ensure
  Thread.current[:semantic_logger_tags] = current_tags
end