Skip to content

Instantly share code, notes, and snippets.

@ktkaushik
Created April 22, 2012 05:02
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 ktkaushik/2454426 to your computer and use it in GitHub Desktop.
Save ktkaushik/2454426 to your computer and use it in GitHub Desktop.
Custom Logger to store logs based on class name or table name. A logger for each file. Create an initializer as cutom_logger.rb.
# This should be an initializer.
class CustomLogger
TABFILE = File.join("#{Rails.root}/log/tab_#{Rails.env}_errors.log")
SEARCHFILE = File.join("#{Rails.root}/log/searches_#{Rails.env}_errors.log")
VIDEOADFILE = File.join("#{Rails.root}/log/video_ads_#{Rails.env}_errors.log")
BANNERADFILE = File.join("#{Rails.root}/log/banner_ads_#{Rails.env}_errors.log")
CATEGORYFILE = File.join("#{Rails.root}/log/categories_#{Rails.env}_errors.log")
CONTENTPATHFILE = File.join("#{Rails.root}/log/content_paths_#{Rails.env}_errors.log")
CATEGORYCLICKFILE = File.join("#{Rails.root}/log/category_clicks_#{Rails.env}_errors.log")
def self.log(message, severity = :info , class_name)
if class_name == "Tab"
@tab_logger ||= ActiveSupport::BufferedLogger.new( TABFILE )
elsif class_name == "Search"
@search_logger ||= ActiveSupport::BufferedLogger.new( SEARCHFILE )
elsif class_name == "VideoAd"
@video_ad_logger ||= ActiveSupport::BufferedLogger.new( VIDEOADFILE )
elsif class_name == "BannerAd"
@banner_ad_logger ||= ActiveSupport::BufferedLogger.new( BANNERADFILE )
elsif class_name == "ContentPath"
@content_path_logger ||= ActiveSupport::BufferedLogger.new( CONTENTPATHFILE )
elsif class_name == "CategoryClick"
@category_click_logger ||= ActiveSupport::BufferedLogger.new( CATEGORYCLICKFILE )
end
@model_log = @tab_logger || @search_logger || @video_ad_logger || @category_click_logger || @banner_ad_logger || @content_path_logger
@model_log.send severity, "[#{Time.now.to_s(:db)}] [#{severity.to_s.capitalize}] #{message}\n"
end
end
# call this method anywhere in your app like this :
CustomLogger.log "This message should go in the tab_development_errors.log" , :error , "Tab"
# a more better way is to do this :
CustomLogger.log "This message should go in the tab_development_errors.log" , :error , tab_record.class.name.to_s (i know the .to_s is not necessary)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment