Class: TransactionLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/transaction_logger.rb,
lib/transaction_logger/version.rb

Defined Under Namespace

Classes: Transaction

Constant Summary collapse

VERSION =
"0.1.0"
@@prefix =
""
@@current_transactions =
{}

Class Method Summary collapse

Class Method Details

.log_prefix=(prefix) ⇒ Object

Sets the hash keys on the TransactionLogger's log to have a prefix.

Using .log_prefix "str_", the output of the log hash will contain keys prefixed with "str_", such as { "str_name" => "Class.method" }.

Parameters:

  • prefix (#to_s)

    Any String or Object that responds to to_s



64
65
66
# File 'lib/transaction_logger.rb', line 64

def self.log_prefix=(prefix)
  @@prefix = "#{prefix}"
end

.loggerObject

Sets the TransactionLogger's output to a new instance of Logger



53
54
55
# File 'lib/transaction_logger.rb', line 53

def self.logger
  @@logger ||= Logger.new(STDOUT)
end

.logger=(logger) ⇒ Object

Sets the TransactionLogger's output to a specific instance of Logger.

Parameters:

  • logger (Logger)

    Any instace of ruby Logger



47
48
49
# File 'lib/transaction_logger.rb', line 47

def self.logger=(logger)
  @@logger = logger
end

.start(lmbda) ⇒ Object

Marks the beginning of a "Transaction lambda," which will log an error if the containing code raises an error. A lambda instance variable let's you call the .log method and access the ".name" and ".context" variables. The start method must take a lambda as an argument.

Whatever the outer method is, if a value is returned within the Transaction lambda it will be returned to the outer method as well.

The start method does not catch errors, so if an error is raised, it will simply envoke a logging message to be outputted and then raise the error.

This also checks which thread is envoking the method in order to make sure the logs are thread-safe.

Parameters:

  • lmbda (Proc)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/transaction_logger.rb', line 25

def self.start(lmbda)

  active_transaction = get_active_transaction

  transaction = TransactionLogger::Transaction.new active_transaction, lmbda
  active_transaction = transaction

  set_active_transaction active_transaction

  begin
    transaction.run
  rescue Exception => e
    raise e
  ensure
    set_active_transaction transaction.parent
  end
end