Class: Logworm::Logger
- Inherits:
-
Object
- Object
- Logworm::Logger
- Defined in:
- lib/client/logger.rb
Class Method Summary collapse
-
.apache_log(ip, method, path, env, status, response_headers) ⇒ Object
Utility function.
-
.attach_to_log(table, values = {}) ⇒ Object
Allows to add fields to ALL log entries that go to a particular table.
-
.db ⇒ Object
Returns a reference to the current backend server.
-
.flush ⇒ Object
Sends the entries to the server, if configured Returns the number of entries send, and the time it takes.
-
.log(table, values = {}) ⇒ Object
Record an entry.
-
.start_cycle ⇒ Object
Starts a new cycle: sets a request_id variable, so that all entries (until flush) share that id.
-
.use_db(db) ⇒ Object
Use a connection to a manually specified server.
-
.use_default_db ⇒ Object
Use connection to the backend servers specified in environment variable or config file.
Class Method Details
.apache_log(ip, method, path, env, status, response_headers) ⇒ Object
Utility function
111 112 113 114 115 116 117 118 |
# File 'lib/client/logger.rb', line 111 def self.apache_log(ip, method, path, env, status, response_headers) if response_headers['Content-Length'] response_length = response_headers['Content-Length'].to_s == '0' ? '-' : response_headers['Content-Length'] else response_length = "-" end "#{ip} - #{env["REMOTE_USER"] || "-"} [#{Time.now.strftime("%d/%b/%Y:%H:%M:%S %z")}] \"#{method} #{path} #{env['HTTP_VERSION']}\" #{status} #{response_length} \"#{env['HTTP_REFERER']}\" \"#{env['HTTP_USER_AGENT']}\"" end |
.attach_to_log(table, values = {}) ⇒ Object
Allows to add fields to ALL log entries that go to a particular table. Can be used, for example, to add user-level and app-specific data to the :web_log log Notice that it overwrites previously attached fields. For example
attach_to_log(:xx, {:a = 1})
attach_to_log(:xx, {:a = 2})
will end up attaching just :a => 2 to every :xx log
72 73 74 |
# File 'lib/client/logger.rb', line 72 def self.attach_to_log(table, values = {}) $attaches[table] = ($attaches[table] ? $attaches[table].merge(values) : values) end |
.db ⇒ Object
Returns a reference to the current backend server
27 28 29 |
# File 'lib/client/logger.rb', line 27 def self.db $lw_server end |
.flush ⇒ Object
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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/client/logger.rb', line 83 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." return [0,0] end # Apply a-posteriori attachments # It merges, and so that attachment may overwrite $lr_queue.each do |le| le[1].merge!($attaches[le[0]]) if $attaches[le[0]] end startTime = Time.now $lw_server.batch_log($lr_queue.to_json) 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/client/logger.rb', line 47 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 = cleanup(values) # 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_cycle ⇒ Object
Starts a new cycle: sets a request_id variable, so that all entries (until flush) share that id
35 36 37 38 39 |
# File 'lib/client/logger.rb', line 35 def self.start_cycle $request_id = "#{Thread.current.object_id}-#{(Time.now.utc.to_f * 1000).to_i}" $lr_queue = [] $attaches = {} end |
.use_db(db) ⇒ Object
Use a connection to a manually specified server
20 21 22 |
# File 'lib/client/logger.rb', line 20 def self.use_db(db) $lw_server = db end |
.use_default_db ⇒ Object
Use connection to the backend servers specified in environment variable or config file
13 14 15 |
# File 'lib/client/logger.rb', line 13 def self.use_default_db $lw_server = DB.from_config end |