Skip to content

Commit

Permalink
write to log or write to console
Browse files Browse the repository at this point in the history
  • Loading branch information
tompesman committed Aug 25, 2012
1 parent 41edf04 commit f1b6169
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ To start the daemon:

Where `<environment>` is your Rails environment and `<options>` can be:

-f, --foreground Run in the foreground.
-f, --foreground Run in the foreground. Log is not written.
-p, --pid-file PATH Path to write PID file. Relative to Rails root unless absolute.
-P, --push-poll N Frequency in seconds to check for new notifications. Default: 2.
-n, --airbrake-notify Enables error notifications via Airbrake.
Expand Down
2 changes: 1 addition & 1 deletion bin/push
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ config.feedback_processor = 'lib/push/feedback_processor'
banner = 'Usage: push <Rails environment> [options]'
ARGV.options do |opts|
opts.banner = banner
opts.on('-f', '--foreground', 'Run in the foreground.') { config.foreground = true }
opts.on('-f', '--foreground', 'Run in the foreground. Log is not written.') { config.foreground = true }
opts.on('-p PATH', '--pid-file PATH', String, 'Path to write PID file. Relative to Rails root unless absolute.') { |path| config.pid_file = path }
opts.on('-P N', '--push-poll N', Integer, "Frequency in seconds to check for new notifications. Default: #{config.push_poll}.") { |n| config.push_poll = n }
opts.on('-n', '--airbrake-notify', 'Enables error notifications via Airbrake.') { config.check_for_errors = true }
Expand Down
20 changes: 14 additions & 6 deletions lib/push/daemon/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module Daemon
class Logger
def initialize(options)
@options = options
log_file = File.open(File.join(Rails.root, 'log', 'push.log'), 'w')
log_file.sync = true
@logger = ActiveSupport::BufferedLogger.new(log_file, Rails.logger.level)
@logger.auto_flushing = Rails.logger.respond_to?(:auto_flushing) ? Rails.logger.auto_flushing : true
open_log unless @options[:foreground]
end

def info(msg)
Expand All @@ -32,8 +29,19 @@ def log(where, msg, prefix = nil)
formatted_msg = "[#{Time.now.to_s(:db)}] "
formatted_msg << "[#{prefix}] " if prefix
formatted_msg << msg
puts formatted_msg if @options[:foreground]
@logger.send(where, formatted_msg)

if @options[:foreground]
puts formatted_msg
else
@logger.send(where, formatted_msg)
end
end

def open_log
log_file = File.open(File.join(Rails.root, 'log', 'push.log'), 'w')
log_file.sync = true
@logger = ActiveSupport::BufferedLogger.new(log_file, Rails.logger.level)
@logger.auto_flushing = Rails.logger.respond_to?(:auto_flushing) ? Rails.logger.auto_flushing : true
end

def airbrake_notify(e)
Expand Down

0 comments on commit f1b6169

Please sign in to comment.