Created
August 20, 2011 06:49
-
-
Save ox/1158776 to your computer and use it in GitHub Desktop.
Networked Logging in Ruby Using ZeroMQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zmq' | |
class Logger | |
def initialize | |
@context = ZMQ::Context.new | |
@pub = @context.socket ZMQ::PUB | |
@pub.bind "ipc://logger:general" | |
end | |
def log(message) | |
@pub.send message | |
end | |
end | |
logger = Logger.new | |
random_thread = Thread.new do | |
loop do | |
logger.log(rand().to_s) | |
sleep 1 | |
end | |
end | |
random_thread.join |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#a networked log writer. hopefully I can attach whatever | |
#end i want to it, so that this will take care of all of | |
#the logging needs, keeping the app running fast. | |
require 'zmq' | |
context = ZMQ::Context.new | |
#pubsub | |
sub = context.socket ZMQ::SUB | |
sub.setsockopt ZMQ::SUBSCRIBE, "" | |
sub.connect "ipc://logger:general" | |
file = File.new('current.log','a+') | |
listening_thread = Thread.new do | |
loop do | |
message = sub.recv | |
unless message.empty? | |
file.puts "got log: #{message}" | |
else | |
break | |
end | |
end | |
end | |
puts "thread set up." | |
puts "I'm listening for messages!" | |
listening_thread.join | |
file.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment