module RJack::Logback

Jruby wrapper module for the Logback log writer. Programmatic configuration and setting of logger output levels is supported.

Examples

High level configuration

require 'rjack-logback'
RJack::Logback.config_console( :thread => true, :level => :info )

Low level configuration

require 'rjack-logback'

log = RJack::SLF4J[ 'example' ]
log.info "About to reconfigure..."

RJack::Logback.configure do
  console = RJack::Logback::ConsoleAppender.new do |a|
    a.target = "System.err"
    a.layout = RJack::Logback::PatternLayout.new do |p|
      p.pattern = "%r %-5level %logger{35} - %msg %ex%n"
    end
  end
  RJack::Logback.root.add_appender( console )
  RJack::Logback.root.level = RJack::Logback::INFO
end

# Adjust output levels (also works outside of configure )
RJack::Logback[ 'example' ].level = RJack::Logback::DEBUG

log.debug "...after reconfigure."

Configure with Logback XML configuration:

RJack::Logback.configure do
  RJack::Logback.load_xml_config( 'sample-logback.xml' )
end

Programmatic Configuration Support

Logback java classes implement interfaces LifeCycle and ContextAware for configurability with Joran (XML). To simplify configuration in ruby, the following classes have been extended:

The extensions provide a block initializer which sets sensible defaults, yields to a block for customization, and then calls start. Logback provides many other components not yet extended in this way. These can be created directly and or extended in a similar fashion externally. Consider providing a patch to the rjack project with desired extensions.

Constants

DEBUG

Level::DEBUG

ERROR

Level::ERROR

INFO

Level::INFO

JConsoleAppender

ch.qos.logback.core.ConsoleAppender

JFileAppender

ch.qos.logback.core.FileAppender

JLevelChangePropagator
JPatternLayout

ch.qos.logback.classic.PatternLayout

JoranConfigurator

ch.qos.logback.classic.joran.JoranConfigurator

LOGBACK_VERSION

Logback java version

Level

ch.qos.logback.classic.Level

OFF

Level::OFF

TRACE

Level::TRACE

VERSION

Logback gem version

WARN

Level::WARN

Public Class Methods

[](name) click to toggle source

Synonym for logger(name)

# File lib/rjack-logback.rb, line 341
def self.[](name)
  logger( name )
end
config_console( options = {} ) click to toggle source
configure() { |context| ... } click to toggle source

Configure Logback with the specified block. The Logback context is reset, yielded to block, and then started after return from the block.

# File lib/rjack-logback.rb, line 281
def self.configure
  @@context.reset

  yield context

  Util.start( context )
  nil
end
context() click to toggle source

Returns the LoggerContext

# File lib/rjack-logback.rb, line 115
def self.context
  @@context
end
load_xml_config( file ) click to toggle source

Load the specified Logback (Joran) XML configuration file. Should be called within a configure {…} block.

# File lib/rjack-logback.rb, line 174
def self.load_xml_config( file )
  cfger = JoranConfigurator.new
  cfger.context = @@context
  cfger.doConfigure( file )
end
logger( name ) click to toggle source

Returns the named Logger

# File lib/rjack-logback.rb, line 336
def self.logger( name )
  Logger.new( @@context.getLogger( name ) )
end
require_jar( name ) click to toggle source

Load logback jar.

# File lib/rjack-logback.rb, line 82
def self.require_jar( name )
  require File.join( LOGBACK_DIR, "#{name}-#{ LOGBACK_VERSION }.jar" )
end
root() click to toggle source

Returns the special “root” Logger

# File lib/rjack-logback.rb, line 346
def self.root
  logger( "root" )
end
to_level( l ) click to toggle source

Converts Symbol to Level constant, or return Level unaltered.

# File lib/rjack-logback.rb, line 351
def self.to_level( l )
  if l.is_a?( Symbol )
    const_get( l.to_s.upcase.to_sym )
  else
    l
  end
end