Module: Loggr::Adapter

Included in:
LoggerFactory
Defined in:
lib/loggr/adapter.rb,
lib/loggr/adapter/nop.rb,
lib/loggr/adapter/base.rb,
lib/loggr/adapter/rails.rb,
lib/loggr/adapter/slf4j.rb,
lib/loggr/adapter/abstract.rb,
lib/loggr/adapter/buffered.rb

Overview

The factory is responsible for getting Logger instances from the adapters.

It delegates both the `logger` and `mdc` calls to the current adpater.

The adpater can be changed by setting `adpater = …`:

Loggr.adapter = Loggr::Adapter::SLF4J

See Also:

  • #mdc

Defined Under Namespace

Classes: AbstractAdapter, BaseAdapter, BufferedAdapter, NOPAdapter, RailsAdapter, SLF4JAdapter

Constant Summary

NOP =

THE instance

NOPAdapter.new
Base =

Okay, basically a singleton thus create instance

BaseAdapter.new
Rails =

THE Rails backed implementation instance

RailsAdapter.new
SLF4J =

THE instance of it

SLF4JAdapter.new
Buffered =

THE instance

BufferedAdapter.new

Instance Method Summary collapse

Instance Method Details

#adapterObject

Get the backend, if no backend is defined uses the default backend.

If running in a rails environment, automatically chooses the rails adapter as a default, else base is used.



35
36
37
# File 'lib/loggr/adapter.rb', line 35

def adapter
  @adapter ||= Object.const_defined?(:Rails) ? Loggr::Adapter::Rails : Loggr::Adapter::Base
end

#adapter=(new_adapter) ⇒ Object

Set a new adapter, either as string, class or whatever :)



41
42
43
# File 'lib/loggr/adapter.rb', line 41

def adapter=(new_adapter)
  @adapter = get_adapter(new_adapter)
end

#logger(name, options = {}, &block) ⇒ Object

Get a new logger instance for supplied named logger or class name.

All adapters must ensure that they provide the same API for creating new loggers. Each logger has a name, further possible options are:

  • `:to`, filename or IO, where to write the output to

  • `:level`, Fixnum, starting log level, @see `Loggr::Severity`

  • `:marker`, String, name of the category/marker

If an adapter does not support setting a specific option, just ignore it.



56
57
58
59
60
61
# File 'lib/loggr/adapter.rb', line 56

def logger(name, options = {}, &block)
  use_adapter = options.key?(:adapter) ? get_adapter(options.delete(:adapter)) : self.adapter
  use_adapter.logger(name, options).tap do |logger|
    yield(logger) if block_given?
  end
end

#mdcObject

The Mapped Diagnostic Context is a basically a hash where values can be stored for certain keys, the context must be stored per thread.

The most basic MDC implementation is `Thread.local ||= Hash.new`.

If a adapter provides a native MDC implementation ensure it does expose these methods:

  • `def []=(key, value)`, set a property in the MDC

  • `def [](key)`, get a property from the MDC

  • `def delete(key)`, delete a property in the MDC

  • `def clear()`, deletes all properties from the MDC

  • `def to_hash`, access MDC as standard ruby hash (might be clone, though!)

Well it should basically behave like a Ruby Hash, eventhough not with all options.



79
80
81
# File 'lib/loggr/adapter.rb', line 79

def mdc
  self.adapter.mdc
end