Class: Platform::LoggedException
- Defined in:
- app/models/platform/logged_exception.rb
Overview
– Copyright © 2010-2012 Michael Berkovich
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++
– Platform::LoggedException Schema Information
Table name: platform_logged_exceptions
id INTEGER not null, primary key
exception_class varchar(255)
controller_name varchar(255)
action_name varchar(255)
server varchar(255)
message text
backtrace text
environment text
request text
session text
cause blob
user_id integer
application_id integer
created_at datetime
updated_at datetime
Indexes
++
Class Method Summary collapse
- .action_name_summary(opts = nil) ⇒ Object
- .action_names ⇒ Object
- .action_summary(opts = nil) ⇒ Object
- .actions ⇒ Object
- .controller_summary(opts = nil) ⇒ Object
- .controllers ⇒ Object
- .create_from_exception(controller, exception, data) ⇒ Object
- .date_summary(opts = nil) ⇒ Object
- .dates ⇒ Object
- .exception_class_summary(opts = nil) ⇒ Object
- .exception_report(date) ⇒ Object
- .exception_summary(opts = nil) ⇒ Object
- .exceptions ⇒ Object
- .host_name ⇒ Object
- .log(exception, opts = nil) ⇒ Object
- .purge(days_to_keep) ⇒ Object
Instance Method Summary collapse
- #backtrace=(backtrace) ⇒ Object
- #controller_action ⇒ Object
- #rejected_parameters ⇒ Object
- #request=(request) ⇒ Object
- #session=(session) ⇒ Object
Class Method Details
.action_name_summary(opts = nil) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/platform/logged_exception.rb', line 128 def self.action_name_summary(opts=nil) opts ||= {} order = opts[:order] || 'action_name' where_clause = opts[:conditions] ? "where #{sanitize_sql(opts[:conditions])}" : nil connection.select_all(%Q{ select action_name, count(*) from #{table_name} #{where_clause} group by action_name order by #{order} }) end |
.action_names ⇒ Object
121 122 123 124 125 126 |
# File 'app/models/platform/logged_exception.rb', line 121 def self.action_names @action_names ||= connection.select_values(%Q{ select distinct action_name from #{table_name} }) end |
.action_summary(opts = nil) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'app/models/platform/logged_exception.rb', line 167 def self.action_summary(opts=nil) opts ||= {} order = opts[:order] || 'controller_name, action_name' where_clause = opts[:conditions] ? "where #{sanitize_sql(opts[:conditions])}" : nil connection.select_all(%Q{ select controller_name, action_name, count(*) from #{table_name} #{where_clause} group by controller_name, action_name order by #{order} }) end |
.actions ⇒ Object
113 114 115 116 117 118 119 |
# File 'app/models/platform/logged_exception.rb', line 113 def self.actions @actions ||= connection.select_all(%Q{ select controller_name, action_name from #{table_name} group by controller_name, action_name }).map {|ii| "#{ii['controller_name']}/#{ii['action_name']}"} end |
.controller_summary(opts = nil) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/models/platform/logged_exception.rb', line 154 def self.controller_summary(opts=nil) opts ||= {} order = opts[:order] || 'controller_name' where_clause = opts[:conditions] ? "where #{sanitize_sql(opts[:conditions])}" : nil connection.select_all(%Q{ select controller_name, count(*) from #{table_name} #{where_clause} group by controller_name order by #{order} }) end |
.controllers ⇒ Object
106 107 108 109 110 111 |
# File 'app/models/platform/logged_exception.rb', line 106 def self.controllers @controllers ||= connection.select_values(%Q{ select distinct controller_name from #{table_name} }) end |
.create_from_exception(controller, exception, data) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/platform/logged_exception.rb', line 56 def self.create_from_exception(controller, exception, data) = exception..inspect << "\n* Extra Data\n\n#{Iconv.conv('UTF-8//IGNORE', 'UTF-8', data)}" unless data.blank? create!( :exception_class => exception.class.name, :controller_name => controller.controller_name, :action_name => controller.action_name, :server => host_name, :message => , :backtrace => exception.backtrace, :session => controller.request.session, :request => controller.request, :user_id => Platform::Config.current_user.try(:id) # :cause => exception.try(:cause) ) rescue StandardError => ex pp ex logger.warn { "Unable to log this error: #{exception.}\n#{exception.backtrace.inspect}" } logger.warn { "Because: #{ex.}\n#{ex.backtrace.inspect}" } end |
.date_summary(opts = nil) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'app/models/platform/logged_exception.rb', line 208 def self.date_summary(opts=nil) opts ||= {} order = opts[:order] || 'datestamp desc' where_clause = opts[:conditions] ? "where #{sanitize_sql(opts[:conditions])}" : nil connection.select_all(%Q{ select date(created_at) as datestamp, count(*) from #{table_name} #{where_clause} group by datestamp order by #{order} }) end |
.dates ⇒ Object
201 202 203 204 205 206 |
# File 'app/models/platform/logged_exception.rb', line 201 def self.dates @dates ||= connection.select_values(%Q{ select distinct date(created_at) from #{table_name} }) end |
.exception_class_summary(opts = nil) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'app/models/platform/logged_exception.rb', line 141 def self.exception_class_summary(opts=nil) opts ||= {} order = opts[:order] || 'exception_class' where_clause = opts[:conditions] ? "where #{sanitize_sql(opts[:conditions])}" : nil connection.select_all(%Q{ select exception_class, count(*) from #{table_name} #{where_clause} group by exception_class order by #{order} }) end |
.exception_report(date) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/models/platform/logged_exception.rb', line 221 def self.exception_report(date) connection.select_all(%Q{ select exception_class, controller_name, action_name, count(*) as count from #{table_name} where date(created_at) = '#{date}' group by exception_class, controller_name, action_name }).inject({}) do |ret, hash| ex = hash['exception_class'] controller_action = "#{hash['controller_name']}/#{hash['action_name']}" ret[ex] ||= {} ret[ex][:total] ||= 0 ret[ex][:total] += hash['count'].to_i ret[ex][controller_action] = hash['count'] ret end end |
.exception_summary(opts = nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'app/models/platform/logged_exception.rb', line 188 def self.exception_summary(opts=nil) opts ||= {} order = opts[:order] || 'exception_class' where_clause = opts[:conditions] ? "where #{sanitize_sql(opts[:conditions])}" : nil connection.select_all(%Q{ select exception_class, count(*) from #{table_name} #{where_clause} group by exception_class order by #{order} }) end |
.exceptions ⇒ Object
180 181 182 183 184 185 186 |
# File 'app/models/platform/logged_exception.rb', line 180 def self.exceptions @exceptions ||= connection.select_values(%Q{ select distinct exception_class from #{table_name} order by exception_class }) end |
.host_name ⇒ Object
244 245 246 |
# File 'app/models/platform/logged_exception.rb', line 244 def self.host_name @host_name ||= Socket.gethostname end |
.log(exception, opts = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/platform/logged_exception.rb', line 77 def self.log(exception, opts=nil) opts ||= {} controller = opts[:controller] || opts[:class] || '' action = opts[:action] || opts[:method] || '' unless Rails.env.test? puts "Caught Exception #{exception.}" puts exception.backtrace.join("\n") if exception.backtrace end ret = create( :exception_class => exception.class.name, :server => host_name, :message => exception..inspect, :backtrace => exception.backtrace, :controller_name => controller.to_s, :action_name => action.to_s, :user_id => Platform::Config.current_user.try(:id), :cause => exception.try(:cause) ) email_exception(ret, opts[:mail_to]) if opts[:mail_to] ret rescue ActiveRecord::ActiveRecordError # don't puke if we can't log the exception end |
.purge(days_to_keep) ⇒ Object
239 240 241 242 |
# File 'app/models/platform/logged_exception.rb', line 239 def self.purge(days_to_keep) days_to_keep -= 1 # include today's logs delete_all(["created_at <= ?", Date.today - days_to_keep]) end |
Instance Method Details
#backtrace=(backtrace) ⇒ Object
248 249 250 251 252 |
# File 'app/models/platform/logged_exception.rb', line 248 def backtrace=(backtrace) return if backtrace.nil? backtrace = sanitize_backtrace(backtrace) * "\n" unless backtrace.is_a?(String) write_attribute :backtrace, backtrace end |
#controller_action ⇒ Object
292 293 294 |
# File 'app/models/platform/logged_exception.rb', line 292 def controller_action @controller_action ||= "#{controller_name.camelcase}/#{action_name}" end |
#rejected_parameters ⇒ Object
288 289 290 |
# File 'app/models/platform/logged_exception.rb', line 288 def rejected_parameters [:password] end |
#request=(request) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'app/models/platform/logged_exception.rb', line 268 def request=(request) if request.is_a?(String) write_attribute :request, request else request.env['Process'] = $$ max = request.env.keys.max { |a,b| a.length <=> b.length } env = request.env.keys.sort.inject([]) do |array, key| array << '* ' + ("%-*s: %s" % [max.length, key, request.env[key].to_s.strip]) end write_attribute(:environment, env.join("\n")) write_attribute(:request, [ "* URL:#{" #{request.method.to_s.upcase}" unless request.get?} #{request.protocol}#{request.env["HTTP_HOST"]}#{request.url}", "* Format: #{request.format.to_s}", "* Parameters: #{request.parameters.reject{|key, value| rejected_parameters.include?(key.to_sym)}.inspect}", "* Rails Root: #{Rails.root}" ] * "\n") end end |
#session=(session) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'app/models/platform/logged_exception.rb', line 254 def session=(session) case session when String write_attribute(:session, session) else write_attribute(:session, [:@session_id, :@data].map do |variable| "* #{variable}:\n" << PP.pp(session.instance_variable_get(variable), '').gsub(/\n/, "\n ").strip end * "\n" ) end end |