class_logger 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -55,9 +55,6 @@ The following can be used in the *path* or *in* options.
55
55
 
56
56
  <dt><strong>%{class_name}</strong></dt>
57
57
  <dd>Will replace itself with the name of the class.</dd>
58
-
59
- <dt><strong>%{caller_path_}</strong></dt>
60
- <dd>Will replace itself the path of the calling file. (Useful if you want logs relative to your scripts location)</dd>
61
58
  </dl>
62
59
 
63
60
  Example Usage
@@ -100,7 +97,7 @@ Example Usage
100
97
  # create a logger for a module
101
98
  module Something
102
99
  include ClassLogger
103
- has_logger :in => "%{caller_path}/log/my_module.log"
100
+ has_logger :in => "#{File.dirname(__FILE__)}/log/my_module.log"
104
101
  has_logger :in => "/var/log/utoh.log", :as => :utoh_logger
105
102
  end
106
103
  Something.logger.info "Testing 123" # => goes to ./log/my_module.log
data/class_logger.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "class_logger/version"
3
+ require 'class_logger'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "class_logger"
data/examples/examples.rb CHANGED
@@ -4,7 +4,7 @@ $log_path = File.dirname(__FILE__)
4
4
  # test module functionality
5
5
  module Hello
6
6
  include ClassLogger
7
- has_logger :in => "%{class_name}.log", :as => :my_logger
7
+ has_logger :in => "#{$log_path}/%{class_name}.log", :as => :my_logger
8
8
 
9
9
  def self.alternate
10
10
  loggers[:my_logger]
@@ -17,7 +17,7 @@ Hello.alternate.info "Hai der!"
17
17
  class Gateway
18
18
  include ClassLogger
19
19
  has_logger :path => $log_path, :level => Logger::ERROR
20
- has_logger :in => "%{caller_path}/transaction.log", :as => :transaction_logger,
20
+ has_logger :in => "#{$log_path}/transaction.log", :as => :transaction_logger,
21
21
  :formatter => proc{ |severity, time, program_name, message| "[%s-Transaction]: %s\n" % [severity, message] }
22
22
 
23
23
  def initialize
@@ -36,7 +36,7 @@ g.transact!
36
36
  # test default functionality
37
37
  class Default
38
38
  include ClassLogger
39
- has_logger :path => "%{caller_path}"
39
+ has_logger :path => $log_path
40
40
  end
41
41
 
42
42
  Default.new.logger.info "Testing"
data/lib/class_logger.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'logger'
2
2
 
3
3
  module ClassLogger
4
+ VERSION = '1.1'
5
+
4
6
  def self.included(base)
5
7
  base.extend DSL
6
8
  end
@@ -25,22 +27,22 @@ module ClassLogger
25
27
 
26
28
  module ClassMethods
27
29
  def loggers
28
- @@loggers ||= {}
30
+ @loggers ||= {}
29
31
  end
30
32
 
31
33
  def setup_logger(options)
32
34
  interpolations = {
33
35
  :rails_root => (defined?(Rails) ? Rails.root : ''),
34
- :class_name => self.to_s.downcase,
35
- :caller_path => (File.dirname(caller[1]) rescue '.')
36
+ :class_name => self.to_s.downcase
36
37
  }
37
-
38
+
38
39
  if i = options[:in]
39
40
  options[:file] = File.basename(i)
40
41
  options[:path] = File.dirname(i)
41
42
  end
42
43
 
43
44
  file_path = File.join(options[:path], options[:file]).to_s % interpolations
45
+
44
46
  if (rotate = options[:rotate])
45
47
  _logger = ::Logger.new(file_path, rotate)
46
48
  else
@@ -0,0 +1,80 @@
1
+ require File.expand_path('../../lib/class_logger', __FILE__)
2
+ require 'fileutils'
3
+ require 'test/unit'
4
+
5
+ $logpath = File.expand_path('../logs', __FILE__)
6
+ Dir[File.join($logpath, '*')].each{ |f| File.delete f }
7
+
8
+ def log_includes(filename, regexp)
9
+ IO.read(filename) =~ regexp
10
+ end
11
+
12
+ module TestModule
13
+ include ClassLogger
14
+ has_logger :path => $logpath
15
+ has_logger :in => File.join($logpath, 'module_named.log'), :as => :named
16
+ has_logger :path => $logpath, :file => "module_file.log", :as => :file
17
+ has_logger :in => "#{$logpath}/module_formatted.log", :as => :formatted,
18
+ :formatter => proc{ |s,t,p,m| "FORMATTED-%s: %s" % [s,m] }
19
+ end
20
+
21
+ class TestClass
22
+ include ClassLogger
23
+ has_logger :path => $logpath
24
+ has_logger :in => "#{$logpath}/class_named.log", :as => :named
25
+ has_logger :in => "#{$logpath}/level.log", :as => :level, :level => Logger::ERROR
26
+ end
27
+
28
+ class ModuleLoggerTest < Test::Unit::TestCase
29
+ def test_log_creation
30
+ assert File.exists?(File.join($logpath, TestModule.to_s.downcase+'.log')), '[Default] log wasnt created in the proper place'
31
+ assert File.exists?(File.join($logpath, 'module_named.log')), '[Named] log wasnt created in the proper place'
32
+ assert File.exists?(File.join($logpath, 'module_file.log')), '[File1] log wasnt created in the proper place'
33
+ assert File.exists?(File.join($logpath, 'module_formatted.log')), '[Formatted] log wasnt created in the proper place'
34
+ end
35
+
36
+ def test_default_logger
37
+ assert TestModule.logger.info("Hello")
38
+ assert log_includes(File.join($logpath,TestModule.to_s.downcase+'.log'), /Hello/)
39
+ end
40
+
41
+ def test_named_logger
42
+ assert TestModule.named.info("Named!")
43
+ assert log_includes(File.join($logpath,'module_named.log'), /Named/)
44
+ end
45
+
46
+ def test_formatted_logger
47
+ assert TestModule.formatted.info("Am i formatted?")
48
+ assert log_includes(File.join($logpath,'module_formatted.log'), /FORMATTED-INFO: Am i formatted\?/)
49
+ end
50
+ end
51
+
52
+ class ClassLoggerTest < Test::Unit::TestCase
53
+ def setup
54
+ @tc = TestClass.new
55
+ end
56
+
57
+ def test_log_creation
58
+ assert File.exists?(File.join($logpath, TestClass.to_s.downcase+'.log')), '[Default] log wasnt created in the proper place'
59
+ end
60
+
61
+ def test_default_logger
62
+ assert TestClass.logger.info("Hello")
63
+ assert @tc.logger.info("Instanced!")
64
+ assert log_includes(File.join($logpath,TestClass.to_s.downcase+'.log'), /Hello/)
65
+ end
66
+
67
+ def test_named_logger
68
+ assert @tc.named.info("NamedINSTANCE")
69
+ assert TestClass.named.info("NamedCLASS")
70
+ assert log_includes(File.join($logpath,'class_named.log'), /NamedINSTANCE/)
71
+ assert log_includes(File.join($logpath,'class_named.log'), /NamedCLASS/)
72
+ end
73
+
74
+ def test_level
75
+ @tc.level.info "Info"
76
+ @tc.level.error "Error"
77
+ assert !log_includes(File.join($logpath, 'level.log'), /Info/)
78
+ assert log_includes(File.join($logpath, 'level.log'), /Error/)
79
+ end
80
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: class_logger
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "1.0"
5
+ version: "1.1"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rob Hurring
@@ -31,7 +31,7 @@ files:
31
31
  - class_logger.gemspec
32
32
  - examples/examples.rb
33
33
  - lib/class_logger.rb
34
- - lib/class_logger/version.rb
34
+ - test/test_class_logger.rb
35
35
  has_rdoc: true
36
36
  homepage: ""
37
37
  licenses: []
@@ -60,5 +60,5 @@ rubygems_version: 1.5.0
60
60
  signing_key:
61
61
  specification_version: 3
62
62
  summary: Adds custom logger(s) to any ruby module or class
63
- test_files: []
64
-
63
+ test_files:
64
+ - test/test_class_logger.rb
@@ -1,3 +0,0 @@
1
- module ClassLogger
2
- VERSION = '1.0'
3
- end