Class: Platform::LoggedException
- Defined in:
- app/models/platform/logged_exception.rb
Overview
– Copyright © 2011 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. ++
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
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/models/platform/logged_exception.rb', line 103 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
96 97 98 99 100 101 |
# File 'app/models/platform/logged_exception.rb', line 96 def self.action_names @action_names ||= connection.select_values(%Q{ select distinct action_name from #{table_name} }) end |
.action_summary(opts = nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/models/platform/logged_exception.rb', line 142 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
88 89 90 91 92 93 94 |
# File 'app/models/platform/logged_exception.rb', line 88 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
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/platform/logged_exception.rb', line 129 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
81 82 83 84 85 86 |
# File 'app/models/platform/logged_exception.rb', line 81 def self.controllers @controllers ||= connection.select_values(%Q{ select distinct controller_name from #{table_name} }) end |
.create_from_exception(controller, exception, data) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/models/platform/logged_exception.rb', line 31 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
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'app/models/platform/logged_exception.rb', line 183 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
176 177 178 179 180 181 |
# File 'app/models/platform/logged_exception.rb', line 176 def self.dates @dates ||= connection.select_values(%Q{ select distinct date(created_at) from #{table_name} }) end |
.exception_class_summary(opts = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/models/platform/logged_exception.rb', line 116 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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/models/platform/logged_exception.rb', line 196 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
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'app/models/platform/logged_exception.rb', line 163 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
155 156 157 158 159 160 161 |
# File 'app/models/platform/logged_exception.rb', line 155 def self.exceptions @exceptions ||= connection.select_values(%Q{ select distinct exception_class from #{table_name} order by exception_class }) end |
.host_name ⇒ Object
219 220 221 |
# File 'app/models/platform/logged_exception.rb', line 219 def self.host_name @host_name ||= Socket.gethostname end |
.log(exception, opts = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/platform/logged_exception.rb', line 52 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
214 215 216 217 |
# File 'app/models/platform/logged_exception.rb', line 214 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
223 224 225 226 227 |
# File 'app/models/platform/logged_exception.rb', line 223 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
267 268 269 |
# File 'app/models/platform/logged_exception.rb', line 267 def controller_action @controller_action ||= "#{controller_name.camelcase}/#{action_name}" end |
#rejected_parameters ⇒ Object
263 264 265 |
# File 'app/models/platform/logged_exception.rb', line 263 def rejected_parameters [:password] end |
#request=(request) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'app/models/platform/logged_exception.rb', line 243 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
229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'app/models/platform/logged_exception.rb', line 229 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 |