deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] Core: Fixed missing syslog library under Windows environment (DTACLOUD-186)
Date Thu, 05 Apr 2012 11:07:52 GMT
From: Michal Fojtik <mfojtik@redhat.com>

Previosly when Deltacloud was started under Windows with production environment,
it throws an exception because of missing 'syslog' library.  This patch should
fix this issue and Deltacloud should log to STDOUT if it is not able locate the
syslog library.

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/deltacloud/server.rb   |    5 ++---
 server/lib/sinatra/rack_syslog.rb |   27 +++++++++++++++++----------
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
index f0f8cbc..7b499d9 100644
--- a/server/lib/deltacloud/server.rb
+++ b/server/lib/deltacloud/server.rb
@@ -62,11 +62,10 @@ end
 
 configure :production do
   use Rack::SyslogLogger
+  set :logger, SyslogFile.new
   disable :logging
   enable :show_errors
-  set :dump_errors, false
-  $stdout = SyslogFile.new
-  $stderr = $stdout
+  enable :dump_errors
 end
 
 configure :development do
diff --git a/server/lib/sinatra/rack_syslog.rb b/server/lib/sinatra/rack_syslog.rb
index f7c3d53..5565179 100644
--- a/server/lib/sinatra/rack_syslog.rb
+++ b/server/lib/sinatra/rack_syslog.rb
@@ -1,10 +1,16 @@
-require 'syslog'
+begin
+  require 'syslog'
+  USE_SYSLOG = true
+rescue LoadError => e
+  USE_SYSLOG = false
+end
+
 require 'sinatra/body_proxy'
 
 class SyslogFile < File
 
   def initialize
-    @log = Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_LOCAL5)
+    @log = USE_SYSLOG ? Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_LOCAL5) : Logger.new(STDOUT)
   end
 
   def write(string)
@@ -13,11 +19,11 @@ class SyslogFile < File
   end
 
   def info(msg)
-    @log.info("%s", msg)
+    @log.info("%s" % msg)
   end
 
   def err(msg)
-    @log.err("%s", msg)
+    @log.err("%s" % msg)
   end
 
   alias :warning :err
@@ -36,23 +42,24 @@ module Rack
     # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
     # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
     #             %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
-    FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n}
+    FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f}
 
     def initialize(app, logger=nil)
       @app = app
-      @logger = logger || $stdout
+      @logger = logger || @app.settings.logger || $stdout
     end
 
     def call(env)
       began_at = Time.now
       status, header, body = @app.call(env)
       header = Utils::HeaderHash.new(header)
-      body = Rack::BodyProxy.new(body) { log(env, status, header, began_at) }
+      body = Rack::BodyProxy.new(body) do
+        log(env, status, header, began_at)
+      end
+      body.close
       [status, header, body]
     end
 
-    private
-
     def log(env, status, header, began_at)
       now = Time.now
       length = extract_content_length(header)
@@ -63,7 +70,7 @@ module Rack
         method = :info
       end
 
-      logger = @logger || env['rack.errors']
+      logger = @logger
       logger.send(method, FORMAT % [
         env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
         env["REMOTE_USER"] || "-",
-- 
1.7.9.1


Mime
View raw message