Skip to content

Commit

Permalink
Syslog support
Browse files Browse the repository at this point in the history
  • Loading branch information
nomoto-1 committed May 29, 2012
1 parent 56dda48 commit b9417de
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/nats/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'socket'
require 'fileutils'
require 'pp'
require 'syslog'

ep = File.expand_path(File.dirname(__FILE__))

Expand Down
17 changes: 17 additions & 0 deletions lib/nats/server/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/nats/server/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -75,6 +75,7 @@ def setup(argv)
end

setup_logs
open_syslog

# Setup optimized select versions
EM.epoll unless @options[:noepoll]
Expand Down
50 changes: 47 additions & 3 deletions lib/nats/server/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ max_connections: 128
no_epoll: false
no_kqueue: true


# Syslog
syslog: nats_syslog_test
1 change: 1 addition & 0 deletions spec/server_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
11 changes: 11 additions & 0 deletions spec/server_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit b9417de

Please sign in to comment.