Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #40 from nttlabs/syslog_spike

Syslog support
  • Loading branch information...
commit f9a933dad5ec45eaebf16e193205f0409e78e44f 2 parents 56dda48 + 7d2889a
@derekcollison authored
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
14 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,15 @@ def setup_logs
$stderr.reopen($stdout)
end
+ def open_syslog
+ return unless @options[:syslog]
+ Syslog.open("#{@options[:syslog]}", Syslog::LOG_PID, Syslog::LOG_USER ) unless Syslog.opened?
+ end
+
+ def close_syslog
+ Syslog.close if @options[:syslog]
+ end
+
def symbolize_users(users)
return nil unless users
auth_users = []
@@ -145,6 +156,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
14 lib/nats/server/util.rb
@@ -5,17 +5,26 @@ def fast_uuid #:nodoc:
"%04x%04x%04x%04x%04x%06x" % v
end
+def syslog(args, priority) #:nodoc:
+ Syslog::log(priority, '%s', PP::pp(args.compact, '', 120))
+end
+
def log(*args) #:nodoc:
+ return syslog(args, Syslog::LOG_NOTICE) if NATSD::Server.syslog
args.unshift(Time.now) if NATSD::Server.log_time
PP::pp(args.compact, $stdout, 120)
end
def debug(*args) #:nodoc:
- log(*args) if NATSD::Server.debug_flag?
+ return unless NATSD::Server.debug_flag?
+ return syslog(args, Syslog::LOG_INFO) if NATSD::Server.syslog
+ log(*args)
end
def trace(*args) #:nodoc:
- log(*args) if NATSD::Server.trace_flag?
+ return unless NATSD::Server.trace_flag?
+ return syslog(args, Syslog::LOG_DEBUG) if NATSD::Server.syslog
+ log(*args)
end
def log_error(e=$!) #:nodoc:
@@ -56,6 +65,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
24 spec/server_log_spec.rb
@@ -1,6 +1,9 @@
require 'spec_helper'
+require 'syslog'
require 'nats/server/server'
require 'nats/server/const'
+require 'nats/server/options'
+require 'nats/server/util'
describe 'server log and pid files' do
@@ -9,6 +12,8 @@
LOG_SERVER = 'nats://localhost:9299'
LOG_LOG_FILE = '/tmp/nats_log_test.log'
LOG_FLAGS = "-l #{LOG_LOG_FILE}"
+ SYSLOG_IDENT = "nats_syslog_test"
+ LOG_SYSLOG_FLAGS= "#{LOG_FLAGS} -S #{SYSLOG_IDENT}"
FileUtils.rm_f(LOG_LOG_FILE)
@s = NatsServerControl.new(LOG_SERVER, LOG_SERVER_PID, LOG_FLAGS)
@@ -39,4 +44,23 @@
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
+
+ it 'should use Syslog module methods when enable syslog option' do
+ *log_msg = 'syslog test'
+ Syslog.should_receive(:open).with(SYSLOG_IDENT, Syslog::LOG_PID, Syslog::LOG_USER)
+ Syslog.should_receive(:log).with(Syslog::LOG_NOTICE, '%s', PP::pp(log_msg, '', 120))
+ Syslog.should_receive(:close)
+ NATSD::Server.process_options(LOG_SYSLOG_FLAGS.split)
+ NATSD::Server.open_syslog
+ log *log_msg
+ NATSD::Server.close_syslog
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.