Class: Logtail::Logger
- Inherits:
-
Logger
- Object
- Logger
- Logtail::Logger
- Defined in:
- lib/logtail/logger.rb
Overview
The Logtail Logger behaves exactly like the standard Ruby ‘::Logger`, except that it supports a transparent API for logging structured data and events.
Defined Under Namespace
Classes: AugmentedFormatter, Formatter, JSONFormatter, MessageOnlyFormatter, PassThroughFormatter
Instance Method Summary collapse
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Patch to ensure that the #level method is used instead of ‘@level`.
-
#formatter=(value) ⇒ Object
Sets a new formatted on the logger.
-
#initialize(*io_devices_and_loggers) ⇒ Logger
constructor
Creates a new Logtail::Logger instance where the passed argument is an IO device.
- #level=(value) ⇒ Object
- #with_context(context, &block) ⇒ Object
Constructor Details
#initialize(*io_devices_and_loggers) ⇒ Logger
Creates a new Logtail::Logger instance where the passed argument is an IO device. That is, anything that responds to ‘#write` and `#close`.
Note, this method does not accept the same arguments as the standard Ruby ‘::Logger`. The Ruby `::Logger` accepts additional options controlling file rotation if the first argument is a file name. This is a design flaw that Logtail does not assume. Logging to a file, or multiple IO devices is demonstrated in the examples below.
149 150 151 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/logtail/logger.rb', line 149 def initialize(*io_devices_and_loggers) if io_devices_and_loggers.size == 0 raise ArgumentError.new("At least one IO device or Logger must be provided when " + "instantiating a Logtail::Logger. Ex: Logtail::Logger.new(STDOUT).") end @extra_loggers = io_devices_and_loggers[1..-1].collect do |obj| if is_a_logger?(obj) obj else self.class.new(obj) end end io_device = io_devices_and_loggers[0] super(io_device) # Ensure we sync STDOUT to avoid buffering if io_device.respond_to?(:"sync=") io_device.sync = true end # Set the default formatter. The formatter cannot be set during # initialization, and can be changed with #formatter=. if io_device.is_a?(LogDevices::HTTP) self.formatter = PassThroughFormatter.new elsif Config.instance.development? || Config.instance.test? self.formatter = MessageOnlyFormatter.new else self.formatter = JSONFormatter.new end self.level = environment_level after_initialize if respond_to?(:after_initialize) Logtail::Config.instance.debug { "Logtail::Logger instantiated, level: #{level}, formatter: #{formatter.class}" } @initialized = true self.warn("The 'logtail-ruby' package has been deprecated. Please, switch to 'logtail' https://rubygems.org/gems/logtail") end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Patch to ensure that the #level method is used instead of ‘@level`. This is required because of Rails’ monkey patching on Logger via ‘::LoggerSilence`.
220 221 222 223 224 225 226 227 228 |
# File 'lib/logtail/logger.rb', line 220 def add(severity, = nil, progname = nil, &block) return true if @logdev.nil? || (severity || UNKNOWN) < level @extra_loggers.each do |logger| logger.add(severity, , progname, &block) end super end |
#formatter=(value) ⇒ Object
The formatter cannot be changed if you are using the HTTP logger backend.
Sets a new formatted on the logger.
196 197 198 199 200 201 202 203 204 |
# File 'lib/logtail/logger.rb', line 196 def formatter=(value) if @initialized && @logdev && @logdev.dev.is_a?(Logtail::LogDevices::HTTP) && !value.is_a?(PassThroughFormatter) raise ArgumentError.new("The formatter cannot be changed when using the " + "Logtail::LogDevices::HTTP log device. The PassThroughFormatter must be used for proper " + "delivery.") end super end |
#level=(value) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/logtail/logger.rb', line 206 def level=(value) if value.is_a?(Symbol) value = level_from_symbol(value) end super end |
#with_context(context, &block) ⇒ Object
214 215 216 |
# File 'lib/logtail/logger.rb', line 214 def with_context(context, &block) Logtail::CurrentContext.with(context, &block) end |