I want to write in simple logger for my that puts messages in memory and, in background, every X seconds write it to a database.
Here is the buffered logger code:
module BufferedLogger
def buffer
@buffer ||= []
end
def log( message )
buffer << message
end
def write_buffer
while message = buffer.shift do
# save the message in nosql
end
end
def repeat_every( interval )
Thread.new do
loop do
start_time = Time.now
yield
elapsed = Time.now - start_time
sleep([interval - elapsed, 0].max)
end
end
end
extend self
thread = repeat_every(10) do
write_buffer
end
end
In development, this works fine, buffer() access to the same @buffer var in both log and write_buffer method. But as soon as I go to production or staging env, i.e. as soon as I’m behind passenger, this @buffer don’t seem to be shared anymore.
Any pointer?
Since
passengercreate separate process, and how these process will persists depends on passenger’s algorithm, I guess it will not work well as you expect. (btw, I had bad experience in this regard using global variables/class variables.)My suggestion to buffer the log is, use logger like fluentd as intermediate processor. Fluentd can monitor and gather the log. You can write a plugin to write the collected log to DB. I think this will suit your needs.