Permalink
Browse files

Tighten logging messages

  • Loading branch information...
1 parent 4a7fb2f commit 8ffe72dda18ed92341eb48292d015ab104fb2832 @rwdaigle committed May 11, 2012
Showing with 46 additions and 33 deletions.
  1. +13 −13 Gemfile.lock
  2. +1 −1 app/models/subject.rb
  3. +24 −16 lib/event_logging.rb
  4. +8 −3 lib/tasks/ptls.rake
View
@@ -36,11 +36,11 @@ GEM
diff-lcs (1.1.3)
dynamic_form (1.1.4)
erubis (2.7.0)
- execjs (1.3.1)
+ execjs (1.3.2)
multi_json (~> 1.0)
foreman (0.46.0)
thor (>= 0.13.6)
- haml (3.1.4)
+ haml (3.1.5)
heroku (2.25.0)
launchy (>= 0.3.2)
netrc (~> 0.7.1)
@@ -53,7 +53,7 @@ GEM
jquery-rails (2.0.2)
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
- json (1.7.0)
+ json (1.7.2)
kgio (2.7.4)
launchy (2.1.0)
addressable (~> 2.2.6)
@@ -105,21 +105,21 @@ GEM
json (~> 1.4)
rest-client (1.6.7)
mime-types (>= 1.16)
- rspec (2.9.0)
- rspec-core (~> 2.9.0)
- rspec-expectations (~> 2.9.0)
- rspec-mocks (~> 2.9.0)
- rspec-core (2.9.0)
- rspec-expectations (2.9.1)
+ rspec (2.10.0)
+ rspec-core (~> 2.10.0)
+ rspec-expectations (~> 2.10.0)
+ rspec-mocks (~> 2.10.0)
+ rspec-core (2.10.0)
+ rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
- rspec-mocks (2.9.0)
- rspec-rails (2.9.0)
+ rspec-mocks (2.10.1)
+ rspec-rails (2.10.1)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
- rspec (~> 2.9.0)
+ rspec (~> 2.10.0)
rubyzip (0.9.8)
- sass (3.1.16)
+ sass (3.1.17)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
View
@@ -47,7 +47,7 @@ def to_param; permalink; end
def to_s; title; end
def to_log
- { subject_id: self.id, subject: self.to_s }
+ { subject_id: self.id, subject_title: self.title }
end
private
View
@@ -10,17 +10,25 @@ def hiya; puts "hiya"; end
def log(*segments, &block)
ms = Benchmark.ms { yield } if block_given?
- log_values = { klass: (self.is_a?(Class) ? self.to_s : self.class.to_s), event: self.caller_method }
- segment_values = segments.inject({}) do |map, segment|
+ log_data = default_log_data
+ call_log_data = segments.inject({}) do |map, segment|
map.merge!(segment.to_log)
end
- log_values.merge!(segment_values)
- log_values.merge!(execution: ms.round(3)) if block_given?
- event_logger.info(log_values.to_json)
+ log_data.merge!(call_log_data)
+ log_data.merge!(duration: ms.round(3)) if block_given?
+ event_logger.info(log_data.to_json)
end
- def caller_method(depth=1)
- parse_caller(caller(depth+1).first).last
+ private
+
+ def default_log_data
+ { timestamp: Time.now.utc.to_f, caller: called_from(2) }
+ end
+
+ def called_from(depth=1)
+ caller_method = parse_caller(caller(depth+1).first).last
+ klass = self.is_a?(Class) ? self.to_s : self.class.to_s
+ "#{klass}##{caller_method}"
end
# Stolen from ActionMailer, where this was used but was not made reusable
@@ -38,15 +46,15 @@ def event_logger
end
end
- def log(*tokens, &block)
- ms = Benchmark.ms { yield } if block_given?
- token_str = tokens.collect do |token|
- token.respond_to?(:to_log) ? token.to_log : token.to_s
- end.join(' ')
- execution = ms ? "execution=#{ms.round(3)}ms" : nil
- klass = self.is_a?(Class) ? self : self.class
- self.class.event_logger.info "[#{klass}] event=#{self.class.caller_method} #{token_str} #{execution}"
- end
+ # def log(*tokens, &block)
+ # ms = Benchmark.ms { yield } if block_given?
+ # token_str = tokens.collect do |token|
+ # token.respond_to?(:to_log) ? token.to_log : token.to_s
+ # end.join(' ')
+ # execution = ms ? "execution=#{ms.round(3)}ms" : nil
+ # klass = self.is_a?(Class) ? self : self.class
+ # self.class.event_logger.info "[#{klass}] event=#{self.class.caller_method} #{token_str} #{execution}"
+ # end
end
class Hash
View
@@ -19,15 +19,20 @@ namespace :ptls do
task :import => :environment do
# Get the file to filesystem
- Object.log({event: "ptls:import", file: ENV['VOCABULARY_CSV_URL']}) do
- # Rails.logger.info "Importing words from #{ENV['VOCABULARY_CSV_URL']}"
+ Object.log({caller: "ptls.rake#import", event: "csv-import", file: ENV['VOCABULARY_CSV_URL']}) do
+
file = "tmp/#{Time.now.utc.to_i}-import.csv"
`curl "#{ENV['VOCABULARY_CSV_URL']}" > #{file}`
# Parse
vocab = Subject.vocabulary
CSV.foreach(file) do |row|
- vocab.units.create(question: row[0])
+ unit = vocab.units.create(question: row[0])
+ if(unit && unit.valid?)
+ Object.log({caller: "ptls.rake#import", event: "csv-import-unit" }, unit, { status: "success" })
+ else
+ Object.log({caller: "ptls.rake#import", event: "csv-import-unit" }, unit, { status: "failure" }, { message: unit.errors.full_messages.join(", ") })
+ end
end
end
end

0 comments on commit 8ffe72d

Please sign in to comment.