yell 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011-2012 Rudolf Schmidt
1
+ Copyright (c) 2011-2013 Rudolf Schmidt
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/lib/yell.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Copyright (c) 2011-2012 Rudolf Schmidt
3
+ # Copyright (c) 2011-2013 Rudolf Schmidt
4
4
  #
5
5
  # Permission is hereby granted, free of charge, to any person obtaining
6
6
  # a copy of this software and associated documentation files (the
@@ -71,7 +71,8 @@ module Yell #:nodoc:
71
71
  Yell.new Yell::Configuration.load!(file)
72
72
  end
73
73
 
74
- def env #:nodoc:
74
+ # @private
75
+ def env
75
76
  return ENV['YELL_ENV'] if ENV.key? 'YELL_ENV'
76
77
  return ENV['RACK_ENV'] if ENV.key? 'RACK_ENV'
77
78
  return ENV['RAILS_ENV'] if ENV.key? 'RAILS_ENV'
@@ -83,7 +84,8 @@ module Yell #:nodoc:
83
84
  end
84
85
  end
85
86
 
86
- def _deprecate( version, message, options = {} ) #:nodoc:
87
+ # @private
88
+ def _deprecate( version, message, options = {} )
87
89
  messages = ["Deprecation Warning (since v#{version}): #{message}" ]
88
90
  messages << " before: #{options[:before]}" if options[:before]
89
91
  messages << " after: #{options[:after]}" if options[:after]
@@ -91,9 +93,10 @@ module Yell #:nodoc:
91
93
  _warn( *messages )
92
94
  end
93
95
 
94
- def _warn( *messages ) #:nodoc:
96
+ # @private
97
+ def _warn( *messages )
95
98
  $stderr.puts "[Yell] " + messages.join( "\n" )
96
- rescue
99
+ rescue Exception
97
100
  # do nothing
98
101
  end
99
102
  end
@@ -138,7 +138,9 @@ module Yell #:nodoc:
138
138
  super() # init the monitor superclass
139
139
 
140
140
  setup!(options)
141
- block.call(self) if block
141
+
142
+ # eval the given block
143
+ block.arity > 0 ? block.call(self) : instance_eval(&block) if block_given?
142
144
  end
143
145
 
144
146
  # The main method for calling the adapter.
@@ -163,12 +165,6 @@ module Yell #:nodoc:
163
165
  end
164
166
 
165
167
  # Get a pretty string representation of the adapter, including
166
- # the inspectable attributes.
167
- #
168
- # @example Inspect the formatter.
169
- # adapter.inspect
170
- #
171
- # @return [String] The inspection string.
172
168
  def inspect
173
169
  inspection = inspectables.inject( [] ) { |r, c| r << "#{c}: #{send(c).inspect}" }
174
170
  "#<#{self.class.name} #{inspection * ', '}>"
@@ -221,8 +217,6 @@ module Yell #:nodoc:
221
217
  end
222
218
 
223
219
  # Get an array of inspected attributes for the adapter.
224
- #
225
- # @return [ String ] An array of pretty printed field values.
226
220
  def inspectables
227
221
  [ :level ]
228
222
  end
data/lib/yell/event.rb CHANGED
@@ -5,7 +5,6 @@ require 'socket'
5
5
 
6
6
  module Yell #:nodoc:
7
7
 
8
- #
9
8
  # Yell::Event.new( :info, 'Hello World', { :scope => 'Application' } )
10
9
  # #=> Hello World scope: Application
11
10
  class Event
@@ -19,9 +18,6 @@ module Yell #:nodoc:
19
18
  @@hostname = Socket.gethostname rescue nil
20
19
  @@progname = $0
21
20
 
22
- # Accessor to the pid
23
- attr_reader :pid
24
-
25
21
  # Accessor to the log level
26
22
  attr_reader :level
27
23
 
@@ -31,24 +27,20 @@ module Yell #:nodoc:
31
27
  # Accessor to the time the log event occured
32
28
  attr_reader :time
33
29
 
34
- # Accessor to the current tread_id
35
- attr_reader :thread_id
36
-
37
30
 
38
- def initialize( level, *messages, &block )
39
- @pid = Process.pid
31
+ def initialize(logger, level, *messages, &block)
40
32
  @time = Time.now
41
33
  @level = level
42
34
 
43
35
  @messages = messages
44
36
  @messages << block.call if block
45
37
 
46
- @thread_id = Thread.current.object_id
47
-
48
- @caller = caller[CallerIndex].to_s
38
+ @caller = logger.trace.at?(level) ? caller[CallerIndex].to_s : ''
49
39
  @file = nil
50
40
  @line = nil
51
41
  @method = nil
42
+
43
+ @pid = nil
52
44
  end
53
45
 
54
46
  # Accessor to the hostname
@@ -61,29 +53,36 @@ module Yell #:nodoc:
61
53
  @@progname
62
54
  end
63
55
 
56
+ # Accessor to the PID
57
+ def pid
58
+ Process.pid
59
+ end
60
+
61
+ # Accessor to the thread's id
62
+ def thread_id
63
+ Thread.current.object_id
64
+ end
65
+
64
66
  # Accessor to filename the log event occured
65
67
  def file
66
- _caller! if @file.nil?
67
- @file
68
+ @file || (_caller!; @file)
68
69
  end
69
70
 
70
71
  # Accessor to the line the log event occured
71
72
  def line
72
- _caller! if @line.nil?
73
- @line
73
+ @line || (_caller!; @line)
74
74
  end
75
75
 
76
76
  # Accessor to the method the log event occured
77
77
  def method
78
- _caller! if @method.nil?
79
- @method
78
+ @method || (_caller!; @method)
80
79
  end
81
80
 
82
81
 
83
82
  private
84
83
 
85
84
  def _caller!
86
- if m = CallerRegexp.match( @caller )
85
+ if m = CallerRegexp.match(@caller)
87
86
  @file, @line, @method = m[1..-1]
88
87
  else
89
88
  @file, @line, @method = ['', '', '']
@@ -87,24 +87,33 @@ module Yell #:nodoc:
87
87
  @pattern = pattern || Yell::DefaultFormat
88
88
  @date_pattern = date_pattern || :iso8601
89
89
 
90
- define!
90
+ define_date_method!
91
+ define_format_method!
91
92
  end
92
93
 
93
- # Get a pretty string representation of the formatter, including
94
- # the pattern and date pattern.
95
- #
96
- # @example Inspect the formatter.
97
- # formatter.inspect
98
- #
99
- # @return [String] The inspection string.
94
+ # Get a pretty string representation of the formatter, including the pattern and date pattern.
100
95
  def inspect
101
96
  "#<#{self.class.name} pattern: #{@pattern.inspect}, date_pattern: #{@date_pattern.inspect}>"
102
97
  end
103
98
 
99
+
104
100
  private
105
101
 
106
- # defines the format method
107
- def define!
102
+ def define_date_method!
103
+ buf = case @date_pattern
104
+ when String then "t.strftime(@date_pattern)"
105
+ when Symbol then respond_to?(@date_pattern, true) ? "#{@date_pattern}(t)" : "t.#{@date_pattern}"
106
+ else "iso8601(t)"
107
+ end
108
+
109
+ instance_eval %-
110
+ def date( t )
111
+ #{buf}
112
+ end
113
+ -
114
+ end
115
+
116
+ def define_format_method!
108
117
  buff, args, _pattern = "", [], @pattern.dup
109
118
 
110
119
  while true
@@ -126,15 +135,22 @@ module Yell #:nodoc:
126
135
  -
127
136
  end
128
137
 
129
- def level( l )
130
- Yell::Severities[ l ]
138
+ # The iso8601 implementation of the standard Time library is more than
139
+ # twice as slow than using strftime. So, we just implement it ourselves.
140
+ def iso8601( t )
141
+ zone = if t.utc?
142
+ "-00:00"
143
+ else
144
+ offset = t.utc_offset
145
+ sign = offset < 0 ? '-' : '+'
146
+ sprintf('%s%02d:%02d', sign, *(offset.abs/60).divmod(60))
147
+ end
148
+
149
+ t.strftime("%Y-%m-%dT%H:%M:%S#{zone}")
131
150
  end
132
151
 
133
- def date( t )
134
- case @date_pattern
135
- when String then t.strftime( @date_pattern )
136
- else t.iso8601
137
- end
152
+ def level( l )
153
+ Yell::Severities[ l ]
138
154
  end
139
155
 
140
156
  def message( *messages )
data/lib/yell/level.rb CHANGED
@@ -138,13 +138,7 @@ module Yell #:nodoc:
138
138
  end
139
139
  alias :to_int :to_i
140
140
 
141
- # Get a pretty string representation of the level, including the
142
- # severities.
143
- #
144
- # @example Inspect the level.
145
- # level.inspect
146
- #
147
- # @return [String] The inspection string.
141
+ # Get a pretty string representation of the level, including the severities.
148
142
  def inspect
149
143
  severities = Yell::Severities.each.with_index.inject( [] ) do |r, (l, i)|
150
144
  r << l if @severities[i]
data/lib/yell/logger.rb CHANGED
@@ -14,6 +14,9 @@ module Yell #:nodoc:
14
14
  # The name of the logger instance
15
15
  attr_reader :name
16
16
 
17
+ # Stacktrace or not
18
+ attr_reader :trace
19
+
17
20
  # Initialize a new Logger
18
21
  #
19
22
  # @example A standard file logger
@@ -36,34 +39,67 @@ module Yell #:nodoc:
36
39
  # Yell::Logger.new :datefile, 'development.log' do |l|
37
40
  # l.level = :info
38
41
  # end
39
- def initialize( *args )
42
+ def initialize( *args, &block )
40
43
  @adapters = []
41
44
 
42
45
  # extract options
43
46
  @options = args.last.is_a?(Hash) ? args.pop : {}
44
47
 
45
48
  # adapters may be passed in the options
46
- _extract_adapters!( @options )
49
+ extract!(@options)
47
50
 
48
51
  # check if filename was given as argument and put it into the @options
49
52
  if [String, Pathname].include?(args.last.class)
50
53
  @options[:filename] = args.pop unless @options[:filename]
51
54
  end
52
55
 
53
- # set the log level when given
54
- self.level = @options[:level]
55
-
56
- # set the loggeer's name
57
- self.name = @options[:name] if @options[:name]
56
+ self.level = @options.fetch(:level, 0) # debug by defauly
57
+ self.name = @options.fetch(:name, nil) # no name by default
58
+ self.trace = @options.fetch(:trace, :error) # trace from :error level onwards
58
59
 
59
60
  # extract adapter
60
- self.adapter args.pop if args.any?
61
+ self.adapter(args.pop) if args.any?
61
62
 
62
63
  # eval the given block
63
- yield(self) if block_given?
64
+ block.arity > 0 ? block.call(self) : instance_eval(&block) if block_given?
64
65
 
65
66
  # default adapter when none defined
66
- self.adapter :file if @adapters.empty?
67
+ self.adapter(:file) if @adapters.empty?
68
+ end
69
+
70
+
71
+ # Set the name of a logger. When providing a name, the logger will
72
+ # automatically be added to the Yell::Repository.
73
+ #
74
+ # @return [String] The logger's name
75
+ def name=( val )
76
+ @name = val
77
+ Yell::Repository[@name] = self if @name
78
+
79
+ @name
80
+ end
81
+
82
+ # Set whether the logger should allow tracing or not. The trace option
83
+ # will tell the logger when to provider caller information.
84
+ #
85
+ # @example No tracing at all
86
+ # trace = false
87
+ #
88
+ # @example Trace every time
89
+ # race = true
90
+ #
91
+ # @example Trace from the error level onwards
92
+ # trace = :error
93
+ # trace = 'gte.error'
94
+ #
95
+ # @return [Yell::Level] a level representation of the tracer
96
+ def trace=( severity )
97
+ @trace = case severity
98
+ when true then Yell::Level.new
99
+ when false then Yell::Level.new( "gt.#{Yell::Severities.last}" )
100
+ when Yell::Level then severity
101
+ else Yell::Level.new( severity )
102
+ end
67
103
  end
68
104
 
69
105
  # Define an adapter to be used for logging.
@@ -84,9 +120,7 @@ module Yell #:nodoc:
84
120
  # adapter( Yell::Adapter::File.new )
85
121
  #
86
122
  # @param [Symbol] type The type of the adapter, may be `:file` or `:datefile` (default `:file`)
87
- #
88
123
  # @return [Yell::Adapter] The instance
89
- #
90
124
  # @raise [Yell::NoSuchAdapter] Will be thrown when the adapter is not defined
91
125
  def adapter( type = :file, *args, &block )
92
126
  options = [@options, *args].inject( Hash.new ) do |h, c|
@@ -96,15 +130,6 @@ module Yell #:nodoc:
96
130
  @adapters << Yell::Adapters.new( type, options, &block )
97
131
  end
98
132
 
99
- def name=( val )
100
- Yell::Repository[val] = self
101
- end
102
-
103
- # Convenience method for resetting all adapters of the Logger.
104
- def close
105
- @adapters.each(&:close)
106
- end
107
-
108
133
  # Creates instance methods for every log level:
109
134
  # `debug` and `debug?`
110
135
  # `info` and `info?`
@@ -115,31 +140,36 @@ module Yell #:nodoc:
115
140
  name = s.downcase
116
141
 
117
142
  class_eval <<-EOS, __FILE__, __LINE__
118
- def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
119
- #
120
- def #{name}( *m, &b ) # def info( *m, &b )
121
- return false unless #{name}? # return false unless info?
122
- write Yell::Event.new(#{index}, *m, &b) # write Yell::Event.new(1, *m, &b)
123
- #
124
- true # true
125
- end # end
143
+ def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
144
+ #
145
+ def #{name}( *m, &b ) # def info( *m, &b )
146
+ return false unless #{name}? # return false unless info?
147
+ write Yell::Event.new(self, #{index}, *m, &b) # write Yell::Event.new(self, 1, *m, &b)
148
+ #
149
+ true # true
150
+ end # end
126
151
  EOS
127
152
  end
128
153
 
129
154
  # Get a pretty string representation of the logger.
130
- #
131
- # @example Inspect the logger
132
- # logger.inspect
133
- #
134
- # @return [String] The inspection string.
135
155
  def inspect
136
156
  inspection = inspectables.inject( [] ) { |r, c| r << "#{c}: #{send(c).inspect}" }
137
157
  "#<#{self.class.name} #{inspection * ', '}, adapters: #{@adapters.map(&:inspect) * ', '}>"
138
158
  end
139
159
 
160
+ # @private
161
+ def close
162
+ @adapters.each(&:close)
163
+ end
164
+
165
+ # @private
166
+ def adapters
167
+ @adapters
168
+ end
169
+
140
170
  private
141
171
 
142
- # The :adapters key may be passed to the options hash. It may appear in
172
+ # The :adapters key may be passed to the options hash. It may appear in
143
173
  # multiple variations:
144
174
  #
145
175
  # @example
@@ -147,11 +177,11 @@ module Yell #:nodoc:
147
177
  #
148
178
  # @example
149
179
  # options = { :adapters => [:stdout => {:level => :info}, :stderr => {:level => :error}]
150
- def _extract_adapters!( opts )
151
- ( opts.delete( :adapters ) || [] ).each do |a|
180
+ def extract!( opts )
181
+ ( opts.delete(:adapters) || [] ).each do |a|
152
182
  case a
153
- when String, Symbol then adapter( a )
154
- else a.each { |n, o| adapter( n, o || {} ) }
183
+ when String, Symbol then adapter( a )
184
+ else a.each { |n, o| adapter( n, o || {} ) }
155
185
  end
156
186
  end
157
187
  end
@@ -162,10 +192,8 @@ module Yell #:nodoc:
162
192
  end
163
193
 
164
194
  # Get an array of inspected attributes for the adapter.
165
- #
166
- # @return [ String ] An array of pretty printed field values.
167
195
  def inspectables
168
- [ :name, :level ]
196
+ [ :name, :level, :trace ]
169
197
  end
170
198
 
171
199
  end
data/lib/yell/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Yell #:nodoc:
4
- VERSION = "1.2.3"
4
+ VERSION = "1.3.0"
5
5
 
6
6
  end
7
7
 
@@ -10,7 +10,7 @@ describe Yell::Adapters::Base do
10
10
  end
11
11
 
12
12
  it "should set the level" do
13
- Yell::Adapters::Base.new :level => :info
13
+ Yell::Adapters::Base.new(:level => :info)
14
14
  end
15
15
  end
16
16
 
@@ -23,23 +23,24 @@ describe Yell::Adapters::Base do
23
23
  end
24
24
 
25
25
  it "should set the level" do
26
- Yell::Adapters::Base.new :level => :info
26
+ Yell::Adapters::Base.new(:level => :info)
27
27
  end
28
28
  end
29
29
  end
30
30
 
31
31
  context :options do
32
- let( :options ) { { :my => :options } }
33
- let( :adapter ) { Yell::Adapters::Base.new( options ) }
32
+ let(:options) { {:my => :options} }
33
+ let(:adapter) { Yell::Adapters::Base.new(options) }
34
34
 
35
35
  it { options.should == options }
36
36
  end
37
37
 
38
38
  context :write do
39
- subject { Yell::Adapters::Base.new :level => :info }
39
+ let(:logger) { Yell::Logger.new }
40
+ subject { Yell::Adapters::Base.new(:level => 1) }
40
41
 
41
42
  it "should delegate :event to :write!" do
42
- event = Yell::Event.new( 1, "Hello World!" )
43
+ event = Yell::Event.new( logger, 1, "Hello World!" )
43
44
 
44
45
  mock( subject ).write!( event )
45
46
 
@@ -47,7 +48,7 @@ describe Yell::Adapters::Base do
47
48
  end
48
49
 
49
50
  it "should not write when event does not have the right level" do
50
- event = Yell::Event.new( 0, "Hello World!" )
51
+ event = Yell::Event.new( logger, 0, "Hello World!" )
51
52
 
52
53
  dont_allow( subject ).write!( event )
53
54
 
@@ -1,15 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Yell::Adapters::Datefile do
4
- let( :filename ) { fixture_path + '/test.log' }
5
- let( :event ) { Yell::Event.new(1, "Hello World") }
4
+ let(:logger) { Yell::Logger.new }
5
+ let(:filename) { fixture_path + '/test.log' }
6
+ let(:event) { Yell::Event.new(logger, 1, "Hello World") }
6
7
 
7
8
  before do
8
9
  Timecop.freeze( Time.now )
9
10
  end
10
11
 
12
+ it { should be_kind_of Yell::Adapters::File }
13
+
11
14
  describe :filename do
12
- let( :adapter ) { Yell::Adapters::Datefile.new(:filename => filename, :symlink => false) }
15
+ let(:adapter) { Yell::Adapters::Datefile.new(:filename => filename, :symlink => false) }
13
16
 
14
17
  it "should be replaced with date_pattern" do
15
18
  adapter.write( event )
@@ -21,12 +24,12 @@ describe Yell::Adapters::Datefile do
21
24
  mock( File ).open( datefile_filename, anything ) { File.new('/dev/null', 'w') }
22
25
 
23
26
  adapter.write( event )
24
- Timecop.freeze( Time.now + 10 ) { adapter.write( event ) }
27
+ Timecop.freeze( Time.now + 10 ) { adapter.write(event) }
25
28
  end
26
29
 
27
30
  context "rollover" do
28
- let( :tomorrow ) { Time.now + 86400 }
29
- let( :tomorrow_datefile_filename ) { fixture_path + "/test.#{tomorrow.strftime(Yell::Adapters::Datefile::DefaultDatePattern)}.log" }
31
+ let(:tomorrow) { Time.now + 86400 }
32
+ let(:tomorrow_datefile_filename) { fixture_path + "/test.#{tomorrow.strftime(Yell::Adapters::Datefile::DefaultDatePattern)}.log" }
30
33
 
31
34
  it "should rotate when date has passed" do
32
35
  mock( File ).open( datefile_filename, anything ) { File.new('/dev/null', 'w') }
@@ -41,7 +44,7 @@ describe Yell::Adapters::Datefile do
41
44
  end
42
45
 
43
46
  describe :keep do
44
- let( :adapter ) { Yell::Adapters::Datefile.new(:keep => 2, :filename => filename, :symlink => false, :date_pattern => "%M") }
47
+ let(:adapter) { Yell::Adapters::Datefile.new(:keep => 2, :filename => filename, :symlink => false, :date_pattern => "%M") }
45
48
 
46
49
  it "should keep the specified number or files upon rollover" do
47
50
  adapter.write( event )
@@ -60,17 +63,17 @@ describe Yell::Adapters::Datefile do
60
63
  end
61
64
 
62
65
  describe :symlink do
63
- let( :time ) { Time.now }
66
+ let(:time) { Time.now }
64
67
  before { Timecop.freeze(time) }
65
68
 
66
69
  context "default (true)" do
67
- let( :adapter ) { Yell::Adapters::Datefile.new(:filename => filename, :date_pattern => "%M") }
70
+ let(:adapter) { Yell::Adapters::Datefile.new(:filename => filename, :date_pattern => "%M") }
68
71
 
69
72
  it "should create the sylink the original filename" do
70
73
  adapter.write( event )
71
74
 
72
75
  File.symlink?( filename ).should be_true
73
- File.readlink( filename ).should == datefile_filename( adapter.date_pattern )
76
+ File.readlink( filename ).should == datefile_filename(adapter.date_pattern)
74
77
  end
75
78
 
76
79
  it "should symlink upon rollover" do
@@ -80,13 +83,13 @@ describe Yell::Adapters::Datefile do
80
83
  adapter.write( event )
81
84
 
82
85
  File.symlink?( filename ).should be_true
83
- File.readlink( filename ).should == datefile_filename( adapter.date_pattern )
86
+ File.readlink( filename ).should == datefile_filename(adapter.date_pattern)
84
87
  end
85
88
  end
86
89
  end
87
90
 
88
91
  context "when set to false" do
89
- let( :adapter ) { Yell::Adapters::Datefile.new(:symlink => false, :filename => filename, :date_pattern => "%M") }
92
+ let(:adapter) { Yell::Adapters::Datefile.new(:symlink => false, :filename => filename, :date_pattern => "%M") }
90
93
 
91
94
  it "should not create the sylink the original filename" do
92
95
  adapter.write( event )
@@ -97,8 +100,8 @@ describe Yell::Adapters::Datefile do
97
100
  end
98
101
 
99
102
  describe :header do
100
- let( :adapter ) { Yell::Adapters::Datefile.new(:filename => filename) }
101
- let( :header ) { File.open(datefile_filename, &:readline) }
103
+ let(:adapter) { Yell::Adapters::Datefile.new(:filename => filename) }
104
+ let(:header) { File.open(datefile_filename, &:readline) }
102
105
 
103
106
  before do
104
107
  adapter.format = "%m" # easier to parse
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Yell::Adapters::File do
4
- let( :devnull ) { File.new('/dev/null', 'w') }
4
+ let(:devnull) { File.new('/dev/null', 'w') }
5
5
 
6
6
  before do
7
7
  stub( File ).open( anything, anything ) { devnull }
@@ -16,11 +16,12 @@ describe Yell::Adapters::File do
16
16
  end
17
17
 
18
18
  context :write do
19
- let( :event ) { Yell::Event.new(1, "Hello World") }
19
+ let(:logger) { Yell::Logger.new }
20
+ let(:event) { Yell::Event.new(logger, 1, "Hello World") }
20
21
 
21
22
  context "default filename" do
22
- let( :filename ) { File.expand_path "#{Yell.env}.log" }
23
- let( :adapter ) { Yell::Adapters::File.new }
23
+ let(:filename) { File.expand_path "#{Yell.env}.log" }
24
+ let(:adapter) { Yell::Adapters::File.new }
24
25
 
25
26
  it "should print to file" do
26
27
  mock( File ).open( filename, File::WRONLY|File::APPEND|File::CREAT ) { devnull }
@@ -30,8 +31,8 @@ describe Yell::Adapters::File do
30
31
  end
31
32
 
32
33
  context "with given :filename" do
33
- let( :filename ) { fixture_path + '/filename.log' }
34
- let( :adapter ) { Yell::Adapters::File.new( :filename => filename ) }
34
+ let(:filename) { fixture_path + '/filename.log' }
35
+ let(:adapter) { Yell::Adapters::File.new( :filename => filename ) }
35
36
 
36
37
  it "should print to file" do
37
38
  mock( File ).open( filename, File::WRONLY|File::APPEND|File::CREAT ) { devnull }
@@ -41,8 +42,8 @@ describe Yell::Adapters::File do
41
42
  end
42
43
 
43
44
  context "with given :pathname" do
44
- let( :pathname ) { Pathname.new(fixture_path).join('filename.log') }
45
- let( :adapter ) { Yell::Adapters::File.new( :filename => pathname ) }
45
+ let(:pathname) { Pathname.new(fixture_path).join('filename.log') }
46
+ let(:adapter) { Yell::Adapters::File.new( :filename => pathname ) }
46
47
 
47
48
  it "should accept pathanme as filename" do
48
49
  mock( File ).open( pathname.to_s, File::WRONLY|File::APPEND|File::CREAT ) { devnull }
@@ -52,7 +53,7 @@ describe Yell::Adapters::File do
52
53
  end
53
54
 
54
55
  context :sync do
55
- let( :adapter ) { Yell::Adapters::File.new }
56
+ let(:adapter) { Yell::Adapters::File.new }
56
57
 
57
58
  it "should sync by default" do
58
59
  mock( devnull ).sync=( true )
@@ -8,14 +8,14 @@ describe Yell::Adapters::Io do
8
8
  it "should set default :format" do
9
9
  adapter = Yell::Adapters::Io.new
10
10
 
11
- adapter.format.should be_kind_of Yell::Formatter
11
+ adapter.format.should be_kind_of(Yell::Formatter)
12
12
  end
13
13
 
14
14
  context :level do
15
- let( :level ) { Yell::Level.new(:warn) }
15
+ let(:level) { Yell::Level.new(:warn) }
16
16
 
17
17
  it "should set the level" do
18
- adapter = Yell::Adapters::Io.new :level => level
18
+ adapter = Yell::Adapters::Io.new(:level => level)
19
19
  adapter.level.should == level
20
20
  end
21
21
 
@@ -26,10 +26,10 @@ describe Yell::Adapters::Io do
26
26
  end
27
27
 
28
28
  context :format do
29
- let( :format ) { Yell::Formatter.new }
29
+ let(:format) { Yell::Formatter.new }
30
30
 
31
31
  it "should set the level" do
32
- adapter = Yell::Adapters::Io.new :format => format
32
+ adapter = Yell::Adapters::Io.new(:format => format)
33
33
  adapter.format.should == format
34
34
  end
35
35
 
@@ -41,9 +41,10 @@ describe Yell::Adapters::Io do
41
41
  end
42
42
 
43
43
  context :write do
44
- let( :event ) { Yell::Event.new(1, "Hello World") }
45
- let( :adapter ) { Yell::Adapters::Io.new }
46
- let( :stream ) { File.new('/dev/null', 'w') }
44
+ let(:logger) { Yell::Logger.new }
45
+ let(:event) { Yell::Event.new(logger, 1, "Hello World") }
46
+ let(:adapter) { Yell::Adapters::Io.new }
47
+ let(:stream) { File.new('/dev/null', 'w') }
47
48
 
48
49
  before do
49
50
  stub( adapter ).stream { stream }
@@ -4,7 +4,7 @@ describe Yell::Adapters do
4
4
 
5
5
  context :new do
6
6
  context "given a Yell::Adapters::Base ancestor" do
7
- let( :stdout ) { Yell::Adapters::Stdout.new }
7
+ let(:stdout) { Yell::Adapters::Stdout.new }
8
8
 
9
9
  it "should return the instance" do
10
10
  Yell::Adapters.new( stdout ).should == stdout
@@ -35,8 +35,8 @@ describe Yell::Adapters do
35
35
  end
36
36
 
37
37
  context :register do
38
- let( :name ) { :test }
39
- let( :klass ) { mock }
38
+ let(:name) { :test }
39
+ let(:klass) { mock }
40
40
 
41
41
  before { Yell::Adapters.register( name, klass ) }
42
42
 
@@ -3,14 +3,14 @@ require 'spec_helper'
3
3
  describe Yell::Configuration do
4
4
 
5
5
  describe ":load!" do
6
- let( :file ) { fixture_path + '/yell.yml' }
7
- let( :config ) { Yell::Configuration.load!( file ) }
6
+ let(:file) { fixture_path + '/yell.yml' }
7
+ let(:config) { Yell::Configuration.load!(file) }
8
8
 
9
9
  subject { config }
10
10
 
11
- it { should be_kind_of Hash }
12
- it { should have_key :level }
13
- it { should have_key :adapters }
11
+ it { should be_kind_of(Hash) }
12
+ it { should have_key(:level) }
13
+ it { should have_key(:adapters) }
14
14
 
15
15
  context :level do
16
16
  subject { config[:level] }
@@ -21,13 +21,13 @@ describe Yell::Configuration do
21
21
  context :adapters do
22
22
  subject { config[:adapters] }
23
23
 
24
- it { should be_kind_of Array }
24
+ it { should be_kind_of(Array) }
25
25
 
26
26
  # stdout
27
27
  it { subject.first.should == :stdout }
28
28
 
29
29
  # stderr
30
- it { subject.last.should be_kind_of Hash }
30
+ it { subject.last.should be_kind_of(Hash) }
31
31
  it { subject.last.should == { :stderr => {:level => 'gte.error'} } }
32
32
  end
33
33
  end
@@ -4,39 +4,21 @@ require 'spec_helper'
4
4
  # the logger methods, we need to divert here in order to get
5
5
  # the correct caller.
6
6
  class EventFactory
7
- def self.event( level, message )
8
- self._event( level, message )
7
+ def self.event(logger, level, message)
8
+ self._event(logger, level, message)
9
9
  end
10
10
 
11
11
  private
12
12
 
13
- def self._event( level, message )
14
- Yell::Event.new level, message
13
+ def self._event(logger, level, message)
14
+ Yell::Event.new(logger, level, message)
15
15
  end
16
16
 
17
17
  end
18
18
 
19
19
  describe Yell::Event do
20
- let(:event) { Yell::Event.new 1, 'Hello World!' }
21
-
22
- context :caller do
23
- let( :event ) { EventFactory.event 1, "Hello World" }
24
-
25
- context :file do
26
- subject { event.file }
27
- it { should == __FILE__ }
28
- end
29
-
30
- context :line do
31
- subject { event.line }
32
- it { should == "8" }
33
- end
34
-
35
- context :method do
36
- subject { event.method }
37
- it { should == 'event' }
38
- end
39
- end
20
+ let(:logger) { Yell::Logger.new(:trace => true) }
21
+ let(:event) { Yell::Event.new(logger, 1, 'Hello World!') }
40
22
 
41
23
  context :level do
42
24
  subject { event.level }
@@ -77,7 +59,7 @@ describe Yell::Event do
77
59
  read, write = IO.pipe
78
60
 
79
61
  @pid = Process.fork do
80
- event = Yell::Event.new 1, 'Hello World!'
62
+ event = Yell::Event.new(logger, 1, 'Hello World!')
81
63
  write.puts event.pid
82
64
  end
83
65
  Process.wait
@@ -96,4 +78,22 @@ describe Yell::Event do
96
78
  it { should == $0 }
97
79
  end
98
80
 
81
+ context :caller do
82
+ subject { EventFactory.event(logger, 1, "Hello World") }
83
+
84
+ context "with trace" do
85
+ its(:file) { should == __FILE__ }
86
+ its(:line) { should == "8" }
87
+ its(:method) { should == "event" }
88
+ end
89
+
90
+ context "without trace" do
91
+ before { logger.trace = false }
92
+
93
+ its(:file) { should == "" }
94
+ its(:line) { should == "" }
95
+ its(:method) { should == "" }
96
+ end
97
+ end
98
+
99
99
  end
@@ -2,11 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe Yell::Formatter do
4
4
 
5
- let( :formatter ) { Yell::Formatter.new(subject) }
6
- let( :event ) { Yell::Event.new 1, 'Hello World!' }
7
- let( :time ) { Time.now }
5
+ let(:logger) { Yell::Logger.new }
6
+ let(:formatter) { Yell::Formatter.new(subject) }
7
+ let(:event) { Yell::Event.new(logger, 1, 'Hello World!') }
8
+ let(:time) { Time.now }
8
9
 
9
- let( :format ) { formatter.format(event) }
10
+ let(:format) { formatter.format(event) }
10
11
 
11
12
  before do
12
13
  Timecop.freeze( time )
@@ -52,7 +53,7 @@ describe Yell::Formatter do
52
53
  end
53
54
 
54
55
  context "caller" do
55
- let( :_caller ) { [nil, nil, "/path/to/file.rb:123:in `test_method'"] }
56
+ let(:_caller) { [nil, nil, "/path/to/file.rb:123:in `test_method'"] }
56
57
 
57
58
  before do
58
59
  any_instance_of( Yell::Event ) do |e|
@@ -104,8 +105,8 @@ describe Yell::Formatter do
104
105
  end
105
106
 
106
107
  context "Exceptions" do
107
- let( :exception ) { StandardError.new( "This is an Exception" ) }
108
- let( :event ) { Yell::Event.new( 1, exception ) }
108
+ let(:exception) { StandardError.new( "This is an Exception" ) }
109
+ let(:event) { Yell::Event.new(logger, 1, exception) }
109
110
 
110
111
  subject { "%m" }
111
112
 
@@ -117,8 +118,8 @@ describe Yell::Formatter do
117
118
  end
118
119
 
119
120
  context "Hashes" do
120
- let( :hash ) { { :test => 'message' } }
121
- let( :event ) { Yell::Event.new( 1, hash ) }
121
+ let(:hash) { { :test => 'message' } }
122
+ let(:event) { Yell::Event.new(logger, 1, hash) }
122
123
 
123
124
  subject { "%m" }
124
125
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Yell::Level do
4
4
 
5
5
  context "default" do
6
- let( :level ) { Yell::Level.new }
6
+ let(:level) { Yell::Level.new }
7
7
 
8
8
  it { level.at?(:debug).should be_true }
9
9
  it { level.at?(:info).should be_true }
@@ -13,7 +13,7 @@ describe Yell::Level do
13
13
  end
14
14
 
15
15
  context "given a Symbol" do
16
- let( :level ) { Yell::Level.new(subject) }
16
+ let(:level) { Yell::Level.new(subject) }
17
17
 
18
18
  context :debug do
19
19
  subject { :debug }
@@ -67,7 +67,7 @@ describe Yell::Level do
67
67
  end
68
68
 
69
69
  context "given a String" do
70
- let( :level ) { Yell::Level.new(subject) }
70
+ let(:level) { Yell::Level.new(subject) }
71
71
 
72
72
  context "basic string" do
73
73
  subject { 'error' }
@@ -131,7 +131,7 @@ describe Yell::Level do
131
131
  end
132
132
 
133
133
  context "given an Array" do
134
- let( :level ) { Yell::Level.new( [:debug, :warn, :fatal] ) }
134
+ let(:level) { Yell::Level.new( [:debug, :warn, :fatal] ) }
135
135
 
136
136
  it { level.at?(:debug).should be_true }
137
137
  it { level.at?(:info).should be_false }
@@ -141,7 +141,7 @@ describe Yell::Level do
141
141
  end
142
142
 
143
143
  context "given a Range" do
144
- let( :level ) { Yell::Level.new( (1..3) ) }
144
+ let(:level) { Yell::Level.new( (1..3) ) }
145
145
 
146
146
  it { level.at?(:debug).should be_false }
147
147
  it { level.at?(:info).should be_true }
@@ -151,7 +151,7 @@ describe Yell::Level do
151
151
  end
152
152
 
153
153
  context "given a Yell::Level instance" do
154
- let( :level ) { Yell::Level.new( :warn ) }
154
+ let(:level) { Yell::Level.new(:warn) }
155
155
 
156
156
  it { level.at?(:debug).should be_false }
157
157
  it { level.at?(:info).should be_false }
@@ -161,7 +161,7 @@ describe Yell::Level do
161
161
  end
162
162
 
163
163
  context "backwards compatibility" do
164
- let( :level ) { Yell::Level.new :warn }
164
+ let(:level) { Yell::Level.new :warn }
165
165
 
166
166
  it { level.to_i.should == 2 }
167
167
  it { Integer(level).should == 2 }
@@ -13,10 +13,12 @@ class LoggerFactory
13
13
  end
14
14
 
15
15
  describe Yell::Logger do
16
- let( :filename ) { fixture_path + '/logger.log' }
16
+ let(:filename) { fixture_path + '/logger.log' }
17
17
 
18
18
  context "a Logger instance" do
19
- let( :logger ) { Yell::Logger.new }
19
+ let(:logger) { Yell::Logger.new }
20
+
21
+ its(:name) { should be_nil }
20
22
 
21
23
  context "log methods" do
22
24
  subject { logger }
@@ -41,23 +43,43 @@ describe Yell::Logger do
41
43
  end
42
44
 
43
45
  context "default adapter" do
44
- let( :adapters ) { logger.instance_variable_get :@adapters }
46
+ let(:adapters) { logger.instance_variable_get(:@adapters) }
45
47
 
46
48
  it { adapters.size.should == 1 }
47
- it { adapters.first.should be_kind_of Yell::Adapters::File }
49
+ it { adapters.first.should be_kind_of(Yell::Adapters::File) }
50
+ end
51
+
52
+ context "default :level" do
53
+ subject { logger.level }
54
+
55
+ it { should be_instance_of(Yell::Level) }
56
+ its(:severities) { should == [true, true, true, true, true, true] }
57
+ end
58
+
59
+ context "default :trace" do
60
+ subject { logger.trace }
61
+
62
+ it { should be_instance_of(Yell::Level) }
63
+ its(:severities) { should == [false, false, false, true, true, true] } # from error onwards
48
64
  end
49
65
  end
50
66
 
51
67
  context "initialize with a :name" do
52
- let( :name ) { 'test' }
68
+ let(:name) { 'test' }
69
+ let!(:logger) { Yell.new(:name => name) }
53
70
 
54
- it "should be added to the repository" do
55
- logger = Yell.new :name => name
71
+ it "should set the logger's name" do
72
+ logger.name.should == name
73
+ end
56
74
 
75
+ it "should be added to the repository" do
57
76
  Yell::Repository[name].should == logger
58
77
  end
59
78
  end
60
79
 
80
+ context "initialize with :trace" do
81
+ end
82
+
61
83
  context "initialize with a :filename" do
62
84
  it "should call adapter with :file" do
63
85
  mock.proxy( Yell::Adapters::File ).new( :filename => 'test.log' )
@@ -103,36 +125,39 @@ describe Yell::Logger do
103
125
  end
104
126
 
105
127
  context "initialize with a block" do
106
- let( :level ) { Yell::Level.new :error }
107
- let( :adapter ) { Yell::Adapters::Stdout.new }
128
+ let(:level) { Yell::Level.new :error }
129
+ let(:stdout) { Yell::Adapters::Stdout.new }
130
+ let(:adapters) { loggr.instance_variable_get(:@adapters) }
108
131
 
109
- let( :logger ) do
110
- Yell::Logger.new do |l|
111
- l.level = level
112
- l.adapter adapter
132
+ context "with arity" do
133
+ subject do
134
+ Yell::Logger.new(:level => level) { |l| l.adapter(:stdout) }
113
135
  end
114
- end
115
136
 
116
- it "should set the level" do
117
- logger.level.should == level
137
+ its(:level) { should == level }
138
+ its('adapters.size') { should == 1 }
139
+ its('adapters.first') { should be_instance_of(Yell::Adapters::Stdout) }
118
140
  end
119
141
 
120
- it "should define adapter" do
121
- adapters = logger.instance_variable_get :@adapters
142
+ context "without arity" do
143
+ subject do
144
+ Yell::Logger.new(:level => level) { adapter(:stdout) }
145
+ end
122
146
 
123
- adapters.size.should == 1
124
- adapters.first.should == adapter
147
+ its(:level) { should == level }
148
+ its('adapters.size') { should == 1 }
149
+ its('adapters.first') { should be_instance_of(Yell::Adapters::Stdout) }
125
150
  end
126
151
  end
127
152
 
128
153
  context "initialize with :adapters option" do
129
- let( :logger ) do
154
+ let(:logger) do
130
155
  Yell::Logger.new :adapters => [ :stdout, { :stderr => {:level => :error} } ]
131
156
  end
132
157
 
133
- let( :adapters ) { logger.instance_variable_get :@adapters }
134
- let( :stdout ) { adapters.first }
135
- let( :stderr ) { adapters.last }
158
+ let(:adapters) { logger.instance_variable_get :@adapters }
159
+ let(:stdout) { adapters.first }
160
+ let(:stderr) { adapters.last }
136
161
 
137
162
  it "should define those adapters" do
138
163
  adapters.size.should == 2
@@ -149,24 +174,24 @@ describe Yell::Logger do
149
174
  end
150
175
 
151
176
  context "caller's :file, :line and :method" do
152
- let( :adapter ) { Yell::Adapters::Stdout.new :format => "%F, %n: %M" }
153
- let( :logger ) { Yell::Logger.new { |l| l.adapter adapter } }
177
+ let(:stdout) { Yell::Adapters::Stdout.new(:format => "%F, %n: %M") }
178
+ let(:logger) { Yell::Logger.new(:trace => true) { |l| l.adapter(stdout) } }
154
179
 
155
180
  it "should write correctly" do
156
181
  factory = LoggerFactory.new
157
182
  factory.logger = logger
158
183
 
159
- mock( adapter.send(:stream) ).syswrite( "#{__FILE__}, 7: foo\n" )
160
- mock( adapter.send(:stream) ).syswrite( "#{__FILE__}, 11: bar\n" )
184
+ mock( stdout.send(:stream) ).syswrite( "#{__FILE__}, 7: foo\n" )
185
+ mock( stdout.send(:stream) ).syswrite( "#{__FILE__}, 11: bar\n" )
161
186
 
162
187
  factory.foo
163
188
  factory.bar
164
189
  end
165
190
  end
166
191
 
167
- context "logging" do
168
- let( :logger ) { Yell::Logger.new(filename, :format => "%m") }
169
- let( :line ) { File.open(filename, &:readline) }
192
+ context "logging in general" do
193
+ let(:logger) { Yell::Logger.new(filename, :format => "%m") }
194
+ let(:line) { File.open(filename, &:readline) }
170
195
 
171
196
  it "should output a single message" do
172
197
  logger.info "Hello World"
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Yell::Repository do
4
- let( :name ) { 'test' }
5
- let( :logger ) { Yell.new :stdout }
4
+ let(:name) { 'test' }
5
+ let(:logger) { Yell.new :stdout }
6
6
 
7
7
  subject { Yell::Repository[name] }
8
8
 
@@ -56,8 +56,8 @@ describe Yell::Repository do
56
56
  end
57
57
 
58
58
  context "[]= with a named logger of other name" do
59
- let( :other ) { 'other' }
60
- let( :logger ) { Yell.new :stdout, :name => other }
59
+ let(:other) { 'other' }
60
+ let(:logger) { Yell.new :stdout, :name => other }
61
61
 
62
62
  before do
63
63
  Yell::Repository[ name ] = logger
@@ -70,7 +70,7 @@ describe Yell::Repository do
70
70
  end
71
71
 
72
72
  context "loggers" do
73
- let( :loggers ) { { name => logger } }
73
+ let(:loggers) { { name => logger } }
74
74
 
75
75
  subject { Yell::Repository.loggers }
76
76
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yell
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
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-01-16 00:00:00.000000000 Z
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Yell - Your Extensible Logging Library. Define multiple adapters, various
15
15
  log level combinations or message formatting options like you've never done before
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project: yell
92
- rubygems_version: 1.8.24
92
+ rubygems_version: 1.8.25
93
93
  signing_key:
94
94
  specification_version: 3
95
95
  summary: Yell - Your Extensible Logging Library