Skip to content

Commit

Permalink
add --no-syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Jan 10, 2008
1 parent 220164a commit 8b62e33
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
pkg
*.log
logs
4 changes: 4 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
== 0.6.7 /
* Minor Enhancements
* Add --no-syslog option to disable Syslog

== 0.6.6 / 2008-01-07
* Bug Fixes
* Redo Timer mutexing to reduce synchronization needs
Expand Down
10 changes: 7 additions & 3 deletions bin/god
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require 'optparse'
require 'drb'

begin
options = {:daemonize => true, :port => 17165}
options = {:daemonize => true, :port => 17165, :syslog => true}

opts = OptionParser.new do |opts|
opts.banner = <<-EOF
Expand Down Expand Up @@ -72,15 +72,19 @@ begin
options[:info] = true
end

opts.on("--log-level LEVEL", "Log level [debug|info|fatal]") do |x|
opts.on("--log-level LEVEL", "Log level [debug|info|warn|error|fatal]") do |x|
options[:log_level] = x.to_sym
end

opts.on("--no-syslog", "Disable output to syslog") do
options[:syslog] = false
end
end

opts.parse!

# validate
if options[:log_level] && ![:debug, :info, :fatal].include?(options[:log_level])
if options[:log_level] && ![:debug, :info, :warn, :error, :fatal].include?(options[:log_level])
abort("Invalid log level '#{options[:log_level]}'")
end

Expand Down
14 changes: 6 additions & 8 deletions lib/god.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
require 'logger'

# stdlib
require 'syslog'

# internal requires
require 'god/errors'
Expand Down Expand Up @@ -58,6 +57,8 @@
require 'god/cli/version'
require 'god/cli/command'

require 'god/diagnostics'

$:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god])

# App wide logging system
Expand All @@ -76,13 +77,6 @@ def applog(watch, level, text)

GOD_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

# Ensure that Syslog is open
begin
Syslog.open('god')
rescue RuntimeError
Syslog.reopen('god')
end

# Return the binding of god's root level
def root_binding
binding
Expand Down Expand Up @@ -196,6 +190,8 @@ def self.internal_init
# log level
log_level_map = {:debug => Logger::DEBUG,
:info => Logger::INFO,
:warn => Logger::WARN,
:error => Logger::ERROR,
:fatal => Logger::FATAL}
LOG.level = log_level_map[self.log_level]

Expand Down Expand Up @@ -566,6 +562,8 @@ def self.start
# mark as running
self.running = true

# start_dike

# join the timer thread so we don't exit
Timer.get.join
end
Expand Down
12 changes: 9 additions & 3 deletions lib/god/cli/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,19 @@ def run_daemonized
God.pid = @options[:pid]
end

# set log level if requested
if @options[:log_level]
God.log_level = @options[:log_level]
unless @options[:syslog]
Logger.syslog = false
end

# load config
if @options[:config]
# set log level, defaults to WARN
if @options[:log_level]
God.log_level = @options[:log_level]
else
God.log_level = :warn
end

unless File.exist?(@options[:config])
abort "File not found: #{@options[:config]}"
end
Expand Down
10 changes: 10 additions & 0 deletions lib/god/diagnostics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def start_dike
require 'dike'
Thread.new do
Dike.logfactory File.join(File.dirname(__FILE__), *%w[.. .. logs])
loop do
Dike.finger
sleep(1)
end
end
end
73 changes: 61 additions & 12 deletions lib/god/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,48 @@ class Logger < ::Logger

attr_accessor :logs

class << self
attr_accessor :syslog
end

self.syslog ||= true

# Instantiate a new Logger object
def initialize
super($stdout)
self.logs = {}
@mutex = Mutex.new
@capture = nil
load_syslog
end

def start_capture
@mutex.synchronize do
@capture = StringIO.new
end
end

def finish_capture
@mutex.synchronize do
cap = @capture.string
@capture = nil
cap
# If Logger.syslog is true then attempt to load the syslog bindings. If syslog
# cannot be loaded, then set Logger.syslog to false and continue.
#
# Returns nothing
def load_syslog
return unless Logger.syslog

begin
require 'syslog'

# Ensure that Syslog is open
begin
Syslog.open('god')
rescue RuntimeError
Syslog.reopen('god')
end
rescue Exception
Logger.syslog = false
end
end

# Log a message
# +watch+ is the String name of the Watch (may be nil if not Watch is applicable)
# +level+ is the log level [:debug|:info|:warn|:error|:fatal]
# +text+ is the String message
#
# Returns nothing
def log(watch, level, text)
# initialize watch log if necessary
self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT) if watch
Expand All @@ -49,9 +70,14 @@ def log(watch, level, text)
self.send(level, text % [])

# send to syslog
Syslog.send(SYSLOG_EQUIVALENTS[level], text)
Syslog.send(SYSLOG_EQUIVALENTS[level], text) if Logger.syslog
end

# Get all log output for a given Watch since a certain Time.
# +watch_name+ is the String name of the Watch
# +since+ is the Time since which to fetch log lines
#
# Returns String
def watch_log_since(watch_name, since)
# initialize watch log if necessary
self.logs[watch_name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT)
Expand All @@ -65,6 +91,29 @@ def watch_log_since(watch_name, since)
end.join
end
end

# private

# Enable capturing of log
#
# Returns nothing
def start_capture
@mutex.synchronize do
@capture = StringIO.new
end
end

# Disable capturing of log and return what was captured since
# capturing was enabled with Logger#start_capture
#
# Returns String
def finish_capture
@mutex.synchronize do
cap = @capture.string
@capture = nil
cap
end
end
end

end
1 change: 1 addition & 0 deletions lib/god/timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def initialize
applog(nil, :fatal, message)
ensure
# sleep until next check
GC.start
sleep INTERVAL
end
end
Expand Down

0 comments on commit 8b62e33

Please sign in to comment.