lager 0.2.0.9 → 1.0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/lager.rb +19 -14
  3. data/test/lager.rb +45 -10
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0.9
1
+ 1.0.0.1
data/lib/lager.rb CHANGED
@@ -38,21 +38,18 @@ require 'logger'
38
38
  #
39
39
  module Lager
40
40
  def self.version
41
- vpath = File.join(File.dirname(__FILE__), '..', 'VERSION')
42
- File.read(vpath).chomp
41
+ file = File.expand_path('../../VERSION', __FILE__)
42
+ File.read(file).chomp
43
43
  end
44
44
 
45
- # create @lager
45
+ # create @lager from dest; return dest
46
46
  # if passed a Logger instance, set @lager to that instance and return
47
47
  # supports IO and String (filename, presumably) for log destination
48
48
  # (passed straight through to Logger.new)
49
49
  # supports symbols, strings, and integers for log level
50
50
  #
51
- def log_to(dest = $stderr, level = nil)
52
- if dest.is_a?(Logger)
53
- @lager = dest
54
- return
55
- end
51
+ def log_to(dest, level = nil)
52
+ return (@lager = dest) if dest.is_a?(Logger)
56
53
  # use the old @lager's level by default, as appropriate
57
54
  level ||= (defined?(@lager) ? @lager.level : :warn)
58
55
  @lager = Logger.new dest
@@ -62,7 +59,7 @@ module Lager
62
59
  line << msg << "\n"
63
60
  }
64
61
  log_level level
65
- nil # don't expose @lager here
62
+ dest # don't expose @lager here
66
63
  end
67
64
 
68
65
  # call without argument to get the log level
@@ -70,20 +67,28 @@ module Lager
70
67
  # :debug, 'debug', and Logger::DEBUG (0) are all supported
71
68
  #
72
69
  def log_level(level = nil)
70
+ raise "no @lager available" unless defined?(@lager)
71
+ return (self.log_level = level) if level
72
+ [:debug, :info, :warn, :error, :fatal][@lager.level] || :unknown
73
+ end
74
+
75
+ def log_level=(level)
73
76
  raise "no @lager available" unless defined?(@lager)
74
77
  case level
75
- when nil
76
- @lager.level
77
78
  when Symbol, String
78
79
  begin
79
80
  @lager.level = Logger.const_get(level.to_s.upcase)
80
81
  rescue NameError
81
- raise "unknown log level #{level}"
82
+ @lager.unknown "unknown log level: #{level}"
82
83
  end
83
84
  when Numeric
84
- @lager.level = level
85
+ if level < Logger::UNKNOWN
86
+ @lager.level = level
87
+ else
88
+ @lager.unknown "unknown log level: #{level}"
89
+ end
85
90
  else
86
- raise "unknown log level: #{level}"
91
+ @lager.unknown "unknown log level: #{level} (#{level.class})"
87
92
  end
88
93
  end
89
94
 
data/test/lager.rb CHANGED
@@ -4,7 +4,11 @@ require 'minitest/autorun'
4
4
  require 'tempfile'
5
5
 
6
6
  require_relative '../lib/lager'
7
- require_relative '../examples/foo' # useful Foo class
7
+
8
+ # useful Foo class
9
+ # calls log_to at require time
10
+ #
11
+ require_relative '../examples/foo'
8
12
 
9
13
  describe Lager do
10
14
  describe ".version" do
@@ -15,12 +19,12 @@ describe Lager do
15
19
 
16
20
  describe "#log_to" do
17
21
  it "must have created a Logger" do
18
- # note, the useful Foo class has already called log_to in the class def
22
+ # note, the useful Foo class has already called log_to
19
23
  Foo.lager.must_be_instance_of(Logger)
20
24
  end
21
25
 
22
26
  it "must return nil" do
23
- Foo.log_to.must_be_nil
27
+ Foo.log_to($stdout).must_equal $stdout
24
28
  end
25
29
 
26
30
  it "must use an existing Logger when provided" do
@@ -54,15 +58,46 @@ describe Lager do
54
58
  end
55
59
  end
56
60
 
57
- describe "#log_level" do
58
- it "must accept :debug as Logger::DEBUG" do
59
- Foo.log_level :debug
60
- Foo.log_level.must_equal Logger::DEBUG
61
+ describe "#log_level=" do
62
+ before do
63
+ Foo.log_level = :fatal
64
+ end
65
+
66
+ it "must accept :debug as :debug" do
67
+ Foo.log_level = :debug
68
+ Foo.log_level.must_equal :debug
69
+ end
70
+
71
+ it "must accept Logger::INFO as :info" do
72
+ Foo.log_level = Logger::INFO
73
+ Foo.log_level.must_equal :info
74
+ end
75
+
76
+ it "must accept 'warn' as :warn" do
77
+ Foo.log_level = 'warn'
78
+ Foo.log_level.must_equal :warn
79
+ end
80
+
81
+ def must_ignore level
82
+ lev = Foo.log_level
83
+ Foo.log_level = level
84
+ Foo.log_level.must_equal lev
85
+ end
86
+
87
+ it "must ignore a bad symbol" do
88
+ must_ignore :bedug
89
+ end
90
+
91
+ it "must ignore an unexpected object" do
92
+ must_ignore Array
93
+ end
94
+
95
+ it "must ignore a bad integer" do
96
+ must_ignore 42
61
97
  end
62
98
 
63
- it "must accept Logger::INFO as Logger::INFO" do
64
- Foo.log_level Logger::INFO
65
- Foo.log_level.must_equal Logger::INFO
99
+ it "must ignore Logger::UNKNOWN" do
100
+ must_ignore Logger::UNKNOWN
66
101
  end
67
102
  end
68
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.9
4
+ version: 1.0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-08 00:00:00.000000000 Z
12
+ date: 2013-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest