Module: Egalite::ErrorLogger

Defined in:
lib/egalite.rb

Constant Summary collapse

@@table =
nil
@@admin_emails =
nil
@@email_from =
nil
@@do_not_catch =
false

Class Method Summary collapse

Class Method Details

.admin_emails=(t) ⇒ Object



105
106
107
# File 'lib/egalite.rb', line 105

def admin_emails=(t)
  @@admin_emails=t
end

.catch_exception(sendmail = false, hash = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/egalite.rb', line 136

def catch_exception(sendmail = false, hash = {})
  begin
    yield
  rescue Exception => e
    ErrorLogger.write_exception(e, hash, sendmail)
    if @@do_not_catch
      raise e
    end
  end
end

.create_table(db, opts = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/egalite.rb', line 88

def create_table(db, opts = {})
  table = opts[:table_name] || :logs
  
  db.create_table(table) {
    primary_key :id, :integer, :auto_increment => true
    column :severity, :varchar
    column :ipaddress, :varchar
    column :text, :varchar
    column :url, :varchar
    column :md5, :varchar
    column :created_at, :timestamp
    column :checked_at, :timestamp
  }
end

.do_not_catch=(t) ⇒ Object



111
112
113
# File 'lib/egalite.rb', line 111

def do_not_catch=(t)
  @@do_not_catch=t
end

.email_from=(t) ⇒ Object



108
109
110
# File 'lib/egalite.rb', line 108

def email_from=(t)
  @@email_from=t
end

.table=(t) ⇒ Object



102
103
104
# File 'lib/egalite.rb', line 102

def table=(t)
  @@table=t
end

.write(hash, sendmail = false) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/egalite.rb', line 114

def write(hash, sendmail = false)
  hash[:md5] = Digest::MD5.hexdigest(hash[:text]) unless hash[:md5]
  if (hash[:severity] == 'critical' or sendmail) and @@admin_emails
    Sendmail.send(hash[:text],{
      :from => @@email_from || "[email protected]",
      :to => @@admin_emails,
      :subject => "Exception report from Egalite framework."
    })
  end
  if @@table
    @@table.insert(hash) rescue nil
  end
end

.write_exception(e, hash, sendmail = false) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/egalite.rb', line 127

def write_exception(e, hash, sendmail = false)
  severity = 'exception'
  severity = 'security' if e.is_a?(SecurityError)
  severity = 'critical' if e.is_a?(CriticalError)
  
  text = "#{e.to_s}\n#{e.backtrace.join("\n")}"
  
  ErrorLogger.write({:severity => severity, :text => text}.merge(hash),sendmail)
end