lib/td/logger/td_logger.rb in td-logger-0.3.0 vs lib/td/logger/td_logger.rb in td-logger-0.3.1

- old
+ new

@@ -34,10 +34,12 @@ apikey = options[:apikey] unless apikey raise ArgumentError, ":apikey options is required" end + debug = !!options[:debug] + require 'thread' require 'stringio' require 'zlib' require 'msgpack' require 'json' @@ -46,11 +48,15 @@ require 'cgi' require 'logger' require 'td/client' @logger = ::Logger.new(STDERR) - @logger.level = ::Logger::INFO + if debug + @logger.level = ::Logger::DEBUG + else + @logger.level = ::Logger::INFO + end @client = TreasureData::Client.new(apikey) @mutex = Mutex.new @cond = ConditionVariable.new @@ -90,12 +96,13 @@ upload(*tuple) } end end - def post(tag, record, time=nil) - time ||= Time.now + def post_with_time(tag, record, time) + @logger.debug { "event: #{tag} #{record.to_json}" rescue nil } + record[:time] ||= time.to_i tag = "#{@tag_prefix}.#{tag}" if @tag_prefix db, table = tag.split('.')[-2, 2] @@ -138,13 +145,21 @@ ensure @mutex.unlock end private + def to_msgpack(msg) + begin + msg.to_msgpack + rescue NoMethodError + JSON.load(JSON.dump(msg)).to_msgpack + end + end + def add(db, table, msg) begin - data = msg.to_msgpack + data = to_msgpack(msg) rescue @logger.error("TreasureDataLogger: Can't convert to msgpack: #{msg.inspect}: #{$!}") return false end @@ -168,18 +183,10 @@ end true end - def to_msgpack(msg) - begin - msg.to_msgpack - rescue NoMethodError - JSON.load(JSON.dump(msg)).to_msgpack - end - end - def try_flush @mutex.synchronize do if @queue.empty? @map.reject! {|(db,table),buffer| @queue << [db, table, buffer] @@ -215,14 +222,15 @@ flushed end def upload(db, table, buffer) - @logger.debug "Uploading event logs to #{db}.#{table} table on Treasure Data" begin out = StringIO.new Zlib::GzipWriter.wrap(out) {|gz| gz.write buffer } stream = StringIO.new(out.string) + + @logger.debug "Uploading event logs to #{db}.#{table} table on Treasure Data (#{stream.size} bytes)" @client.import(db, table, "msgpack.gz", stream, stream.size) rescue TreasureData::NotFoundError unless @auto_create_table raise $!