Permalink
Browse files

Syslog support

  • Loading branch information...
1 parent 56dda48 commit b9417de33f00881a2e59f62a48598ac89f51bf42 @nomoto-1 nomoto-1 committed May 29, 2012
View
1 lib/nats/server.rb
@@ -2,6 +2,7 @@
require 'socket'
require 'fileutils'
require 'pp'
+require 'syslog'
ep = File.expand_path(File.dirname(__FILE__))
View
17 lib/nats/server/options.rb
@@ -27,6 +27,7 @@ def parser
opts.on("-l", "--log FILE", "File to redirect log output") { |file| @options[:log_file] = file }
opts.on("-T", "--logtime", "Timestamp log entries (default: false)") { @options[:log_time] = true }
+ opts.on("-S", "--syslog IDENT", "Enable Syslog output") { |ident| @options[:syslog] = ident }
opts.on("-D", "--debug", "Enable debugging output") { @options[:debug] = true }
opts.on("-V", "--trace", "Trace the raw protocol") { @options[:trace] = true }
@@ -77,6 +78,7 @@ def read_config_file
@options[:pid_file] = config['pid_file'] if @options[:pid_file].nil?
@options[:log_file] = config['log_file'] if @options[:log_file].nil?
@options[:log_time] = config['logtime'] if @options[:log_time].nil?
+ @options[:syslog] = config['syslog'] if @options[:syslog].nil?
@options[:debug] = config['debug'] if @options[:debug].nil?
@options[:trace] = config['trace'] if @options[:trace].nil?
@@ -116,6 +118,18 @@ def setup_logs
$stderr.reopen($stdout)
end
+ def open_syslog
+ return unless @options[:syslog]
+ unless Syslog.opened?
+ Syslog.open("#{@options[:syslog]}", Syslog::LOG_PID, Syslog::LOG_USER )
+ end
+ end
+
+ def close_syslog
+ return unless @options[:syslog]
+ Syslog.close
+ end
+
def symbolize_users(users)
return nil unless users
auth_users = []
@@ -145,6 +159,9 @@ def finalize_options
debug "DEBUG is on"
trace "TRACE is on"
+ #Syslog
+ @syslog = @options[:syslog]
+
# Authorization
# Multi-user setup for auth
View
3 lib/nats/server/server.rb
@@ -6,7 +6,7 @@ module NATSD #:nodoc: all
class Server
class << self
- attr_reader :id, :info, :log_time, :auth_required, :ssl_required, :debug_flag, :trace_flag, :options
+ attr_reader :id, :info, :log_time, :auth_required, :ssl_required, :debug_flag, :trace_flag, :syslog, :options
attr_reader :max_payload, :max_pending, :max_control_line, :auth_timeout, :ssl_timeout, :ping_interval, :ping_max
attr_accessor :varz, :healthz, :max_connections, :num_connections, :in_msgs, :out_msgs, :in_bytes, :out_bytes
@@ -75,6 +75,7 @@ def setup(argv)
end
setup_logs
+ open_syslog
# Setup optimized select versions
EM.epoll unless @options[:noepoll]
View
50 lib/nats/server/util.rb
@@ -5,17 +5,60 @@ def fast_uuid #:nodoc:
"%04x%04x%04x%04x%04x%06x" % v
end
-def log(*args) #:nodoc:
+def log_syslog(args, priority = Syslog::LOG_NOTICE) #:nodoc:
+ args.unshift(Time.now) if NATSD::Server.log_time
+ Syslog::log(priority, '%s', PP::pp(args.compact, '', 120))
+end
+
+def debug_syslog(args) #:nodoc:
+ if NATSD::Server.debug_flag?
+ priority = Syslog::LOG_INFO
+ log_syslog(args, priority)
+ end
+end
+
+def trace_syslog(args) #:nodoc:
+ if NATSD::Server.trace_flag?
+ priority = Syslog::LOG_DEBUG
+ log_syslog(args, priority)
+ end
+end
+
+def log_stdout(*args) #:nodoc:
args.unshift(Time.now) if NATSD::Server.log_time
PP::pp(args.compact, $stdout, 120)
end
+def debug_stdout(*args) #:nodoc:
+ log_stdout(*args) if NATSD::Server.debug_flag?
+end
+
+def trace_stdout(*args) #:nodoc:
+ log_stdout(*args) if NATSD::Server.trace_flag?
+end
+
+def log(*args) #:nodoc:
+ if NATSD::Server.syslog
+ log_syslog(args)
+ else
+ log_stdout(*args)
+ end
+end
+
def debug(*args) #:nodoc:
- log(*args) if NATSD::Server.debug_flag?
+ if NATSD::Server.syslog
+ debug_syslog(args)
+ else
+ debug_stdout(*args)
+ end
end
def trace(*args) #:nodoc:
- log(*args) if NATSD::Server.trace_flag?
+ if NATSD::Server.syslog
+ trace_syslog(args)
+ else
+ trace_stdout(*args)
+ end
end
def log_error(e=$!) #:nodoc:
@@ -56,6 +99,7 @@ def num_cpu_cores
def shutdown #:nodoc:
puts
log 'Server exiting..'
+ NATSD::Server.close_syslog
EM.stop
if NATSD::Server.pid_file
FileUtils.rm(NATSD::Server.pid_file) if File.exists? NATSD::Server.pid_file
View
3 spec/resources/config.yml
@@ -31,4 +31,5 @@ max_connections: 128
no_epoll: false
no_kqueue: true
-
+# Syslog
+syslog: nats_syslog_test
@derekcollison
derekcollison Jun 4, 2012

We should probably do a separate config file for the syslog test..

View
1 spec/server_config_spec.rb
@@ -49,6 +49,7 @@
opts[:pid_file].should == config['pid_file']
opts[:log_file].should == config['log_file']
opts[:log_time].should == config['logtime']
+ opts[:syslog].should == config['syslog']
opts[:debug].should == config['debug']
opts[:trace].should == config['trace']
opts[:max_control_line].should == config['max_control_line']
View
11 spec/server_log_spec.rb
@@ -9,6 +9,9 @@
LOG_SERVER = 'nats://localhost:9299'
LOG_LOG_FILE = '/tmp/nats_log_test.log'
LOG_FLAGS = "-l #{LOG_LOG_FILE}"
+ LOG_SYSLOG_IDENT = "nats_syslog_test"
+ LOG_SYSLOG_FLAGS= "#{LOG_FLAGS} -S #{LOG_SYSLOG_IDENT}"
+
FileUtils.rm_f(LOG_LOG_FILE)
@s = NatsServerControl.new(LOG_SERVER, LOG_SERVER_PID, LOG_FLAGS)
@@ -39,4 +42,12 @@
File.read(LOG_LOG_FILE).split("\n").size.should == 2
end
+ it 'should not output to the log file when enable syslog option' do
+ @s.kill_server
+ FileUtils.rm_f(LOG_LOG_FILE)
+ @s = NatsServerControl.new(LOG_SERVER, LOG_SERVER_PID, LOG_SYSLOG_FLAGS)
+ @s.start_server
+ File.read(LOG_LOG_FILE).split("\n").size.should == 0
+ end
+
end

2 comments on commit b9417de

@derekcollison

Thanks I will take a look.

@derekcollison

The logging now looks more complex than it needs to be. Also, should we have an explicit test to make sure output is actually making it to the system log?

Please sign in to comment.