Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

init

  • Loading branch information...
commit 53fcb2febed92d97bd78ff271fdeac161935a016 0 parents
@robhurring authored
3  .gitignore
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in class_logger.gemspec
+gemspec
2  Rakefile
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
21 class_logger.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "class_logger/version"
+
+Gem::Specification.new do |s|
+ s.name = "class_logger"
+ s.version = ClassLogger::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Rob Hurring"]
+ s.email = ["rob@ubrio.us"]
+ s.homepage = ""
+ s.summary = %q{Adds custom logger(s) to any ruby module or class}
+ s.description = %q{Allows you to create multiple loggers for any given class}
+
+ s.rubyforge_project = "class_logger"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
44 lib/class_logger.rb
@@ -0,0 +1,44 @@
+module ClassLogger
+ def self.included(base)
+ base.extend DSL
+ end
+
+ module DSL
+ def has_logger(options = {})
+ send :extend, ClassMethods
+ default_options = {
+ :rotate => nil,
+ :max_size => nil,
+ :keep => nil,
+ :path => "%<rails_root>s/log",
+ :in => "%<class_name>s.log",
+ :as => :logger,
+ :formatter => proc{ |severity, time, program_name, message| "[%s,%s]: %s\n" % [severity, time, message] },
+ :level => ::Logger::DEBUG
+ }
+ self.setup_logger(default_options.merge(options))
+ end
+ end
+
+ module ClassMethods
+ def loggers
+ @@loggers ||= {}
+ end
+
+ def setup_logger(options)
+ file_path = File.join(options[:path], options[:in]).to_s % \
+ {:rails_root => (defined?(Rails) ? Rails.root : ''), :class_name => self.to_s.downcase}
+ if (rotate = options[:rotate])
+ _logger = ::Logger.new(file_path, rotate)
+ else
+ _logger = ::Logger.new(file_path, options[:keep], options[:max_size])
+ end
+ _logger.formatter = options[:formatter]
+ _logger.level = options[:level]
+
+ as = options[:as]
+ self.loggers[as] = _logger
+ define_method(as){ self.class.loggers[as] }
+ end
+ end
+end
3  lib/class_logger/version.rb
@@ -0,0 +1,3 @@
+module ClassLogger
+ VERSION = "0.0.1"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.