yell 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  Yell works and is tested with ruby 1.8.7, 1.9.x, jruby 1.8 and 1.9 mode, rubinius 1.8 and 1.9 as well as ree.
6
6
 
7
+ **If you want to use Yell with Rails, then head over to [yell-rails](https://github.com/rudionrails/yell-rails).**
7
8
 
8
9
  ## Installation
9
10
 
data/Rakefile CHANGED
@@ -30,7 +30,7 @@ begin
30
30
  desc "Run specs"
31
31
  RSpec::Core::RakeTask.new do |t|
32
32
  t.rspec_opts = %w(--color --format progress --order random)
33
- t.ruby_opts = %w(-w)
33
+ # t.ruby_opts = %w(-w)
34
34
  end
35
35
  rescue LoadError
36
36
  task :spec do
data/lib/yell/event.rb CHANGED
@@ -5,14 +5,18 @@ require 'socket'
5
5
 
6
6
  module Yell #:nodoc:
7
7
 
8
+ #
9
+ # Yell::Event.new( :info, 'Hello World', { :scope => 'Application' } )
10
+ # #=> Hello World scope: Application
8
11
  class Event
9
12
  # regex to fetch caller attributes
10
- @@caller_regexp = /^(.+?):(\d+)(?::in `(.+)')?/
13
+ CallerRegexp = /^(.+?):(\d+)(?::in `(.+)')?/
11
14
 
12
15
  # jruby and rubinius seemsto have a different caller
13
- @@caller_index = 2
14
16
  if defined?(RUBY_ENGINE) and ["rbx", "jruby"].include?(RUBY_ENGINE)
15
- @@caller_index =1
17
+ CallerIndex =1
18
+ else
19
+ CallerIndex = 2
16
20
  end
17
21
 
18
22
  # Prefetch those values (no need to do that on every new instance)
@@ -25,6 +29,9 @@ module Yell #:nodoc:
25
29
  # Accessor to the log message
26
30
  attr_reader :message
27
31
 
32
+ # Accessor to additional options
33
+ attr_reader :options
34
+
28
35
  # Accessor to the time the log event occured
29
36
  attr_reader :time
30
37
 
@@ -32,14 +39,15 @@ module Yell #:nodoc:
32
39
  attr_reader :thread_id
33
40
 
34
41
 
35
- def initialize( level, message = nil, &block )
42
+ def initialize( level, message = nil, options = {}, &block )
36
43
  @time = Time.now
37
44
  @level = level
38
45
  @message = block ? block.call : message
46
+ @options = options
39
47
 
40
48
  @thread_id = Thread.current.object_id
41
49
 
42
- @caller = caller[@@caller_index].to_s
50
+ @caller = caller[CallerIndex].to_s
43
51
  @file = nil
44
52
  @line = nil
45
53
  @method = nil
@@ -77,7 +85,7 @@ module Yell #:nodoc:
77
85
  private
78
86
 
79
87
  def _caller!
80
- if m = @@caller_regexp.match( @caller )
88
+ if m = CallerRegexp.match( @caller )
81
89
  @file, @line, @method = m[1..-1]
82
90
  else
83
91
  @file, @line, @method = ['', '', '']
@@ -61,10 +61,11 @@ module Yell #:nodoc:
61
61
  end
62
62
 
63
63
  PatternTable = {
64
- "m" => "message(event)", # Message
65
- "l" => "level(event)[0,1]", # Level (short), e.g.'I', 'W'
66
- "L" => "level(event)", # Level, e.g. 'INFO', 'WARN'
67
- "d" => "date(event)", # ISO8601 Timestamp
64
+ "m" => "message(event.message)", # Message
65
+ "o" => "message(event.options)", # Message options
66
+ "l" => "level(event.level)[0,1]", # Level (short), e.g.'I', 'W'
67
+ "L" => "level(event.level)", # Level, e.g. 'INFO', 'WARN'
68
+ "d" => "date(event.time)", # ISO8601 Timestamp
68
69
  "h" => "event.hostname", # Hostname
69
70
  "p" => "event.pid", # PID
70
71
  "t" => "event.thread_id", # Thread ID
@@ -115,21 +116,24 @@ module Yell #:nodoc:
115
116
  -
116
117
  end
117
118
 
118
- def message( event )
119
- return event.message unless event.message.is_a? Exception
119
+ def message( m )
120
+ case m
121
+ when Hash
122
+ m.map { |k,v| "#{k}: #{v}" }.join( ", " )
123
+ when Exception
124
+ backtrace = m.backtrace ? "\n\t#{m.backtrace.join("\n\t")}" : ""
120
125
 
121
- exception = event.message
122
- backtrace = exception.backtrace ? "\n\t#{exception.backtrace.join("\n\t")}" : ""
123
-
124
- "%s: %s%s" % [exception.class, exception.message, backtrace]
126
+ "%s: %s%s" % [m.class, m.message, backtrace]
127
+ else m
128
+ end
125
129
  end
126
130
 
127
- def level( event )
128
- Yell::Severities[ event.level ]
131
+ def level( l )
132
+ Yell::Severities[ l ]
129
133
  end
130
134
 
131
- def date( event )
132
- @date_pattern ? event.time.strftime( @date_pattern ) : event.time.iso8601
135
+ def date( t )
136
+ @date_pattern ? t.strftime( @date_pattern ) : t.iso8601
133
137
  end
134
138
 
135
139
  end
data/lib/yell/logger.rb CHANGED
@@ -122,10 +122,9 @@ module Yell #:nodoc:
122
122
  class_eval <<-EOS, __FILE__, __LINE__
123
123
  def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
124
124
  #
125
- def #{name}( m = nil, &b ) # def info( m = nil, &b )
125
+ def #{name}( m = nil, o = {}, &b ) # def info( m = nil, o = {}, &b )
126
126
  return unless #{name}? # return unless info?
127
- #
128
- write Yell::Event.new( #{index}, m, &b ) # write Yell::Event.new( 1, m, &b )
127
+ write Yell::Event.new(#{index}, m, o, &b) # write Yell::Event.new(1, m, o, &b)
129
128
  #
130
129
  true # true
131
130
  end # 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 = "0.10.0"
4
+ VERSION = "0.11.0"
5
5
 
6
6
  end
7
7
 
@@ -1,4 +1,4 @@
1
- development:
1
+ test:
2
2
  :level: info
3
3
 
4
4
  :adapters:
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $:.unshift File.expand_path('..', __FILE__)
2
2
  $:.unshift File.expand_path('../../lib', __FILE__)
3
3
 
4
- require 'ostruct'
4
+ ENV['YELL_ENV'] = 'test'
5
5
 
6
6
  require 'yell'
7
7
 
@@ -13,7 +13,14 @@ RSpec.configure do |config|
13
13
  config.mock_framework = :rr
14
14
 
15
15
  config.after do
16
- Dir[ "*.log" ].each { |f| File.delete f }
16
+ Dir[ fixture_path + "/*.log" ].each { |f| File.delete f }
17
17
  end
18
+
19
+ private
20
+
21
+ def fixture_path
22
+ File.expand_path( "fixtures", File.dirname(__FILE__) )
23
+ end
24
+
18
25
  end
19
26
 
@@ -1,45 +1,68 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Yell::Adapters::Datefile do
4
- let( :time ) { Time.now }
5
- let( :filename ) { 'filename.log' }
6
-
4
+ let( :filename ) { fixture_path + '/test.log' }
7
5
  let( :event ) { Yell::Event.new(1, "Hello World") }
8
6
 
7
+ before do
8
+ Timecop.freeze( Time.now )
9
+ end
10
+
9
11
  describe :filename do
10
12
  let( :adapter ) { Yell::Adapters::Datefile.new(:filename => filename) }
11
- let( :date_filename ) { "filename.#{time.strftime(Yell::Adapters::Datefile::DefaultDatePattern)}.log" }
12
-
13
- before do
14
- Timecop.freeze( time )
15
- end
16
13
 
17
14
  it "should be replaced with date_pattern" do
18
15
  adapter.write( event )
19
16
 
20
- File.exist?(date_filename).should be_true
17
+ File.exist?(datefile_filename).should be_true
21
18
  end
22
19
 
23
20
  it "should open file handle only once" do
24
- mock( File ).open( date_filename, anything ) { File.new('/dev/null', 'w') }
21
+ mock( File ).open( datefile_filename, anything ) { File.new('/dev/null', 'w') }
25
22
 
26
23
  adapter.write( event )
27
24
  adapter.write( event )
28
25
  end
29
26
 
30
27
  context "rollover" do
31
- let( :tomorrow ) { time + 86400 }
32
- let( :tomorrow_date_filename ) { "filename.#{tomorrow.strftime(Yell::Adapters::Datefile::DefaultDatePattern)}.log" }
28
+ let( :tomorrow ) { Time.now + 86400 }
29
+ let( :tomorrow_datefile_filename ) { fixture_path + "/test.#{tomorrow.strftime(Yell::Adapters::Datefile::DefaultDatePattern)}.log" }
33
30
 
34
31
  it "should rollover when date has passed" do
35
- mock( File ).open( date_filename, anything ) { File.new('/dev/null', 'w') }
32
+ mock( File ).open( datefile_filename, anything ) { File.new('/dev/null', 'w') }
36
33
  adapter.write( event )
37
34
 
38
- Timecop.freeze( tomorrow ) # tomorrow
35
+ Timecop.freeze( tomorrow )
36
+
37
+ mock( File ).open( tomorrow_datefile_filename, anything ) { File.new('/dev/null', 'w') }
38
+ adapter.write( event )
39
+ end
40
+ end
41
+ end
42
+
43
+ describe :keep do
44
+ let( :adapter ) { Yell::Adapters::Datefile.new(:keep => 2, :filename => filename, :date_pattern => "%M" ) }
45
+
46
+ it "should keep the specified number or files upon wollover" do
47
+ adapter.write( event )
48
+ Dir[ fixture_path + '/*.log' ].size.should == 1
49
+
50
+ Timecop.freeze( Time.now + 60 ) do
51
+ adapter.write( event )
52
+ Dir[ fixture_path + '/*.log' ].size.should == 2
53
+ end
39
54
 
40
- mock( File ).open( tomorrow_date_filename, anything ) { File.new('/dev/null', 'w') }
55
+ Timecop.freeze( Time.now + 120 ) do
41
56
  adapter.write( event )
57
+ Dir[ fixture_path + '/*.log' ].size.should == 2
42
58
  end
43
59
  end
44
60
  end
61
+
62
+
63
+ private
64
+
65
+ def datefile_filename( pattern = Yell::Adapters::Datefile::DefaultDatePattern )
66
+ fixture_path + "/test.#{Time.now.strftime(pattern)}.log"
67
+ end
45
68
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Yell::Configuration do
4
4
 
5
5
  describe ":load!" do
6
- let( :file ) { File.dirname(__FILE__) + '/../fixtures/yell.yml' }
6
+ let( :file ) { fixture_path + '/yell.yml' }
7
7
  let( :config ) { Yell::Configuration.load!( file ) }
8
8
 
9
9
  subject { config }
@@ -17,6 +17,8 @@ class EventFactory
17
17
  end
18
18
 
19
19
  describe Yell::Event do
20
+ let(:event) { Yell::Event.new 1, 'Hello World!', :test => :option }
21
+
20
22
  context :caller do
21
23
  let( :event ) { EventFactory.event 1, "Hello World" }
22
24
 
@@ -36,8 +38,6 @@ describe Yell::Event do
36
38
  end
37
39
  end
38
40
 
39
- let(:event) { Yell::Event.new 1, 'Hello World!' }
40
-
41
41
  context :level do
42
42
  subject { event.level }
43
43
  it { should == 1 }
@@ -48,6 +48,11 @@ describe Yell::Event do
48
48
  it { should == 'Hello World!' }
49
49
  end
50
50
 
51
+ context :options do
52
+ subject { event.options }
53
+ it { should == { :test => :option } }
54
+ end
55
+
51
56
  context :time do
52
57
  subject { event.time }
53
58
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Yell::Formatter do
4
4
 
5
5
  let( :formatter ) { Yell::Formatter.new(subject) }
6
- let( :event ) { Yell::Event.new 1, 'Hello World!' }
6
+ let( :event ) { Yell::Event.new 1, 'Hello World!', :test => :option }
7
7
  let( :time ) { Time.now }
8
8
 
9
9
  let( :format ) { formatter.format(event) }
@@ -17,6 +17,11 @@ describe Yell::Formatter do
17
17
  it { format.should == "Hello World!" }
18
18
  end
19
19
 
20
+ context "%o" do
21
+ subject { "%o" }
22
+ it { format.should == "test: option" }
23
+ end
24
+
20
25
  context "%l" do
21
26
  subject { "%l" }
22
27
  it { format.should == "I" }
@@ -95,7 +100,7 @@ describe Yell::Formatter do
95
100
  end
96
101
 
97
102
  context "Exceptions" do
98
- let( :exception ) { StandardError.new( "message" ) }
103
+ let( :exception ) { StandardError.new( "This is an Exception" ) }
99
104
  let( :event ) { Yell::Event.new( 1, exception ) }
100
105
 
101
106
  subject { "%m" }
@@ -104,7 +109,16 @@ describe Yell::Formatter do
104
109
  mock( exception ).backtrace.times(any_times) { ["backtrace"] }
105
110
  end
106
111
 
107
- it { format.should == "#{exception.class}: #{exception.message}\n\tbacktrace" }
112
+ it { format.should == "StandardError: This is an Exception\n\tbacktrace" }
113
+ end
114
+
115
+ context "Hashes" do
116
+ let( :hash ) { { :test => 'message' } }
117
+ let( :event ) { Yell::Event.new( 1, hash ) }
118
+
119
+ subject { "%m" }
120
+
121
+ it { format.should == "test: message" }
108
122
  end
109
123
 
110
124
  end
@@ -60,7 +60,7 @@ describe Yell::Logger do
60
60
  mock.proxy( Yell::Adapters::Stdout ).new( anything )
61
61
  end
62
62
 
63
- it "should call adapter with :stdout" do
63
+ it "should call adapter with STDOUT" do
64
64
  Yell::Logger.new STDOUT
65
65
  end
66
66
 
@@ -74,7 +74,7 @@ describe Yell::Logger do
74
74
  mock.proxy( Yell::Adapters::Stderr ).new( anything )
75
75
  end
76
76
 
77
- it "should call adapter with :stderr" do
77
+ it "should call adapter with STDERR" do
78
78
  Yell::Logger.new STDERR
79
79
  end
80
80
 
@@ -144,5 +144,6 @@ describe Yell::Logger do
144
144
  factory.bar
145
145
  end
146
146
  end
147
+
147
148
  end
148
149
 
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: 0.10.0
4
+ version: 0.11.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: 2012-05-12 00:00:00.000000000 Z
12
+ date: 2012-05-21 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
@@ -100,3 +100,4 @@ test_files:
100
100
  - spec/yell/level_spec.rb
101
101
  - spec/yell/logger_spec.rb
102
102
  - spec/yell_spec.rb
103
+ has_rdoc: