Skip to content
This repository
Browse code

Merge pull request #40 from nttlabs/syslog_spike

Syslog support
  • Loading branch information...
commit f9a933dad5ec45eaebf16e193205f0409e78e44f 2 parents 56dda48 + 7d2889a
Derek Collison authored June 26, 2012
1  lib/nats/server.rb
@@ -2,6 +2,7 @@
2 2
 require 'socket'
3 3
 require 'fileutils'
4 4
 require 'pp'
  5
+require 'syslog'
5 6
 
6 7
 ep = File.expand_path(File.dirname(__FILE__))
7 8
 
14  lib/nats/server/options.rb
@@ -27,6 +27,7 @@ def parser
27 27
 
28 28
           opts.on("-l", "--log FILE", "File to redirect log output")           { |file| @options[:log_file] = file }
29 29
           opts.on("-T", "--logtime", "Timestamp log entries (default: false)") { @options[:log_time] = true }
  30
+          opts.on("-S", "--syslog IDENT", "Enable Syslog output")              { |ident| @options[:syslog] = ident }
30 31
           opts.on("-D", "--debug", "Enable debugging output")                  { @options[:debug] = true }
31 32
           opts.on("-V", "--trace", "Trace the raw protocol")                   { @options[:trace] = true }
32 33
 
@@ -77,6 +78,7 @@ def read_config_file
77 78
         @options[:pid_file] = config['pid_file'] if @options[:pid_file].nil?
78 79
         @options[:log_file] = config['log_file'] if @options[:log_file].nil?
79 80
         @options[:log_time] = config['logtime'] if @options[:log_time].nil?
  81
+        @options[:syslog] = config['syslog'] if @options[:syslog].nil?
80 82
         @options[:debug] = config['debug'] if @options[:debug].nil?
81 83
         @options[:trace] = config['trace'] if @options[:trace].nil?
82 84
 
@@ -116,6 +118,15 @@ def setup_logs
116 118
         $stderr.reopen($stdout)
117 119
       end
118 120
 
  121
+      def open_syslog
  122
+        return unless @options[:syslog]
  123
+        Syslog.open("#{@options[:syslog]}", Syslog::LOG_PID,  Syslog::LOG_USER ) unless Syslog.opened? 
  124
+      end
  125
+
  126
+      def close_syslog
  127
+        Syslog.close if @options[:syslog]
  128
+      end
  129
+
119 130
       def symbolize_users(users)
120 131
         return nil unless users
121 132
         auth_users = []
@@ -145,6 +156,9 @@ def finalize_options
145 156
         debug "DEBUG is on"
146 157
         trace "TRACE is on"
147 158
 
  159
+        # Syslog
  160
+        @syslog = @options[:syslog]
  161
+
148 162
         # Authorization
149 163
 
150 164
         # Multi-user setup for auth
3  lib/nats/server/server.rb
@@ -6,7 +6,7 @@ module NATSD #:nodoc: all
6 6
   class Server
7 7
 
8 8
     class << self
9  
-      attr_reader :id, :info, :log_time, :auth_required, :ssl_required, :debug_flag, :trace_flag, :options
  9
+      attr_reader :id, :info, :log_time, :auth_required, :ssl_required, :debug_flag, :trace_flag, :syslog, :options
10 10
       attr_reader :max_payload, :max_pending, :max_control_line, :auth_timeout, :ssl_timeout, :ping_interval, :ping_max
11 11
       attr_accessor :varz, :healthz, :max_connections, :num_connections, :in_msgs, :out_msgs, :in_bytes, :out_bytes
12 12
 
@@ -75,6 +75,7 @@ def setup(argv)
75 75
         end
76 76
 
77 77
         setup_logs
  78
+        open_syslog
78 79
 
79 80
         # Setup optimized select versions
80 81
         EM.epoll unless @options[:noepoll]
14  lib/nats/server/util.rb
@@ -5,17 +5,26 @@ def fast_uuid #:nodoc:
5 5
   "%04x%04x%04x%04x%04x%06x" % v
6 6
 end
7 7
 
  8
+def syslog(args, priority) #:nodoc:
  9
+  Syslog::log(priority, '%s', PP::pp(args.compact, '', 120))
  10
+end
  11
+
8 12
 def log(*args) #:nodoc:
  13
+  return syslog(args, Syslog::LOG_NOTICE) if NATSD::Server.syslog
9 14
   args.unshift(Time.now) if NATSD::Server.log_time
10 15
   PP::pp(args.compact, $stdout, 120)
11 16
 end
12 17
 
13 18
 def debug(*args) #:nodoc:
14  
-  log(*args) if NATSD::Server.debug_flag?
  19
+  return unless NATSD::Server.debug_flag?
  20
+  return syslog(args, Syslog::LOG_INFO) if NATSD::Server.syslog
  21
+  log(*args)
15 22
 end
16 23
 
17 24
 def trace(*args) #:nodoc:
18  
-  log(*args) if NATSD::Server.trace_flag?
  25
+  return unless NATSD::Server.trace_flag?
  26
+  return syslog(args, Syslog::LOG_DEBUG) if NATSD::Server.syslog
  27
+  log(*args)
19 28
 end
20 29
 
21 30
 def log_error(e=$!) #:nodoc:
@@ -56,6 +65,7 @@ def num_cpu_cores
56 65
 def shutdown #:nodoc:
57 66
   puts
58 67
   log 'Server exiting..'
  68
+  NATSD::Server.close_syslog
59 69
   EM.stop
60 70
   if NATSD::Server.pid_file
61 71
     FileUtils.rm(NATSD::Server.pid_file) if File.exists? NATSD::Server.pid_file
24  spec/server_log_spec.rb
... ...
@@ -1,6 +1,9 @@
1 1
 require 'spec_helper'
  2
+require 'syslog'
2 3
 require 'nats/server/server'
3 4
 require 'nats/server/const'
  5
+require 'nats/server/options'
  6
+require 'nats/server/util'
4 7
 
5 8
 describe 'server log and pid files' do
6 9
 
@@ -9,6 +12,8 @@
9 12
     LOG_SERVER = 'nats://localhost:9299'
10 13
     LOG_LOG_FILE = '/tmp/nats_log_test.log'
11 14
     LOG_FLAGS = "-l #{LOG_LOG_FILE}"
  15
+    SYSLOG_IDENT = "nats_syslog_test"
  16
+    LOG_SYSLOG_FLAGS= "#{LOG_FLAGS} -S #{SYSLOG_IDENT}"
12 17
 
13 18
     FileUtils.rm_f(LOG_LOG_FILE)
14 19
     @s = NatsServerControl.new(LOG_SERVER, LOG_SERVER_PID, LOG_FLAGS)
@@ -39,4 +44,23 @@
39 44
     File.read(LOG_LOG_FILE).split("\n").size.should == 2
40 45
   end
41 46
 
  47
+  it 'should not output to the log file when enable syslog option' do
  48
+    @s.kill_server
  49
+    FileUtils.rm_f(LOG_LOG_FILE)
  50
+    @s = NatsServerControl.new(LOG_SERVER, LOG_SERVER_PID, LOG_SYSLOG_FLAGS)
  51
+    @s.start_server
  52
+    File.read(LOG_LOG_FILE).split("\n").size.should == 0
  53
+  end
  54
+
  55
+ it 'should use Syslog module methods when enable syslog option' do
  56
+    *log_msg  = 'syslog test'
  57
+    Syslog.should_receive(:open).with(SYSLOG_IDENT, Syslog::LOG_PID, Syslog::LOG_USER)
  58
+    Syslog.should_receive(:log).with(Syslog::LOG_NOTICE, '%s', PP::pp(log_msg, '', 120))
  59
+    Syslog.should_receive(:close)
  60
+    NATSD::Server.process_options(LOG_SYSLOG_FLAGS.split)
  61
+    NATSD::Server.open_syslog
  62
+    log  *log_msg
  63
+    NATSD::Server.close_syslog
  64
+ end
  65
+
42 66
 end

0 notes on commit f9a933d

Please sign in to comment.
Something went wrong with that request. Please try again.