diff options
author | Azul <azul@riseup.net> | 2016-09-30 11:59:20 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-09-30 11:59:20 +0200 |
commit | 8ee1d1cb70bee8c938966e4e042b4929ced8b1a3 (patch) | |
tree | 99d3a5c5ded96b84924b582f615d662536f5bb0a /lib/nickserver | |
parent | 997af2eea91263b1d6c07e02a98f02f81a320acf (diff) |
logging: use proper logger
This way the logs do not interfere with test output
Diffstat (limited to 'lib/nickserver')
-rw-r--r-- | lib/nickserver/dispatcher.rb | 5 | ||||
-rw-r--r-- | lib/nickserver/logging_responder.rb | 18 | ||||
-rw-r--r-- | lib/nickserver/reel_server.rb | 17 |
3 files changed, 30 insertions, 10 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb index 26ad09e..71e71cf 100644 --- a/lib/nickserver/dispatcher.rb +++ b/lib/nickserver/dispatcher.rb @@ -39,10 +39,6 @@ module Nickserver def handle(request) handler_chain.handle request - rescue RuntimeError => exc - $stderr.puts "Error: #{exc}" - $stderr.puts exc.backtrace - ErrorResponse.new(exc.to_s) end def handler_chain @@ -64,7 +60,6 @@ module Nickserver def proxy_error_response exc = handler_chain.rescued_exceptions.first if exc - $stderr.puts " Error: #{exc}" Nickserver::Response.new 502, JSON.dump(error: exc.to_s) end diff --git a/lib/nickserver/logging_responder.rb b/lib/nickserver/logging_responder.rb new file mode 100644 index 0000000..6eb756f --- /dev/null +++ b/lib/nickserver/logging_responder.rb @@ -0,0 +1,18 @@ +module Nickserver + class LoggingResponder + + def initialize(responder, logger) + @responder = responder + @logger = logger + end + + def respond(status, body) + logger.info " -> #{status}" + responder.respond(status, body) + end + + protected + + attr_reader :responder, :logger + end +end diff --git a/lib/nickserver/reel_server.rb b/lib/nickserver/reel_server.rb index 79265ec..0ff4425 100644 --- a/lib/nickserver/reel_server.rb +++ b/lib/nickserver/reel_server.rb @@ -1,8 +1,11 @@ silence_warnings do require 'reel' end +require 'logger' +require 'nickserver/config' require 'nickserver/adapters/celluloid_http' require 'nickserver/dispatcher' +require 'nickserver/logging_responder' module Nickserver class ReelServer < Reel::Server::HTTP @@ -31,19 +34,20 @@ module Nickserver protected def handle_request(request) - puts "#{request.method} #{request.uri}" - puts " #{params(request)}" + logger.info "#{request.method} #{request.uri}" + logger.debug " #{params(request)}" handler = handler_for(request) handler.respond_to params(request), request.headers rescue StandardError => e - $stderr.puts e - $stderr.puts e.backtrace.join "\n " + logger.error e + logger.error e.backtrace.join "\n " request.respond 500, "{}" end def handler_for(request) # with reel the request is the responder - Dispatcher.new(request) + responder = LoggingResponder.new(request, logger) + Dispatcher.new(responder) end def params(request) @@ -54,5 +58,8 @@ module Nickserver end end + def logger + @logger ||= ::Logger.new Config.log_file + end end end |