Class: RJack::SLF4J::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/rjack-slf4j.rb

Overview

Ruby ::Logger compatible adapter for org.slf4j.Logger

Generated Methods

Corresponding methods are generated for each of the SLF4J levels:

  • trace
  • debug
  • info
  • warn
  • error
  • fatal (alias to error)

These have the form (using info as example):

log = Logger.new( "name" )
log.info?                    # Is this level enabled for logging?
log.info( "message" )        # Log message
log.info { "message" }       # Execute block if enabled
                             and log returned value
log.info( "message", ex )    # Log message with exception message/stack trace
log.info( ex ) { "message" } # Log message with exception message/stack trace
log.info( ex )               # Log exception with default "Exception:" message

Note that the exception variants are aware of JRuby's NativeException class (a wrapped java exception) and will log using the Java ex.cause in this case.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Logger

Create new or find existing Logger by name. If name is a Module (Class, etc.) then use SLF4J.to_log_name( name ) as the name

Note that loggers are arranged in a hiearchy by dot '.' name notation using java package/class name conventions:

  • "pmodule"
  • "pmodule.cmodule."
  • "pmodule.cmodule.ClassName"

Which enables hierarchical level setting and abbreviation in some output adapters.



207
208
209
210
211
# File 'lib/rjack-slf4j.rb', line 207

def initialize( name )
  @name = name.is_a?( Module ) ? SLF4J.to_log_name( name ) : name
  @logger = org.slf4j.LoggerFactory.getLogger( @name )
  @level = 0 #DEBUG
end

Instance Attribute Details

#datetime_format ⇒ Object

Unused attribute, for Ruby ::Logger compatibility.



284
285
286
# File 'lib/rjack-slf4j.rb', line 284

def datetime_format
  @datetime_format
end

#formatter ⇒ Object

Unused attribute, for Ruby ::Logger compatibility.



293
294
295
# File 'lib/rjack-slf4j.rb', line 293

def formatter
  @formatter
end

#level ⇒ Object Also known as: sev_threshold

Unused attribute, for Ruby ::Logger compatibility.



271
272
273
# File 'lib/rjack-slf4j.rb', line 271

def level
  @level
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



193
194
195
# File 'lib/rjack-slf4j.rb', line 193

def name
  @name
end

#progname ⇒ Object

Unused attribute, for Ruby ::Logger compatibility.



262
263
264
# File 'lib/rjack-slf4j.rb', line 262

def progname
  @progname
end

Instance Method Details

#add(rlvl, msg = nil, progname = nil, &block) ⇒ Object Also known as: log

Log via Ruby ::Logger levels, for compatibility. UNKNOWN or nil level is mapped to #info



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/rjack-slf4j.rb', line 315

def add( rlvl, msg = nil, progname = nil, &block )
  case rlvl
  when 0 #DEBUG
    debug( msg, &block )
  when 1 #INFO
    info( msg, &block )
  when 2 #WARN
    warn( msg, &block )
  when 3 #ERROR
    error( msg, &block )
  when 4 #FATAL
    error( msg, &block )
  else #UNKNOWN, nil
    info( msg, &block )
  end
end

#close ⇒ Object

No-Op, for Ruby ::Logger compatibility.



335
336
337
# File 'lib/rjack-slf4j.rb', line 335

def close
  #No-OP
end

#java_logger ⇒ Object

Return underlying org.slf4j.Logger



214
215
216
# File 'lib/rjack-slf4j.rb', line 214

def java_logger
  @logger
end

#reopen ⇒ Object

No-Op, for Ruby ::Logger compatibility.



340
341
# File 'lib/rjack-slf4j.rb', line 340

def reopen
end