Class: Timber::Logger
- Inherits:
-
Logger
- Object
- Logger
- Timber::Logger
- Includes:
- ActiveSupport::LoggerThreadSafeLevel, LoggerSilence
- Defined in:
- lib/timber/logger.rb
Overview
The Timber 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, 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) ⇒ Logger
constructor
Creates a new Timber::Logger instance where the passed argument is an IO device.
- #level=(value) ⇒ Object
Constructor Details
#initialize(*io_devices) ⇒ Logger
Creates a new Timber::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 Timber does not assume. Logging to a file, or multiple IO devices is demonstrated in the examples below.
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 |
# File 'lib/timber/logger.rb', line 162 def initialize(*io_devices) if io_devices.size == 0 raise ArgumentError.new("At least one IO device must be provided when instantiating " + "a Timber::Logger. Ex: Timber::Logger.new(STDOUT).") end @extra_loggers = io_devices[1..-1].collect { |io_device| self.class.new(io_device) } io_device = io_devices[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 = AugmentedFormatter.new end self.level = environment_level after_initialize if respond_to?(:after_initialize) Timber::Config.instance.debug { "Timber::Logger instantiated, level: #{level}, formatter: #{formatter.class}" } @initialized = true 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`.
224 225 226 227 228 229 230 231 232 |
# File 'lib/timber/logger.rb', line 224 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.
200 201 202 203 204 205 206 207 208 |
# File 'lib/timber/logger.rb', line 200 def formatter=(value) if @initialized && @logdev && @logdev.dev.is_a?(Timber::LogDevices::HTTP) && !value.is_a?(PassThroughFormatter) raise ArgumentError.new("The formatter cannot be changed when using the " + "Timber::LogDevices::HTTP log device. The PassThroughFormatter must be used for proper " + "delivery.") end super end |
#level=(value) ⇒ Object
210 211 212 213 214 215 |
# File 'lib/timber/logger.rb', line 210 def level=(value) if value.is_a?(Symbol) value = level_from_symbol(value) end super end |