Class: Justlogging::Transaction
- Inherits:
-
Object
- Object
- Justlogging::Transaction
- Defined in:
- lib/justlogging/transaction.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#log_entry ⇒ Object
readonly
Returns the value of attribute log_entry.
Class Method Summary collapse
Instance Method Summary collapse
- #add_event(event) ⇒ Object
- #add_exception(ex) ⇒ Object
- #complete! ⇒ Object
- #exception? ⇒ Boolean
- #formatted_events ⇒ Object
- #formatted_exception ⇒ Object
- #formatted_log_entry ⇒ Object
- #formatted_payload ⇒ Object
-
#initialize(id, env) ⇒ Transaction
constructor
A new instance of Transaction.
- #request ⇒ Object
- #set_log_entry(event) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(id, env) ⇒ Transaction
Returns a new instance of Transaction.
15 16 17 18 19 20 21 |
# File 'lib/justlogging/transaction.rb', line 15 def initialize(id, env) @id = id @events = [] @log_entry = nil @exception = nil @env = env end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
13 14 15 |
# File 'lib/justlogging/transaction.rb', line 13 def env @env end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
13 14 15 |
# File 'lib/justlogging/transaction.rb', line 13 def events @events end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
13 14 15 |
# File 'lib/justlogging/transaction.rb', line 13 def exception @exception end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
13 14 15 |
# File 'lib/justlogging/transaction.rb', line 13 def id @id end |
#log_entry ⇒ Object (readonly)
Returns the value of attribute log_entry.
13 14 15 |
# File 'lib/justlogging/transaction.rb', line 13 def log_entry @log_entry end |
Class Method Details
.create(key, env) ⇒ Object
4 5 6 7 |
# File 'lib/justlogging/transaction.rb', line 4 def self.create(key, env) Thread.current[:justlogging_transaction_id] = key Justlogging.transactions[key] = Justlogging::Transaction.new(key, env) end |
.current ⇒ Object
9 10 11 |
# File 'lib/justlogging/transaction.rb', line 9 def self.current Justlogging.transactions[Thread.current[:justlogging_transaction_id]] end |
Instance Method Details
#add_event(event) ⇒ Object
31 32 33 |
# File 'lib/justlogging/transaction.rb', line 31 def add_event(event) @events << event end |
#add_exception(ex) ⇒ Object
35 36 37 |
# File 'lib/justlogging/transaction.rb', line 35 def add_exception(ex) @exception = ex end |
#complete! ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/justlogging/transaction.rb', line 101 def complete! Thread.current[:justlogging_transaction_id] = nil current_transaction = Justlogging.transactions.delete(@id) if @events.any? || exception? Justlogging.agent.add_to_queue(current_transaction.to_hash) end end |
#exception? ⇒ Boolean
39 40 41 |
# File 'lib/justlogging/transaction.rb', line 39 def exception? !! @exception end |
#formatted_events ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/justlogging/transaction.rb', line 52 def formatted_events @events.map do |event| { :name => event.name, :duration => event.duration, :time => event.time, :end => event.end, :payload => event.payload } end end |
#formatted_exception ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/justlogging/transaction.rb', line 43 def formatted_exception return {} unless exception? { :backtrace => @exception.backtrace, :exception => @exception.name, :message => @exception. } end |
#formatted_log_entry ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/justlogging/transaction.rb', line 64 def formatted_log_entry { :name => request.fullpath, :environment => Rails.env, :server => @env['SERVER_NAME'], :kind => 'http_request' }.merge(formatted_payload) end |
#formatted_payload ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/justlogging/transaction.rb', line 73 def formatted_payload if @log_entry { :duration => @log_entry.duration, :time => @log_entry.time, :end => @log_entry.end }.merge(@log_entry.payload).tap do |o| o[:action] = "#{o.delete(:controller)}##{o[:action]}" end else if exception? {:action => @exception.inspect.gsub(/^<#(.*)>$/, '\1')} else {} end end end |
#request ⇒ Object
23 24 25 |
# File 'lib/justlogging/transaction.rb', line 23 def request ActionDispatch::Request.new(@env) end |
#set_log_entry(event) ⇒ Object
27 28 29 |
# File 'lib/justlogging/transaction.rb', line 27 def set_log_entry(event) @log_entry = event end |
#to_hash ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/justlogging/transaction.rb', line 91 def to_hash { :request_id => @id, :log_entry => formatted_log_entry, :events => formatted_events, :exception => formatted_exception, :failed => exception.present? } end |