Class: Unilogger::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/unilogger/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Builder

class



43
44
45
# File 'lib/unilogger/builder.rb', line 43

def initialize( configuration )
  @configuration = configuration
end

Class Method Details

.as_factory(kind) ⇒ Object



34
35
36
37
38
39
# File 'lib/unilogger/builder.rb', line 34

def as_factory( kind )
  kind = "LogFileEmitter" if kind =~ /^logger$/i
  factory = Unilogger.const_get(kind) rescue Object.const_get(kind) rescue nil
  raise "unknown kind of emitter (#{kind})" if ! factory
  return factory
end

.as_level(level, default_level = ::Logger::Severity::INFO) ⇒ Object



28
29
30
31
32
# File 'lib/unilogger/builder.rb', line 28

def as_level( level, default_level = ::Logger::Severity::INFO )
  return default_level if level.nil? || level.size == 0
  return level if level.kind_of?(Integer)
  ::Logger::Severity.const_get( level.to_s.upcase )
end

.build(options) ⇒ Object

options must include env => “development”, “test”, “production”, etc. root => parent of config and log directories



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/unilogger/builder.rb', line 13

def build( options )
  env  = options[:env] || ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
  root = options[:root]
  
  if File.exist?( yml = "#{root}/config/logger.yml" ) then
    cfg = YAML.load(IO.read( yml )) [env]
  elsif File.exist?( yml = "#{root}/config/logger.yml.erb" )
    cfg = YAML.load( (ERB.new( IO.read( yml ) ).result) ) [env]
  else
    cfg = { "level" => "debug", "emitters" => [ { "logger" => { "logdev" => "stderr" } } ] }
  end
  
  Unilogger::Builder.new( cfg ).logger
end

Instance Method Details

#loggerObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/unilogger/builder.rb', line 47

def logger
  # accept debug...fatal, 0...4, default info
  level = self.class.as_level( @configuration["level"] )
  
  # emitters
  emitters = @configuration["emitters"].map do |options|
    options = options.first
    kind    = options.first
    factory = self.class.as_factory( kind )
    factory.build( options.last )
  end
  
  Logger.new( level, emitters )
end