Permalink
Browse files

ST: Major Yell::Event refactoring:

- Yell::Formatter %o is now obsolete
- You may pass as many messages to a Yell::Logger instance
  • Loading branch information...
1 parent dc473e3 commit 29c31f492c280058cef66ebe92f233e938df89f0 @rudionrails committed Oct 3, 2012
Showing with 64 additions and 42 deletions.
  1. +6 −8 lib/yell/event.rb
  2. +14 −11 lib/yell/formatter.rb
  3. +8 −8 lib/yell/logger.rb
  4. +4 −9 spec/yell/event_spec.rb
  5. +1 −6 spec/yell/formatter_spec.rb
  6. +31 −0 spec/yell/logger_spec.rb
View
@@ -22,11 +22,8 @@ class Event
# Accessor to the log level
attr_reader :level
- # Accessor to the log message
- attr_reader :message
-
- # Accessor to additional options
- attr_reader :options
+ # Accessor to the log messages
+ attr_reader :messages
# Accessor to the time the log event occured
attr_reader :time
@@ -35,11 +32,12 @@ class Event
attr_reader :thread_id
- def initialize( level, message = nil, options = {}, &block )
+ def initialize( level, *messages, &block )
@time = Time.now
@level = level
- @message = block ? block.call : message
- @options = options
+
+ @messages = messages
+ @messages << block.call if block
@thread_id = Thread.current.object_id
View
@@ -61,8 +61,7 @@ def format=( pattern )
end
PatternTable = {
- "m" => "message(event.message)", # Message
- "o" => "message(event.options)", # Message options
+ "m" => "message(*event.messages)", # Message
"l" => "level(event.level)[0,1]", # Level (short), e.g.'I', 'W'
"L" => "level(event.level)", # Level, e.g. 'INFO', 'WARN'
"d" => "date(event.time)", # ISO8601 Timestamp
@@ -116,7 +115,19 @@ def format( event )
-
end
- def message( m )
+ def level( l )
+ Yell::Severities[ l ]
+ end
+
+ def date( t )
+ @date_pattern ? t.strftime( @date_pattern ) : t.iso8601
+ end
+
+ def message( *messages )
+ messages.map { |m| to_message(m) }.join(" ")
+ end
+
+ def to_message( m )
case m
when Hash
m.map { |k,v| "#{k}: #{v}" }.join( ", " )
@@ -129,14 +140,6 @@ def message( m )
end
end
- def level( l )
- Yell::Severities[ l ]
- end
-
- def date( t )
- @date_pattern ? t.strftime( @date_pattern ) : t.iso8601
- end
-
end
end
View
@@ -113,14 +113,14 @@ def close
name = s.downcase
class_eval <<-EOS, __FILE__, __LINE__
- def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
- #
- def #{name}( m = nil, o = {}, &b ) # def info( m = nil, o = {}, &b )
- return false unless #{name}? # return false unless info?
- write Yell::Event.new(#{index}, m, o, &b) # write Yell::Event.new(1, m, o, &b)
- #
- true # true
- end # end
+ def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
+ #
+ def #{name}( *m, &b ) # def info( *m, &b )
+ return false unless #{name}? # return false unless info?
+ write Yell::Event.new(#{index}, *m, &b) # write Yell::Event.new(1, *m, &b)
+ #
+ true # true
+ end # end
EOS
end
View
@@ -17,7 +17,7 @@ def self._event( level, message )
end
describe Yell::Event do
- let(:event) { Yell::Event.new 1, 'Hello World!', :test => :option }
+ let(:event) { Yell::Event.new 1, 'Hello World!' }
context :caller do
let( :event ) { EventFactory.event 1, "Hello World" }
@@ -43,14 +43,9 @@ def self._event( level, message )
it { should == 1 }
end
- context :message do
- subject { event.message }
- it { should == 'Hello World!' }
- end
-
- context :options do
- subject { event.options }
- it { should == { :test => :option } }
+ context :messages do
+ subject { event.messages }
+ it { should == ['Hello World!'] }
end
context :time do
@@ -3,7 +3,7 @@
describe Yell::Formatter do
let( :formatter ) { Yell::Formatter.new(subject) }
- let( :event ) { Yell::Event.new 1, 'Hello World!', :test => :option }
+ let( :event ) { Yell::Event.new 1, 'Hello World!' }
let( :time ) { Time.now }
let( :format ) { formatter.format(event) }
@@ -17,11 +17,6 @@
it { format.should == "Hello World!" }
end
- context "%o" do
- subject { "%o" }
- it { format.should == "test: option" }
- end
-
context "%l" do
subject { "%l" }
it { format.should == "I" }
View
@@ -13,6 +13,7 @@ def bar
end
describe Yell::Logger do
+ let( :filename ) { fixture_path + '/logger.log' }
context "a Logger instance" do
let( :logger ) { Yell::Logger.new }
@@ -155,5 +156,35 @@ def bar
end
end
+ context "logging" do
+ let( :logger ) { Yell::Logger.new(filename, :format => "%m") }
+ let( :line ) { File.open(filename, &:readline) }
+
+ it "should output a single message" do
+ logger.info "Hello World"
+ line.should == "Hello World\n"
+ end
+
+ it "should output multiple messages" do
+ logger.info "Hello", "W", "o", "r", "l", "d"
+ line.should == "Hello W o r l d\n"
+ end
+
+ it "should output a hash and message" do
+ logger.info "Hello World", :test => :message
+ line.should == "Hello World test: message\n"
+ end
+
+ it "should output a hash and message" do
+ logger.info( {:test => :message}, "Hello World" )
+ line.should == "test: message Hello World\n"
+ end
+
+ it "should output a hash and block" do
+ logger.info(:test => :message) { "Hello World" }
+ line.should == "test: message Hello World\n"
+ end
+ end
+
end

0 comments on commit 29c31f4

Please sign in to comment.