Module: SLF4J

Includes:
SLF4JBase
Defined in:
lib/slf4j.rb

Overview

Wrapper and core Logger compatible adapter for the SLF4J logging interface.

Usage

require 'slf4j'

log = SLF4J[ "my.app.logger" ]
log.info "Hello World!"

Adapters

All of the following output adapters are available via require:

require 'slf4j/jcl'       # Output to Jakarta Commons Logging
require 'slf4j/jdk14'     # JDK java.util.logging 
require 'slf4j/log4j12'   # Log4j (provided elsewhere)
require 'slf4j/nop'       # NOP null logger (provided)
require 'slf4j/simple'    # Simple logger (provided)

The first loaded output adapter wins (as with mulitple adapters on the classpath). A warning will be logged to “slf4j” if an attempt is made to require a second output adapter.

And the following input adapters will intercept JCL, java.util.logging (jdk14), or log4j log output and direct it through SLF4J:

require 'slf4j/jcl-over-slf4j'   # Route Jakarta Commons Logging to SLF4J
require 'slf4j/jul-to-slf4j'     # JDK java.util.logging to SLF4J
require 'slf4j/log4j-over-slf4j' # Log4j to SLF4J

Multiple input adapters may be require’d. However, a RuntimeError will be raised in the attempt to require both an output adapter and input adapter from/to the same interface, for example ‘slf4j/jcl-over-slf4j’ and ‘slf4j/jcl’, which would otherwise cause a circular logging loop (and stack overflow.)

Adapter names match the corresponding SLF4J jars.

Defined Under Namespace

Classes: Logger

Constant Summary collapse

LEVELS =

SLF4J severity levels

%w{ trace debug info warn error }
@@api_loader =
org.slf4j.ILoggerFactory.java_class.class_loader
@@loaded =
{}
@@output_name =
nil

Constants included from SLF4JBase

SLF4JBase::ADAPTERS, SLF4JBase::SLF4J_DIR, SLF4JBase::SLF4J_VERSION, SLF4JBase::VERSION

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Synonym for logger( name )



194
195
196
# File 'lib/slf4j.rb', line 194

def self.[]( name )
  Logger.new( name )
end

.linked_factoryObject

The ILoggerFactory instance if an output adapter jas been loaded



199
200
201
# File 'lib/slf4j.rb', line 199

def self.linked_factory
   org.slf4j.LoggerFactory.getILoggerFactory
end

.logger(name = self.class.name) ⇒ Object

Get Logger by name



188
189
190
# File 'lib/slf4j.rb', line 188

def logger( name = self.class.name ) 
  Logger.new( name )
end

.output_nameObject

Output adapter name if one has been added, or nil.



124
125
126
# File 'lib/slf4j.rb', line 124

def self.output_name
  @@output_name
end

.require_adapter(name) ⇒ Object

Require an adapter by name (add the jar to classpath) This is normally done via require ‘slf4j/name



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/slf4j.rb', line 73

def self.require_adapter( name ) 
  row = ADAPTERS.assoc( name )
  if row 
    name,ban = row
    output = false
  else
    row = ADAPTERS.rassoc( name )
    ban,name = row
    output = true
  end
 
  if @@loaded[ ban ]
    raise "Illegal attempt to load '#{name}' when '#{ban}' is loaded."
  end

  if output 
    if ! @@output_name.nil? && name != @@output_name
      logger("slf4j").warn do
        "Ignoring attempt to load #{name} after #{@@output_name} already loaded." 
      end
      return
    end
    if java.lang.Thread::current_thread.context_class_loader != @@api_loader
      $stderr.puts( "WARNING: Attempting to load #{name} in child class" + 
                    " loader of slf4j-api.jar loader." )
    end
    require_jar( 'slf4j-' + name )
    @@output_name = name
  else
    require_jar( name )
  end

  # Special case, requires explicit 'install'
  if name == 'jul-to-slf4j' 
    org.slf4j.bridge.SLF4JBridgeHandler.install
  end

  @@loaded[ name ] = true
end

.require_jar(name) ⇒ Object

:nodoc:



113
114
115
# File 'lib/slf4j.rb', line 113

def self.require_jar( name ) # :nodoc:
  require File.join( SLF4J_DIR, "#{name}-#{ SLF4J_VERSION }.jar" )
end