Permalink
Browse files

Allow custom logger setup.

Defining a setup_logger method in the API will allow the API to
override the standard Goliath logging. Note, if this is used, Goliath
will not setup any loggers. It is your responsibility to deal with
stdout and file logging if desired.

[Issue #52]
  • Loading branch information...
1 parent 90cdd5c commit 42260c42cb984b6303076cc79c082be39bbeaf23 @dj2 dj2 committed May 19, 2011
Showing with 41 additions and 1 deletion.
  1. +15 −0 examples/custom_logger.rb
  2. +7 −1 lib/goliath/runner.rb
  3. +19 −0 spec/unit/runner_spec.rb
View
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+$:<< '../lib' << 'lib'
+
+require 'goliath'
+
+class CustomLogger < Goliath::API
+ def setup_logger(logger, opts)
+ log_format = Log4r::PatternFormatter.new(:pattern => "%d :: %m")
+ logger.add(Log4r::StdoutOutputter.new('console', :formatter => log_format))
+ end
+
+ def response(env)
+ [200, {}, "OK"]
+ end
+end
View
@@ -176,12 +176,18 @@ def show_options(opts)
# @return [Logger] The logger object
def setup_logger
log = Log4r::Logger.new('goliath')
+ log.level = @verbose ? Log4r::DEBUG : Log4r::INFO
+
+ # allow api to setup logging as it desires
+ if api.respond_to?(:setup_logger)
+ api.setup_logger(log, options)
+ return log
+ end
log_format = Log4r::PatternFormatter.new(:pattern => "[#{Process.pid}:%l] %d :: %m")
setup_file_logger(log, log_format) if @log_file
setup_stdout_logger(log, log_format) if @log_stdout
- log.level = @verbose ? Log4r::DEBUG : Log4r::INFO
log
end
View
@@ -89,6 +89,25 @@
log = @r.send(:setup_logger)
end
end
+
+ describe 'custom logger' do
+ it 'calls the api loger setup method' do
+ api = mock('api')
+ api.should_receive(:respond_to?).with(:setup_logger).and_return(true)
+ api.should_receive(:setup_logger).and_return(true)
+
+ @r.should_not_receive(:setup_stdout_logger)
+ @r.should_not_receive(:setup_file_logger)
+
+ @r.api = api
+ @r.log_stdout = true
+ @r.verbose = true
+ @r.log_file = 'out.log'
+
+ l = @r.send(:setup_logger)
+ l.should_not be_nil
+ end
+ end
end
it 'creates the log dir if neeed' do

0 comments on commit 42260c4

Please sign in to comment.