Skip to content

Commit

Permalink
Fix for configuration via Configurability::Config.
Browse files Browse the repository at this point in the history
  • Loading branch information
ged committed May 11, 2012
1 parent 85be0fa commit 9ca49fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/loggability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ module Loggability

# Configuration defaults
CONFIG_DEFAULTS = {
:defaults => {
:severity => 'warn',
:formatter => 'default',
:output => 'STDERR',
},
:__default__ => 'warn STDERR',
}

# Regexp for parsing logspec lines in the config
Expand Down Expand Up @@ -293,17 +289,18 @@ def log_to( loghost )
def self::configure( config=nil )
if config
self.log.debug "Configuring Loggability with custom config."
confighash = config.to_hash

# Set up all loggers with defaults first
if defaultspec = config.delete( :__default__ ) || config.delete( '__default__' )
if defaultspec = confighash.delete( :__default__ ) || confighash.delete( '__default__' )
level, format, target = self.parse_config_spec( defaultspec )
Loggability.level = level if level
Loggability.format_as( format ) if format
Loggability.output_to( target ) if target
end

# Then let individual configs override.
config.each do |key, logspec|
confighash.each do |key, logspec|
unless Loggability.log_host?( key )
self.log.debug " no such log host %p; skipping" % [ key ]
next
Expand Down
22 changes: 22 additions & 0 deletions spec/loggability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@
Loggability[ class2 ].logdev.dev.path.should == 'spec-error.log'
end

it "can configure loghosts with a Configurability::Config object" do
class1 = Class.new { extend Loggability; log_as :class1 }
class2 = Class.new { extend Loggability; log_as :class2 }

configsource = (<<-"END_CONFIG").gsub( /^\t{3}/, '' )
---
logging:
class1: debug (html)
class2: error spec-error.log
END_CONFIG

config = Configurability::Config.new( configsource )
config.install

Loggability[ class1 ].level.should == :debug
Loggability[ class1 ].formatter.should be_a( Loggability::Formatter::HTML )
Loggability[ class2 ].level.should == :error
Loggability[ class2 ].logdev.dev.should be_a( File )
Loggability[ class2 ].logdev.dev.path.should == 'spec-error.log'
end

it "can configure all loghosts with a config key of __default__" do
Loggability.configure( '__default__' => 'debug STDERR (html)' )

Expand Down

0 comments on commit 9ca49fe

Please sign in to comment.