Class: Appsignal::Logger
Overview
Logger that flushes logs to the AppSignal logging service.
Constant Summary collapse
- PLAINTEXT =
0
- LOGFMT =
1
- JSON =
2
- SEVERITY_MAP =
{ DEBUG => 2, INFO => 3, WARN => 5, ERROR => 6, FATAL => 7 }.freeze
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
Instance Method Summary collapse
-
#add(severity, message = nil, group = nil) ⇒ Object
(also: #log)
private
We support the various methods in the Ruby logger class by supplying this method.
- #broadcast_to(logger) ⇒ Object
-
#debug(message = nil, attributes = {}) ⇒ void
Log a debug level message.
-
#error(message = nil, attributes = {}) ⇒ void
Log an error level message.
-
#fatal(message = nil, attributes = {}) ⇒ void
Log a fatal level message.
-
#formatter=(formatter) ⇒ Object
When a formatter is set on the logger (e.g. when wrapping the logger in ‘ActiveSupport::TaggedLogging`) we want to set that formatter on all the loggers that are being broadcasted to.
-
#info(message = nil, attributes = {}) ⇒ void
Log an info level message.
-
#initialize(group, level: INFO, format: PLAINTEXT, attributes: {}) ⇒ void
constructor
Create a new logger instance.
-
#silence(severity = ERROR, &block) ⇒ Object
When using ActiveSupport::TaggedLogging without the broadcast feature, the passed logger is required to respond to the ‘silence` method.
-
#warn(message = nil, attributes = {}) ⇒ void
Log a warn level message.
Constructor Details
#initialize(group, level: INFO, format: PLAINTEXT, attributes: {}) ⇒ void
Create a new logger instance
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/appsignal/logger.rb', line 32 def initialize(group, level: INFO, format: PLAINTEXT, attributes: {}) raise TypeError, "group must be a string" unless group.is_a? String @group = group @level = level @format = format @mutex = Mutex.new @default_attributes = attributes @appsignal_attributes = {} @loggers = [] end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
23 24 25 |
# File 'lib/appsignal/logger.rb', line 23 def level @level end |
Instance Method Details
#add(severity, message = nil, group = nil) ⇒ Object Also known as: log
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
We support the various methods in the Ruby logger class by supplying this method.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/appsignal/logger.rb', line 55 def add(severity, = nil, group = nil) severity ||= UNKNOWN return true if severity < level group = @group if group.nil? if .nil? if block_given? = yield else = group group = @group end end return if .nil? @loggers.each do |logger| logger.add(severity, , group) rescue nil end unless .is_a?(String) Appsignal.internal_logger.warn( "Logger message was ignored, because it was not a String: #{.inspect}" ) return end = formatter.call(severity, Time.now, group, ) if formatter Appsignal::Extension.log( group, SEVERITY_MAP.fetch(severity, 0), @format, , Appsignal::Utils::Data.generate(appsignal_attributes) ) end |
#broadcast_to(logger) ⇒ Object
177 178 179 |
# File 'lib/appsignal/logger.rb', line 177 def broadcast_to(logger) @loggers << logger end |
#debug(message = nil, attributes = {}) ⇒ void
This method returns an undefined value.
Log a debug level message
99 100 101 102 103 104 105 106 |
# File 'lib/appsignal/logger.rb', line 99 def debug( = nil, attributes = {}) return if level > DEBUG = yield if .nil? && block_given? return if .nil? add_with_attributes(DEBUG, , @group, attributes) end |
#error(message = nil, attributes = {}) ⇒ void
This method returns an undefined value.
Log an error level message
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/appsignal/logger.rb', line 138 def error( = nil, attributes = {}) return if level > ERROR = yield if .nil? && block_given? return if .nil? = "#{.class}: #{.}" if .is_a?(Exception) add_with_attributes(ERROR, , @group, attributes) end |
#fatal(message = nil, attributes = {}) ⇒ void
This method returns an undefined value.
Log a fatal level message
153 154 155 156 157 158 159 160 |
# File 'lib/appsignal/logger.rb', line 153 def fatal( = nil, attributes = {}) return if level > FATAL = yield if .nil? && block_given? return if .nil? add_with_attributes(FATAL, , @group, attributes) end |
#formatter=(formatter) ⇒ Object
When a formatter is set on the logger (e.g. when wrapping the logger in ‘ActiveSupport::TaggedLogging`) we want to set that formatter on all the loggers that are being broadcasted to.
47 48 49 50 |
# File 'lib/appsignal/logger.rb', line 47 def formatter=(formatter) super @loggers.each { |logger| logger.formatter = formatter } end |
#info(message = nil, attributes = {}) ⇒ void
This method returns an undefined value.
Log an info level message
112 113 114 115 116 117 118 119 |
# File 'lib/appsignal/logger.rb', line 112 def info( = nil, attributes = {}) return if level > INFO = yield if .nil? && block_given? return if .nil? add_with_attributes(INFO, , @group, attributes) end |
#silence(severity = ERROR, &block) ⇒ Object
When using ActiveSupport::TaggedLogging without the broadcast feature, the passed logger is required to respond to the ‘silence` method.
Reference links:
169 170 171 172 173 174 175 |
# File 'lib/appsignal/logger.rb', line 169 def silence(severity = ERROR, &block) previous_level = @level @level = severity block.call(self) ensure @level = previous_level end |
#warn(message = nil, attributes = {}) ⇒ void
This method returns an undefined value.
Log a warn level message
125 126 127 128 129 130 131 132 |
# File 'lib/appsignal/logger.rb', line 125 def warn( = nil, attributes = {}) return if level > WARN = yield if .nil? && block_given? return if .nil? add_with_attributes(WARN, , @group, attributes) end |