Class: Logworm::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/logworm_client/logger.rb

Class Method Summary collapse

Class Method Details

.dbObject

Returns a reference to the current backend server



26
27
28
# File 'lib/logworm_client/logger.rb', line 26

def self.db
  $lw_server
end

.flushObject

Sends the entries to the server, if configured Returns the number of entries send, and the time it takes

Warning: may raise Exception if there’s a problem. It’s up to the called to rescue from it in order to continue the processing of a web request, for example.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/logworm_client/logger.rb', line 72

def self.flush
  to_send = $lr_queue.size

  # Return if no entries
  return [0,0] if to_send == 0 

  # Return if no server
  unless $lw_server
    $stderr.puts "\t logworm not configured. #{to_send} entries dropped."
    $lr_queue = [] 
    return [0,0]
  end
  
  startTime = Time.now
  $lw_server.batch_log($lr_queue.to_json)
  $lr_queue = [] 
  endTime = Time.now
  
  [to_send, (endTime - startTime)]
end

.log(table, values = {}) ⇒ Object

Record an entry. Not sent to the servers until ‘flush’ is called

Warning: may raise Exception if there’s a problem. It’s up to the called to rescue from it in order to continue the processing of a web request, for example.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/logworm_client/logger.rb', line 44

def self.log(table, values = {})
  return unless table and (table.is_a? String or table.is_a? Symbol)
  return unless values.is_a? Hash

  # Turn keys into symbols, delete empty ones, rename conflicting ones
  kvalues = values.delete_if {|k,v| k.to_s == ""}.map {|k,v| [k.to_sym, v]}
  kvalues = Hash[*kvalues.flatten]
  [:_ts, :_ts_utc, :_request_id].each do |k|
    kvalues["orig_#{k}".to_sym] = kvalues[k] if kvalues.has_key? k
  end
  
  # Add information
  ts = Time.now.utc
  kvalues[:_ts_utc]     = (ts.to_f * 1000).to_i
  kvalues[:_ts]         = ts.strftime("%Y-%m-%dT%H:%M:%SZ")
  kvalues[:_request_id] = $request_id if $request_id
  
  # Enqueue
  $lr_queue << [table, kvalues]
end

.start_cycleObject

Starts a new cycle: sets a request_id variable, so that all entries (until flush) share that id



34
35
36
# File 'lib/logworm_client/logger.rb', line 34

def self.start_cycle
  $request_id = "#{Thread.current.object_id}-#{(Time.now.utc.to_f * 1000).to_i}"
end

.use_db(db) ⇒ Object

Use a connection to a manually specified server



19
20
21
# File 'lib/logworm_client/logger.rb', line 19

def self.use_db(db)
  $lw_server = db
end

.use_default_dbObject

Use connection to the backend servers specified in environment variable or config file



12
13
14
# File 'lib/logworm_client/logger.rb', line 12

def self.use_default_db
   $lw_server = DB.from_config
end