yell 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'monitor'
3
+ require 'thread'
4
4
 
5
5
  module Yell #:nodoc:
6
6
  module Adapters #:nodoc:
7
7
 
8
- class Mutex
9
- include MonitorMixin
10
- end
11
-
12
8
  # This class provides the basic interface for all allowed operations on any
13
9
  # adapter implementation. Other adapters should inherit from it for the methods
14
10
  # used by the {Yell::Logger}.
@@ -117,10 +113,9 @@ module Yell #:nodoc:
117
113
  #
118
114
  # You should not overload the constructor, use #setup instead.
119
115
  def initialize( options = {}, &block )
120
- @mutex = Yell::Adapters::Mutex.new
116
+ @mutex = Mutex.new
121
117
 
122
118
  setup!(options)
123
-
124
119
  block.call(self) if block
125
120
  end
126
121
 
@@ -10,13 +10,13 @@ module Yell #:nodoc:
10
10
 
11
11
  # The possible unix log colors
12
12
  Colors = {
13
- 'DEBUG' => "\e[32;1m", # green;bold
14
- # 'INFO' => "\e[0m", # white
15
- 'WARN' => "\e[33;1m", # yello;bold
16
- 'ERROR' => "\e[31;1m", # red;bold
17
- 'FATAL' => "\e[35;1m", # magenta;bold
18
- 'UNKNOWN' => "\e[36m", # cyan
19
- 'DEFAULT' => "\e[0m" # NONE
13
+ 0 => "\e[32;1m", # green;bold
14
+ # 1 => "\e[0m", # white
15
+ 2 => "\e[33;1m", # yello;bold
16
+ 3 => "\e[31;1m", # red;bold
17
+ 4 => "\e[35;1m", # magenta;bold
18
+ 5 => "\e[36m", # cyan
19
+ -1 => "\e[0m" # NONE
20
20
  }
21
21
 
22
22
  setup do |options|
@@ -31,7 +31,7 @@ module Yell #:nodoc:
31
31
 
32
32
  # colorize if applicable
33
33
  if colors and color = Colors[event.level]
34
- message = color + message + Colors['DEFAULT']
34
+ message = color + message + Colors[-1]
35
35
  end
36
36
 
37
37
  message << "\n" unless message[-1] == ?\n # add new line if there is none
data/lib/yell/event.rb CHANGED
@@ -1,9 +1,23 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'time'
4
+ require 'socket'
5
+
3
6
  module Yell #:nodoc:
4
7
 
5
8
  class Event
6
- CallerRegexp = /^(.+?):(\d+)(?::in `(.+)')?/
9
+ # regex to fetch caller attributes
10
+ @@caller_regexp = /^(.+?):(\d+)(?::in `(.+)')?/
11
+
12
+ # jruby and rubinius seemsto have a different caller
13
+ @@caller_index = 2
14
+ if defined?(RUBY_ENGINE) and ["rbx", "jruby"].include?(RUBY_ENGINE)
15
+ @@caller_index =1
16
+ end
17
+
18
+ # Prefetch those values (no need to do that on every new instance)
19
+ @@hostname = Socket.gethostname rescue nil
20
+ @@pid = Process.pid
7
21
 
8
22
  # Accessor to the log level
9
23
  attr_reader :level
@@ -14,21 +28,6 @@ module Yell #:nodoc:
14
28
  # Accessor to the time the log event occured
15
29
  attr_reader :time
16
30
 
17
- # Accessor to filename the log event occured
18
- attr_reader :file
19
-
20
- # Accessor to the line the log event occured
21
- attr_reader :line
22
-
23
- # Accessor to the method the log event occured
24
- attr_reader :method
25
-
26
- # Accessor to the hostname
27
- attr_reader :hostname
28
-
29
- # Accessor to the pid
30
- attr_reader :pid
31
-
32
31
  # Accessor to the current tread_id
33
32
  attr_reader :thread_id
34
33
 
@@ -38,18 +37,47 @@ module Yell #:nodoc:
38
37
  @level = level
39
38
  @message = block ? block.call : message
40
39
 
41
- @hostname = Socket.gethostname rescue nil
42
- @pid = Process.pid
43
40
  @thread_id = Thread.current.object_id
44
41
 
45
- _initialize_caller
42
+ @caller = caller[@@caller_index].to_s
43
+ @file = nil
44
+ @line = nil
45
+ @method = nil
46
+ end
47
+
48
+ # Accessor to the pid
49
+ def hostname
50
+ @@hostname
51
+ end
52
+
53
+ # Accessor to the hostname
54
+ def pid
55
+ @@pid
56
+ end
57
+
58
+ # Accessor to filename the log event occured
59
+ def file
60
+ _caller! if @file.nil?
61
+ @file
62
+ end
63
+
64
+ # Accessor to the line the log event occured
65
+ def line
66
+ _caller! if @line.nil?
67
+ @line
68
+ end
69
+
70
+ # Accessor to the method the log event occured
71
+ def method
72
+ _caller! if @method.nil?
73
+ @method
46
74
  end
47
75
 
48
76
 
49
77
  private
50
78
 
51
- def _initialize_caller
52
- if m = CallerRegexp.match( caller(4).first )
79
+ def _caller!
80
+ if m = @@caller_regexp.match( @caller )
53
81
  @file, @line, @method = m[1..-1]
54
82
  else
55
83
  @file, @line, @method = ['', '', '']
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'time'
4
+
3
5
  module Yell #:nodoc:
4
6
 
5
7
  # No format on the log message
@@ -59,9 +61,9 @@ module Yell #:nodoc:
59
61
  end
60
62
 
61
63
  PatternTable = {
62
- "m" => "event.message", # Message
63
- "l" => "event.level[0,1]", # Level (short), e.g.'I', 'W'
64
- "L" => "event.level", # Level, e.g. 'INFO', 'WARN'
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'
65
67
  "d" => "date(event)", # ISO8601 Timestamp
66
68
  "h" => "event.hostname", # Hostname
67
69
  "p" => "event.pid", # PID
@@ -100,7 +102,7 @@ module Yell #:nodoc:
100
102
  buff << match[1] unless match[1].empty?
101
103
  break if match[2].nil?
102
104
 
103
- buff << match[2] + 's' # '%s'
105
+ buff << match[2] + 's'
104
106
  args << PatternTable[ match[3] ]
105
107
 
106
108
  _pattern = match[4]
@@ -113,6 +115,19 @@ module Yell #:nodoc:
113
115
  -
114
116
  end
115
117
 
118
+ def message( event )
119
+ return event.message unless event.message.is_a? Exception
120
+
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]
125
+ end
126
+
127
+ def level( event )
128
+ Yell::Severities[ event.level ]
129
+ end
130
+
116
131
  def date( event )
117
132
  @date_pattern ? event.time.strftime( @date_pattern ) : event.time.iso8601
118
133
  end
data/lib/yell/logger.rb CHANGED
@@ -49,8 +49,8 @@ module Yell #:nodoc:
49
49
  self.adapter args.pop if args.any?
50
50
 
51
51
  # eval the given block
52
- block.call(self) if block
53
- # _call( &block ) if block
52
+ # block.call(self) if block
53
+ _call( &block ) if block
54
54
 
55
55
  # default adapter when none defined
56
56
  adapter :file if @adapters.empty?
@@ -118,15 +118,15 @@ module Yell #:nodoc:
118
118
  name = s.downcase
119
119
 
120
120
  class_eval <<-EOS, __FILE__, __LINE__
121
- def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
122
- #
123
- def #{name}( m = nil, &b ) # def info( m = nil, &b )
124
- return unless #{name}? # return unless info?
125
- #
126
- write Yell::Event.new( '#{s}', m, &b ) # write Yell::Event.new( "INFO", m, &b )
127
- #
128
- true # true
129
- end # end
121
+ def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
122
+ #
123
+ def #{name}( m = nil, &b ) # def info( m = nil, &b )
124
+ return unless #{name}? # return unless info?
125
+ #
126
+ write Yell::Event.new( #{index}, m, &b ) # write Yell::Event.new( 1, m, &b )
127
+ #
128
+ true # true
129
+ end # end
130
130
  EOS
131
131
  end
132
132
 
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.7.0"
4
+ VERSION = "0.8.0"
5
5
 
6
6
  end
7
7
 
data/lib/yell.rb CHANGED
@@ -21,9 +21,6 @@
21
21
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
- require 'time'
25
- require 'socket'
26
-
27
24
  module Yell #:nodoc:
28
25
  Severities = [ 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'UNKNOWN' ]
29
26
 
@@ -39,7 +39,7 @@ describe Yell::Adapters::Base do
39
39
  subject { Yell::Adapters::Base.new :level => :info }
40
40
 
41
41
  it "should delegate :event to :write!" do
42
- event = Yell::Event.new( "INFO", "Hello World!" )
42
+ event = Yell::Event.new( 1, "Hello World!" )
43
43
 
44
44
  mock( subject ).write!( event )
45
45
 
@@ -47,7 +47,7 @@ describe Yell::Adapters::Base do
47
47
  end
48
48
 
49
49
  it "should not write when event does not have the right level" do
50
- event = Yell::Event.new( "DEBUG", "Hello World!" )
50
+ event = Yell::Event.new( 0, "Hello World!" )
51
51
 
52
52
  dont_allow( subject ).write!( event )
53
53
 
@@ -4,7 +4,7 @@ describe Yell::Adapters::Datefile do
4
4
  let( :time ) { Time.now }
5
5
  let( :filename ) { 'filename.log' }
6
6
 
7
- let( :event ) { Yell::Event.new("INFO", "Hello World") }
7
+ let( :event ) { Yell::Event.new(1, "Hello World") }
8
8
 
9
9
  describe :filename do
10
10
  let( :adapter ) { Yell::Adapters::Datefile.new(:filename => filename) }
@@ -16,7 +16,7 @@ describe Yell::Adapters::File do
16
16
  end
17
17
 
18
18
  context :write do
19
- let( :event ) { Yell::Event.new("INFO", "Hello World") }
19
+ let( :event ) { Yell::Event.new(1, "Hello World") }
20
20
 
21
21
  context "default filename" do
22
22
  let( :filename ) { "#{Yell.env}.log" }
@@ -47,7 +47,7 @@ describe Yell::Adapters::Io do
47
47
  end
48
48
 
49
49
  context :write do
50
- let( :event ) { Yell::Event.new("INFO", "Hello World") }
50
+ let( :event ) { Yell::Event.new(1, "Hello World") }
51
51
  let( :adapter ) { Yell::Adapters::Io.new }
52
52
  let( :stream ) { File.new('/dev/null', 'w') }
53
53
 
@@ -1,11 +1,46 @@
1
1
  require 'spec_helper'
2
2
 
3
+ # Since Yell::Event.new is not called directly, but through
4
+ # the logger methods, we need to divert here in order to get
5
+ # the correct caller.
6
+ class EventFactory
7
+ def self.event( level, message )
8
+ self._event( level, message )
9
+ end
10
+
11
+ private
12
+
13
+ def self._event( level, message )
14
+ Yell::Event.new level, message
15
+ end
16
+
17
+ end
18
+
3
19
  describe Yell::Event do
4
- let(:event) { Yell::Event.new 'INFO', 'Hello World!' }
20
+ context :caller do
21
+ let( :event ) { EventFactory.event 1, "Hello World" }
22
+
23
+ context :file do
24
+ subject { event.file }
25
+ it { should == __FILE__ }
26
+ end
27
+
28
+ context :line do
29
+ subject { event.line }
30
+ it { should == "8" }
31
+ end
32
+
33
+ context :method do
34
+ subject { event.method }
35
+ it { should == 'event' }
36
+ end
37
+ end
38
+
39
+ let(:event) { Yell::Event.new 1, 'Hello World!' }
5
40
 
6
41
  context :level do
7
42
  subject { event.level }
8
- it { should == 'INFO' }
43
+ it { should == 1 }
9
44
  end
10
45
 
11
46
  context :message do
@@ -35,31 +70,4 @@ describe Yell::Event do
35
70
  it { should == Process.pid }
36
71
  end
37
72
 
38
- context :caller do
39
- let(:file) { "event.rb" }
40
- let(:line) { "123" }
41
- let(:method) { "test_method" }
42
-
43
- before do
44
- any_instance_of( Yell::Event ) do |e|
45
- mock( e ).caller(4) { ["#{file}:#{line}:in `#{method}'"] }
46
- end
47
- end
48
-
49
- context :file do
50
- subject { event.file }
51
- it { should == file }
52
- end
53
-
54
- context :line do
55
- subject { event.line }
56
- it { should == line }
57
- end
58
-
59
- context :method do
60
- subject { event.method }
61
- it { should == method }
62
- end
63
- end
64
-
65
73
  end
@@ -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 'INFO', 'Hello World!' }
6
+ let( :event ) { Yell::Event.new 1, 'Hello World!' }
7
7
  let( :time ) { Time.now }
8
8
 
9
9
  let( :format ) { formatter.format(event) }
@@ -48,11 +48,13 @@ describe Yell::Formatter do
48
48
  end
49
49
 
50
50
  context "caller" do
51
- let( :_caller ) { ["/path/to/file.rb:123:in `test_method'"] }
51
+ let( :_caller ) { [nil, nil, "/path/to/file.rb:123:in `test_method'"] }
52
52
 
53
53
  before do
54
54
  any_instance_of( Yell::Event ) do |e|
55
- mock(e).caller(4) { _caller }
55
+ stub(e).file { "/path/to/file.rb" }
56
+ stub(e).line { "123" }
57
+ stub(e).method { "test_method" }
56
58
  end
57
59
  end
58
60
 
@@ -92,4 +94,17 @@ describe Yell::Formatter do
92
94
  it { format.should == "#{time.iso8601} [ INFO] #{$$} #{Socket.gethostname} : Hello World!" }
93
95
  end
94
96
 
97
+ context "Exceptions" do
98
+ let( :exception ) { StandardError.new( "message" ) }
99
+ let( :event ) { Yell::Event.new( 1, exception ) }
100
+
101
+ subject { "%m" }
102
+
103
+ before do
104
+ mock( exception ).backtrace.times(any_times) { ["backtrace"] }
105
+ end
106
+
107
+ it { format.should == "#{exception.class}: #{exception.message}\n\tbacktrace" }
108
+ end
109
+
95
110
  end
@@ -1,5 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class LoggerFactory
4
+ attr_accessor :logger
5
+
6
+ def foo
7
+ logger.info :foo
8
+ end
9
+
10
+ def bar
11
+ logger.info :bar
12
+ end
13
+ end
14
+
3
15
  describe Yell::Logger do
4
16
 
5
17
  context "a Logger instance" do
@@ -35,7 +47,7 @@ describe Yell::Logger do
35
47
  end
36
48
  end
37
49
 
38
- context "a Logger instance with a given :filename" do
50
+ context "initialize with a :filename" do
39
51
  it "should call adapter with :file" do
40
52
  mock.proxy( Yell::Adapters::File ).new( :filename => 'test.log' )
41
53
 
@@ -43,7 +55,7 @@ describe Yell::Logger do
43
55
  end
44
56
  end
45
57
 
46
- context "a Logger instance with a given :stdout adapter" do
58
+ context "initialize with a :stdout adapter" do
47
59
  before do
48
60
  mock.proxy( Yell::Adapters::Stdout ).new( anything )
49
61
  end
@@ -57,7 +69,7 @@ describe Yell::Logger do
57
69
  end
58
70
  end
59
71
 
60
- context "a Logger instance with a given :stderr adapter" do
72
+ context "initialize with a :stderr adapter" do
61
73
  before do
62
74
  mock.proxy( Yell::Adapters::Stderr ).new( anything )
63
75
  end
@@ -71,7 +83,7 @@ describe Yell::Logger do
71
83
  end
72
84
  end
73
85
 
74
- context "a Logger instance with a given block" do
86
+ context "initialize with a block" do
75
87
  let( :level ) { Yell::Level.new :error }
76
88
  let( :adapter ) { Yell::Adapters::Stdout.new }
77
89
 
@@ -94,5 +106,20 @@ describe Yell::Logger do
94
106
  end
95
107
  end
96
108
 
109
+ context "caller's :file, :line and :method" do
110
+ let( :adapter ) { Yell::Adapters::Stdout.new :format => "%F, %n: %M" }
111
+ let( :logger ) { Yell::Logger.new { |l| l.adapter adapter } }
112
+
113
+ it "should write correctly" do
114
+ factory = LoggerFactory.new
115
+ factory.logger = logger
116
+
117
+ mock( adapter.send(:stream) ).write( "#{__FILE__}, 7: foo\n" )
118
+ mock( adapter.send(:stream) ).write( "#{__FILE__}, 11: bar\n" )
119
+
120
+ factory.foo
121
+ factory.bar
122
+ end
123
+ end
97
124
  end
98
125
 
metadata CHANGED
@@ -1,56 +1,67 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: yell
3
- version: !ruby/object:Gem::Version
4
- version: 0.7.0
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 8
8
+ - 0
9
+ version: 0.8.0
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Rudolf Schmidt
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2012-04-21 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2012-04-24 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: rspec
16
- requirement: &70348235288880 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
22
  prerelease: false
24
- version_requirements: *70348235288880
25
- - !ruby/object:Gem::Dependency
26
- name: rr
27
- requirement: &70348235288440 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
33
30
  type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rr
34
34
  prerelease: false
35
- version_requirements: *70348235288440
36
- - !ruby/object:Gem::Dependency
37
- name: timecop
38
- requirement: &70348235288020 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
44
42
  type: :development
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: timecop
45
46
  prerelease: false
46
- version_requirements: *70348235288020
47
- description: Yell - Your Extensible Logging Library. Define multiple adapters, various
48
- log level combinations or message formatting options like you've never done before
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :development
55
+ version_requirements: *id003
56
+ description: Yell - Your Extensible Logging Library. Define multiple adapters, various log level combinations or message formatting options like you've never done before
49
57
  email:
50
58
  executables: []
59
+
51
60
  extensions: []
61
+
52
62
  extra_rdoc_files: []
53
- files:
63
+
64
+ files:
54
65
  - .gitignore
55
66
  - .travis.yml
56
67
  - Gemfile
@@ -91,31 +102,37 @@ files:
91
102
  - spec/yell/logger_spec.rb
92
103
  - spec/yell_spec.rb
93
104
  - yell.gemspec
105
+ has_rdoc: true
94
106
  homepage: http://rudionrails.github.com/yell
95
107
  licenses: []
108
+
96
109
  post_install_message:
97
110
  rdoc_options: []
98
- require_paths:
111
+
112
+ require_paths:
99
113
  - lib
100
- required_ruby_version: !ruby/object:Gem::Requirement
101
- none: false
102
- requirements:
103
- - - ! '>='
104
- - !ruby/object:Gem::Version
105
- version: '0'
106
- required_rubygems_version: !ruby/object:Gem::Requirement
107
- none: false
108
- requirements:
109
- - - ! '>='
110
- - !ruby/object:Gem::Version
111
- version: '0'
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
112
128
  requirements: []
129
+
113
130
  rubyforge_project: yell
114
- rubygems_version: 1.8.17
131
+ rubygems_version: 1.3.6
115
132
  signing_key:
116
133
  specification_version: 3
117
134
  summary: Yell - Your Extensible Logging Library
118
- test_files:
135
+ test_files:
119
136
  - spec/spec_helper.rb
120
137
  - spec/yell/adapters/base_spec.rb
121
138
  - spec/yell/adapters/datefile_spec.rb
@@ -128,4 +145,3 @@ test_files:
128
145
  - spec/yell/level_spec.rb
129
146
  - spec/yell/logger_spec.rb
130
147
  - spec/yell_spec.rb
131
- has_rdoc: