Class: Appsignal::Logger
- Defined in:
- lib/appsignal/logger.rb,
lib/appsignal/logger/extension_backend.rb,
lib/appsignal/logger/opentelemetry_backend.rb,
sig/appsignal.rbs
Overview
Logger that flushes logs to the AppSignal logging service.
Instance Attribute Summary collapse
-
#level ⇒ Integer
readonly
Logging severity threshold.
Instance Method Summary collapse
-
#<<(message) ⇒ Integer
Log an info level message.
-
#broadcast_to(logger) ⇒ Array<Logger>
Adds a logger to broadcast log messages to.
-
#debug(message = nil, attributes = {}, &block) ⇒ void
Log a debug level message.
-
#error(message = nil, attributes = {}, &block) ⇒ void
Log an error level message.
-
#fatal(message = nil, attributes = {}, &block) ⇒ void
Log a fatal level message.
-
#formatter=(formatter) ⇒ Proc
Sets the formatter for this logger and all broadcasted loggers.
-
#info(message = nil, attributes = {}, &block) ⇒ void
Log an info level message.
-
#initialize(group, level: INFO, format: AUTODETECT, attributes: {}) ⇒ void
constructor
Create a new logger instance.
-
#silence(severity = ERROR, &block) ⇒ Object
Temporarily silences the logger to a specified level while executing a block.
-
#warn(message = nil, attributes = {}, &block) ⇒ void
Log a warn level message.
Constructor Details
#initialize(group, level: INFO, format: AUTODETECT, attributes: {}) ⇒ void
Create a new logger instance
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/appsignal/logger.rb', line 81 def initialize(group, level: INFO, format: AUTODETECT, attributes: {}) raise TypeError, "group must be a string" unless group.is_a? String @group = group @level = level @silenced = false @format = validated_format(format) @mutex = Mutex.new @default_attributes = attributes @appsignal_attributes = attributes @loggers = [] end |
Instance Attribute Details
#level ⇒ Integer (readonly)
Logging severity threshold
69 70 71 |
# File 'lib/appsignal/logger.rb', line 69 def level @level end |
Instance Method Details
#<<(message) ⇒ Integer
Log an info level message
Returns the number of characters written.
201 202 203 204 |
# File 'lib/appsignal/logger.rb', line 201 def <<() info() .length end |
#broadcast_to(logger) ⇒ Array<Logger>
Adds a logger to broadcast log messages to.
This implementation of the broadcasting logic exists here in the
AppSignal Ruby gem, because it doesn't work in Rails using tagged logging.
It would log a log line as many times as there are Rails.logger.tagged
calls wrapping it.
For example, this setup with one log line:
appsignal_logger = Appsignal::Logger.new("rails")
Rails.logger.broadcast_to(appsignal_logger)
Rails.logger.tagged("my tag") do
Rails.logger.tagged("my nested tag") do
Rails.logger.info("Nested log")
end
end
Is logged as the following to the AppSignal logger. Each combination of tags is sent separately.
Nested log
[my nested tag] Nested log
[my tag] Nested log
[my tag] [my nested tag] Nested log
Once it's fixed in Rails, it can be removed here.
Related issues and PRs:
- https://github.com/rails/rails/issues/46084
- https://github.com/rails/rails/issues/44668
- https://github.com/rails/rails/pull/53105
- https://github.com/rails/rails/pull/49771
Another issue was that the Rails .broadcast_to implementation in
ActiveSupport::BroadcastLogger will run the block passed to .tagged
as many times as there are loggers.
Very early in the Rails execution stack, a middleware runs
logger.tagged(...) { @app.call(env) }.
This would cause the request handler to run twice, and it also causes
the Rack response to be an array of Rack responses instead,
which breaks the app entirely.
Related issues:
283 284 285 |
# File 'lib/appsignal/logger.rb', line 283 def broadcast_to(logger) @loggers << logger end |
#debug(message = nil, attributes = {}, &block) ⇒ void
This method returns an undefined value.
Log a debug level message
159 160 161 |
# File 'lib/appsignal/logger.rb', line 159 def debug( = nil, attributes = {}, &block) add_with_attributes(DEBUG, , @group, attributes, &block) end |
#error(message = nil, attributes = {}, &block) ⇒ void
This method returns an undefined value.
Log an error level message
183 184 185 |
# File 'lib/appsignal/logger.rb', line 183 def error( = nil, attributes = {}, &block) add_with_attributes(ERROR, , @group, attributes, &block) end |
#fatal(message = nil, attributes = {}, &block) ⇒ void
This method returns an undefined value.
Log a fatal level message
191 192 193 |
# File 'lib/appsignal/logger.rb', line 191 def fatal( = nil, attributes = {}, &block) add_with_attributes(FATAL, , @group, attributes, &block) end |
#formatter=(formatter) ⇒ Proc
Sets the formatter for this logger and all broadcasted loggers.
98 99 100 101 102 103 |
# File 'lib/appsignal/logger.rb', line 98 def formatter=(formatter) super @loggers.each do |logger| logger.formatter = formatter if logger.respond_to?(:formatter=) end end |
#info(message = nil, attributes = {}, &block) ⇒ void
This method returns an undefined value.
Log an info level message
167 168 169 |
# File 'lib/appsignal/logger.rb', line 167 def info( = nil, attributes = {}, &block) add_with_attributes(INFO, , @group, attributes, &block) end |
#silence(severity = ERROR, &block) ⇒ Object
Temporarily silences the logger to a specified level while executing a block.
When using ActiveSupport::TaggedLogging without the broadcast feature,
the passed logger is required to respond to the silence method.
Reference links:
218 219 220 221 222 223 224 225 226 |
# File 'lib/appsignal/logger.rb', line 218 def silence(severity = ERROR, &block) previous_level = @level @level = severity @silenced = true block.call(self) ensure @level = previous_level @silenced = false end |
#warn(message = nil, attributes = {}, &block) ⇒ void
This method returns an undefined value.
Log a warn level message
175 176 177 |
# File 'lib/appsignal/logger.rb', line 175 def warn( = nil, attributes = {}, &block) add_with_attributes(WARN, , @group, attributes, &block) end |