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.



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

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_formatObject

Unused attribute, for Ruby ::Logger compatibility.



266
267
268
# File 'lib/rjack-slf4j.rb', line 266

def datetime_format
  @datetime_format
end

#formatterObject

Unused attribute, for Ruby ::Logger compatibility.



275
276
277
# File 'lib/rjack-slf4j.rb', line 275

def formatter
  @formatter
end

#levelObject Also known as: sev_threshold

Unused attribute, for Ruby ::Logger compatibility.



253
254
255
# File 'lib/rjack-slf4j.rb', line 253

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



177
178
179
# File 'lib/rjack-slf4j.rb', line 177

def name
  @name
end

#prognameObject

Unused attribute, for Ruby ::Logger compatibility.



244
245
246
# File 'lib/rjack-slf4j.rb', line 244

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



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/rjack-slf4j.rb', line 297

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

#closeObject

No-Op, for Ruby ::Logger compatibility.



317
318
319
# File 'lib/rjack-slf4j.rb', line 317

def close
  #No-OP
end

#java_loggerObject

Return underlying org.slf4j.Logger



198
199
200
# File 'lib/rjack-slf4j.rb', line 198

def java_logger
  @logger
end