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.
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 192 193 194 195 196 197 |
# File 'lib/logtail/logger.rb', line 155 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 at_exit { self.close } 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`.
226 227 228 229 230 231 232 233 234 |
# File 'lib/logtail/logger.rb', line 226 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.
202 203 204 205 206 207 208 209 210 |
# File 'lib/logtail/logger.rb', line 202 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
212 213 214 215 216 217 |
# File 'lib/logtail/logger.rb', line 212 def level=(value) if value.is_a?(Symbol) value = level_from_symbol(value) end super end |
#with_context(context, &block) ⇒ Object
220 221 222 |
# File 'lib/logtail/logger.rb', line 220 def with_context(context, &block) Logtail::CurrentContext.with(context, &block) end |