Skip to content

Instantly share code, notes, and snippets.

@ctdk
Created June 12, 2009 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctdk/128997 to your computer and use it in GitHub Desktop.
Save ctdk/128997 to your computer and use it in GitHub Desktop.
# experiments with logging!
# Logging hook.
before :save, :story_log
# hmm?
before :save!, :story_log
# Logging goodies
def story_log
logopts = Hash.new
#logopts[:type] = (self.id) ? "story_update" : "story_new"
# don't log story creation now, just updates. Maybe not even
# all of those
return if self.id.nil?
# should break up the types a bit more. For now
logopts[:type] = "story_update"
logopts[:item] = self.id
title = (self.title != "Untitled Story") ? self.title : self.story_draft.title
logopts[:description] = %Q{Author: #{self.user.nickname} Title: #{title} Status: #{self.displaystatus}}
LogInfo.log_event(logopts)
end
### the logger classes
class LogInfo
include DataMapper::Resource
property :id, Serial
property :log_type, String
property :log_item, String
property :description, String
property :extended, Boolean
property :user_id, Integer
property :ip_address, String
property :date, DateTime
has 1, :log_info_extended
def self.log_event(options = {})
l = self.new
l.log_type = options[:type]
l.log_item = options[:item]
l.description = options[:description]
l.extended = options[:extended]
if (l.extended)
l.log_extended_info = LogInfoExtended.new
l.log_info_extended.extended_description = options[:exte
nded_description]
end
l.user_id = $user.id
l.ip_address = request.env["REMOTE_ADDR"]
l.date = DateTime.now
l.save
end
end
class LogInfoExtended
include DataMapper::Resource
property :log_info_id, Integer, :key => true
property :extended_description, Text, :lazy => false
belongs_to :log_info
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment