Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gentle reconnection #23

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1bc225e
adds rabbit outputter pre-configured for mtnsat
deanshelton913 Jun 24, 2013
4673dbb
feature request @Pat C.: Change Auto_Delete to FALSE
deanshelton913 Jun 25, 2013
fad1fb0
adds duribility:true
deanshelton913 Jun 25, 2013
f233f30
update rabbit outputter to take external yaml configurations
deanshelton913 Jul 3, 2013
538435d
remove extra debug output
deanshelton913 Jul 3, 2013
5ef456f
update version
deanshelton913 Jul 3, 2013
1177231
repair misnamed keys for rabbitmq config
deanshelton913 Jul 3, 2013
b4735e2
update config debug output
deanshelton913 Jul 3, 2013
31b8751
update formatter to sanitize all text with "quotes"
deanshelton913 Jul 9, 2013
b460de7
adding recovery
thegboat Sep 6, 2013
2237b09
adding recovery
thegboat Sep 6, 2013
fadee93
Merge pull request #1 from thegboat/master
deanshelton913 Sep 27, 2013
06cbbaf
expanding RabbitOutputter with environment settings
thegboat Nov 8, 2013
cdf82be
expanding RabbitOutputter with environment settings
thegboat Nov 8, 2013
6e84ef5
expanding RabbitOutputter with environment settings
thegboat Nov 8, 2013
2f74130
expanding RabbitOutputter with environment settings
thegboat Nov 8, 2013
7c6421b
expanding the yml config capabilities
thegboat Nov 8, 2013
1fdbb0e
reading multiple yml file formats
thegboat Nov 8, 2013
f97e692
refactoring configuration file load
thegboat Nov 12, 2013
d3d0fdd
refactoring configuration file load
thegboat Nov 12, 2013
26d71cc
adding call to load_config
thegboat Nov 12, 2013
f48db47
fixing paht to config file
thegboat Nov 12, 2013
892d87d
fixing path to config file
thegboat Nov 12, 2013
21ec72a
Merge branch 'master' of github.com:MTNSatelliteComm/log4r
thegboat Nov 12, 2013
36f8a3a
Merge pull request #2 from thegboat/master
thegboat Nov 15, 2013
b810f20
Merge branch 'master' of github.com:MTNSatelliteComm/log4r
thegboat Dec 17, 2013
969132d
making a single publish event per request for logs
thegboat Dec 19, 2013
f7a7911
using stringio didnt work using array concat
thegboat Dec 19, 2013
8b0c974
Merge branch 'cleaner_rabbit_output'
thegboat Dec 20, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source :gemcutter

gem 'bunny', :git => "git://github.com/ruby-amqp/bunny.git", :branch => "master"
gemspec
1 change: 1 addition & 0 deletions lib/log4r.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "log4r/outputter/fileoutputter"
require "log4r/outputter/consoleoutputters"
require "log4r/outputter/staticoutputter"
require "log4r/outputter/rabbitoutputter"
require "log4r/outputter/rollingfileoutputter"
require "log4r/formatter/patternformatter"
require "log4r/loggerfactory"
Expand Down
18 changes: 11 additions & 7 deletions lib/log4r/formatter/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Version:: $Id$

require "singleton"

require 'json'
require "log4r/base"

module Log4r
Expand Down Expand Up @@ -68,13 +68,17 @@ def format(event)
# +inspect+. An example -- Array: [1, 2, 3]

def format_object(obj)
if obj.kind_of? Exception
klass = obj.class
case
when klass == Exception
return "Caught #{obj.class}: #{obj.message}\n\t" +\
(obj.backtrace.nil? ? [] : obj.backtrace[0...@depth]).join("\n\t")
elsif obj.kind_of? String
return obj
else # inspect the object
return "#{obj.class}: #{obj.inspect}"
(obj.backtrace.nil? ? [] : obj.backtrace[0...@depth]).join("\n\t").gsub('"','\\\\"')
when klass == Hash
return obj.to_json.gsub('"','\\\\"')
when klass == String
return obj.gsub('"','\\\\"')
else # inspect the object
return "#{obj.class}: #{obj.inspect}".gsub('"','\\\\"')
end
end
end
Expand Down
85 changes: 85 additions & 0 deletions lib/log4r/outputter/rabbitoutputter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# :nodoc:
require "rubygems"
require 'logger'
require "log4r/outputter/outputter"
require 'bunny'
require 'yaml'
require 'stringio'

module Log4r
# See log4r/logserver.rb
class RabbitOutputter < Outputter

def initialize(_name, hash={})
# Configuration defaults
super(_name, hash)
stderr_log "Unable to find rabbit configuration file" unless load_config
@config ||= {:host => "localhost"}
@config.symbolize_keys!
@queue_name = @config.delete(:queue) || ''
init_buffer
start_bunny rescue nil
end

def load_config_file(name)
path = "#{Rails.root}/config/#{name}"
if File.exist?(path)
@config = YAML::load(IO.read(path))
end
end

def load_config
@config = if load_config_file("bunny.yml")
@config[Rails.env]
else
load_config_file("rabbitmq.yml")
end
end

def start_bunny
begin
stderr_log "Starting Bunny Client"
config = @config.clone
config[:pass] &&= "**redacted**"
stderr_log config
@conn = Bunny.new @config
@conn.start
create_channel
rescue Bunny::TCPConnectionFailed => e
stderr_log "rescued from: #{e}. Unable to connect to Rabbit Server"
end
end

def stderr_log(msg)
$stderr.puts "[#{Time.now.utc}] #{msg}"
end

def create_channel
ch = @conn.create_channel
@queue = ch.queue(@queue_name, auto_delete: false, durable: true)
end

def flush
@queue.publish(read_buffer, { routing_key: @queue.name }) if @conn.connected? and @queue
init_buffer
rescue Exception => e
@conn.send(:handle_network_failure, e)
create_channel if @conn.connected?
end

private

def init_buffer
@buffered_data = []
end

def write(data)
@buffered_data << data.gsub(/\s+$/,'')
end

def read_buffer
@buffered_data.join("\n")
end

end
end
2 changes: 1 addition & 1 deletion lib/log4r/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Log4r
Log4rVersion = [1, 1, 10].join '.' # deprecate?
Log4rVersion = [1, 1, 11].join '.' # deprecate?
VERSION = Log4rVersion
end