Class: SemanticLogger::Base
- Inherits:
-
Object
- Object
- SemanticLogger::Base
- Defined in:
- lib/semantic_logger/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#filter ⇒ Object
Class name to be logged.
-
#instance_named_tags ⇒ Object
readonly
Tags permanently bound to this logger instance via
#tagged(without a block). -
#instance_tags ⇒ Object
readonly
Tags permanently bound to this logger instance via
#tagged(without a block). -
#name ⇒ Object
Class name to be logged.
Instance Method Summary collapse
-
#backtrace(thread: Thread.current, level: :warn, message: "Backtrace:", payload: nil, metric: nil, metric_amount: nil) ⇒ Object
Log a thread backtrace.
-
#benchmark(level, message, params = {}) ⇒ Object
Deprecated: use measure, which takes the same arguments.
-
#fast_tag(tag) ⇒ Object
:nodoc:.
-
#level ⇒ Object
Returns the level set by an enclosing
#with_levelblock on the current fiber, otherwise the level set for this instance, otherwise its fallback level (see #fallback_level). -
#level=(level) ⇒ Object
Set the logging level for this logger.
-
#log(_log_) ⇒ Object
Write log data to underlying data storage.
-
#measure(level, message, params = {}, &block) ⇒ Object
Dynamically supply the log level with every measurement call.
- #named_tags ⇒ Object
-
#pop_tags(quantity = 1) ⇒ Object
:nodoc:.
-
#push_tags(*tags) ⇒ Object
Returns the list of tags pushed after flattening them out and removing blanks.
-
#should_log?(log) ⇒ Boolean
Whether this log entry meets the criteria to be logged by this appender.
-
#silence(new_level = :error) ⇒ Object
:nodoc:.
-
#tagged(*tags) ⇒ Object
(also: #with_tags)
Add the tags or named tags to the list of tags to log for this thread whilst the supplied block is active.
-
#tags ⇒ Object
:nodoc:.
-
#with_level(level) ⇒ Object
Change the log level of this instance while the block runs, on the current fiber only.
Instance Attribute Details
#filter ⇒ Object
Class name to be logged
10 11 12 |
# File 'lib/semantic_logger/base.rb', line 10 def filter @filter end |
#instance_named_tags ⇒ Object
Tags permanently bound to this logger instance via #tagged (without a block).
Empty for a regular (non-child) logger.
245 246 247 |
# File 'lib/semantic_logger/base.rb', line 245 def @instance_named_tags end |
#instance_tags ⇒ Object
Tags permanently bound to this logger instance via #tagged (without a block).
Empty for a regular (non-child) logger.
245 246 247 |
# File 'lib/semantic_logger/base.rb', line 245 def @instance_tags end |
#name ⇒ Object
Class name to be logged
10 11 12 |
# File 'lib/semantic_logger/base.rb', line 10 def name @name end |
Instance Method Details
#backtrace(thread: Thread.current, level: :warn, message: "Backtrace:", payload: nil, metric: nil, metric_amount: nil) ⇒ Object
Log a thread backtrace
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/semantic_logger/base.rb', line 152 def backtrace(thread: Thread.current, level: :warn, message: "Backtrace:", payload: nil, metric: nil, metric_amount: nil) # The entry has the name, tags, and named tags of the thread whose backtrace it logs. log = new_log(level, nil, thread) return false unless meets_log_level?(log) backtrace = thread == Thread.current ? Utils.extract_backtrace(caller) : thread.backtrace if backtrace += "\n" << backtrace.join("\n") end if log.assign(message: , backtrace: backtrace, payload: payload, metric: metric, metric_amount: metric_amount) && !filtered?(log) self.log(log) else false end end |
#benchmark(level, message, params = {}) ⇒ Object
Deprecated: use measure, which takes the same arguments. Removed in v6.
144 145 146 147 148 149 |
# File 'lib/semantic_logger/base.rb', line 144 def benchmark(level, , params = {}, &) SemanticLogger::Deprecation.warn( "benchmark is deprecated and removed in v6. Use measure, which takes the same arguments." ) measure(level, , params, &) end |
#fast_tag(tag) ⇒ Object
:nodoc:
270 271 272 |
# File 'lib/semantic_logger/base.rb', line 270 def fast_tag(tag, &) SemanticLogger.fast_tag(tag, &) end |
#level ⇒ Object
Returns the level set by an enclosing #with_level block on the current fiber,
otherwise the level set for this instance, otherwise its fallback level (see #fallback_level).
33 34 35 36 37 38 |
# File 'lib/semantic_logger/base.rb', line 33 def level index = @with_level_used && with_level_index return Levels.level(index) if index @level || fallback_level end |
#level=(level) ⇒ Object
Set the logging level for this logger
Note: This level is only for this particular instance. It does not override the log level in any logging instance or the default log level SemanticLogger.default_level
Must be one of the values in SemanticLogger::LEVELS, or nil to use this instance's fallback level (see #fallback_level).
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/semantic_logger/base.rb', line 20 def level=(level) if level.nil? # Use the fallback level for this instance (see #fallback_level) @level_index = nil @level = nil else @level_index = Levels.index(level) @level = Levels.level(@level_index) end end |
#log(_log_) ⇒ Object
Write log data to underlying data storage
275 276 277 |
# File 'lib/semantic_logger/base.rb', line 275 def log(_log_) raise NotImplementedError, "Logging Appender must implement #log(log)" end |
#measure(level, message, params = {}, &block) ⇒ Object
Dynamically supply the log level with every measurement call
134 135 136 137 138 139 140 141 |
# File 'lib/semantic_logger/base.rb', line 134 def measure(level, , params = {}, &block) index = Levels.index(level) if level_index <= index measure_internal(level, index, , params, &block) elsif block yield(params) end end |
#named_tags ⇒ Object
239 240 241 |
# File 'lib/semantic_logger/base.rb', line 239 def SemanticLogger. end |
#pop_tags(quantity = 1) ⇒ Object
:nodoc:
260 261 262 |
# File 'lib/semantic_logger/base.rb', line 260 def (quantity = 1) SemanticLogger.(quantity) end |
#push_tags(*tags) ⇒ Object
Returns the list of tags pushed after flattening them out and removing blanks
Note:
- This method is slow since it needs to flatten the tags and remove empty elements to support Rails 4.
- For better performance with clean tags, use
SemanticLogger.push_tags
253 254 255 256 257 |
# File 'lib/semantic_logger/base.rb', line 253 def (*) # Need to flatten and reject empties to support calls from Rails 4 = .flatten.collect(&:to_s).reject(&:empty?) SemanticLogger.(*) end |
#should_log?(log) ⇒ Boolean
Whether this log entry meets the criteria to be logged by this appender.
280 281 282 |
# File 'lib/semantic_logger/base.rb', line 280 def should_log?(log) meets_log_level?(log) && !filtered?(log) end |
#silence(new_level = :error) ⇒ Object
:nodoc:
265 266 267 |
# File 'lib/semantic_logger/base.rb', line 265 def silence(new_level = :error, &) SemanticLogger.silence(new_level, &) end |
#tagged(*tags) ⇒ Object Also known as:
Add the tags or named tags to the list of tags to log for this thread whilst the supplied block is active.
Returns result of block.
Tagged example:
SemanticLogger.tagged(12345, 'jack') do
logger.debug('Hello World')
end
Named Tags (Hash) example:
SemanticLogger.tagged(tracking_number: 12345) do
logger.debug('Hello World')
end
Notes:
-
Named tags are the recommended approach since the tag consists of a name value pair this is more useful than just a string value in the logs, or centralized logging system.
-
This method is slow when using multiple text tags since it needs to flatten the tags and remove empty elements to support Rails 4.
-
It is recommended to keep tags as a list without any empty values, or contain any child arrays. However, this api will convert:
`logger.tagged([['first', nil], nil, ['more'], 'other'])`to:
`logger.tagged('first', 'more', 'other')` -
For better performance with clean tags, see
SemanticLogger.tagged.
Child logger:
- When called without a block, returns a new logger instance that permanently
carries the supplied tags ("instance tags"). Those tags are added to every log
entry emitted by the returned logger, and only that logger, even across threads.
Unlike the block form they are not pushed onto the thread, so other loggers are
unaffected. This is the recommended way to bind a logger to an object's identity:
logger = SemanticLogger['Cart'].tagged(cart_id: cart.id)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/semantic_logger/base.rb', line 212 def tagged(*) # No block: build a child logger that carries these tags on every entry. return tagged_child() unless block_given? block = -> { yield(self) } # Allow named tags to be passed into the logger # Rails::Rack::Logger passes logs as an array with a single argument if .size == 1 && !.first.is_a?(Array) tag = [0] return yield if tag.nil? || tag == "" return tag.is_a?(Hash) ? SemanticLogger.named_tagged(tag, &block) : SemanticLogger.fast_tag(tag.to_s, &block) end # Need to flatten and reject empties to support calls from Rails 4 = .flatten.collect(&:to_s).reject(&:empty?) SemanticLogger.tagged(*, &block) end |
#tags ⇒ Object
:nodoc:
235 236 237 |
# File 'lib/semantic_logger/base.rb', line 235 def SemanticLogger. end |
#with_level(level) ⇒ Object
Change the log level of this instance while the block runs, on the current fiber only.
Other threads and fibers that share this instance keep logging at its own level.
The level set with #level= is left untouched, so an instance that follows the
global default level keeps following it after the block. Blocks can be nested.
Matches Ruby's Logger#with_level, which Sidekiq calls to apply a job's log_level.
Accepts the same levels as #level=. Returns the result of the block.
48 49 50 51 52 |
# File 'lib/semantic_logger/base.rb', line 48 def with_level(level, &) index = Levels.index(level) @with_level_used = true Context.with_level(self, index, &) end |