Class: Unilogger::Builder
- Inherits:
-
Object
- Object
- Unilogger::Builder
- Defined in:
- lib/unilogger/builder.rb
Class Method Summary collapse
- .as_factory(kind) ⇒ Object
- .as_level(level, default_level = ::Logger::Severity::INFO) ⇒ Object
-
.build(options) ⇒ Object
options must include env => “development”, “test”, “production”, etc.
Instance Method Summary collapse
-
#initialize(configuration) ⇒ Builder
constructor
class.
- #logger ⇒ Object
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( ) env = [:env] || ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development" root = [: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
#logger ⇒ Object
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 || = .first kind = .first factory = self.class.as_factory( kind ) factory.build( .last ) end Logger.new( level, emitters ) end |