yell 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yell/adapters/base.rb +37 -1
- data/lib/yell/adapters/datefile.rb +5 -0
- data/lib/yell/adapters/file.rb +3 -21
- data/lib/yell/adapters/io.rb +25 -2
- data/lib/yell/adapters/streams.rb +6 -10
- data/lib/yell/configuration.rb +6 -1
- data/lib/yell/event.rb +8 -2
- data/lib/yell/formatter.rb +24 -10
- data/lib/yell/level.rb +16 -0
- data/lib/yell/logger.rb +17 -0
- data/lib/yell/version.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/yell/adapters/io_spec.rb +1 -7
- data/spec/yell/event_spec.rb +4 -0
- data/spec/yell/formatter_spec.rb +11 -7
- data/spec/yell/logger_spec.rb +2 -2
- metadata +2 -3
data/lib/yell/adapters/base.rb
CHANGED
@@ -64,11 +64,21 @@ module Yell #:nodoc:
|
|
64
64
|
compile!( :write!, &block )
|
65
65
|
end
|
66
66
|
|
67
|
+
# Define your open method with this helper.
|
68
|
+
#
|
69
|
+
# @example Open a file handle
|
70
|
+
# open do
|
71
|
+
# @stream = ::File.open( 'test.log', ::File::WRONLY|::File::APPEND|::File::CREAT )
|
72
|
+
# end
|
73
|
+
def open( &block )
|
74
|
+
compile!( :open!, &block )
|
75
|
+
end
|
76
|
+
|
67
77
|
# Define your close method with this helper.
|
68
78
|
#
|
69
79
|
# @example Closing a file handle
|
70
80
|
# close do
|
71
|
-
# @
|
81
|
+
# @stream.close
|
72
82
|
# end
|
73
83
|
def close( &block )
|
74
84
|
compile!( :close!, &block )
|
@@ -152,6 +162,17 @@ module Yell #:nodoc:
|
|
152
162
|
close!
|
153
163
|
end
|
154
164
|
|
165
|
+
# 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
|
+
def inspect
|
173
|
+
inspection = inspectables.inject( [] ) { |r, c| r << "#{c}: #{send(c).inspect}" }
|
174
|
+
"#<#{self.class.name} #{inspection * ', '}>"
|
175
|
+
end
|
155
176
|
|
156
177
|
private
|
157
178
|
|
@@ -171,6 +192,14 @@ module Yell #:nodoc:
|
|
171
192
|
# Not implemented
|
172
193
|
end
|
173
194
|
|
195
|
+
# Perform the actual open.
|
196
|
+
#
|
197
|
+
# Adapter classes should provide their own implementation
|
198
|
+
# of this method.
|
199
|
+
def open!
|
200
|
+
# Not implemented
|
201
|
+
end
|
202
|
+
|
174
203
|
# Perform the actual close.
|
175
204
|
#
|
176
205
|
# Adapter classes should provide their own implementation
|
@@ -191,6 +220,13 @@ module Yell #:nodoc:
|
|
191
220
|
@level.nil? || @level.at?( event.level )
|
192
221
|
end
|
193
222
|
|
223
|
+
# Get an array of inspected attributes for the adapter.
|
224
|
+
#
|
225
|
+
# @return [ String ] An array of pretty printed field values.
|
226
|
+
def inspectables
|
227
|
+
[ :level ]
|
228
|
+
end
|
229
|
+
|
194
230
|
end
|
195
231
|
|
196
232
|
end
|
data/lib/yell/adapters/file.rb
CHANGED
@@ -9,32 +9,14 @@ module Yell #:nodoc:
|
|
9
9
|
|
10
10
|
setup do |options|
|
11
11
|
@filename = ::File.expand_path options.fetch(:filename, default_filename)
|
12
|
-
|
13
|
-
# sync immediately to IO (or not)
|
14
|
-
self.sync = options.fetch(:sync, true)
|
15
12
|
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
#
|
20
|
-
# When true (default), every log event is immediately written to the file.
|
21
|
-
# When false, the log event is buffered internally.
|
22
|
-
attr_accessor :sync
|
23
|
-
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
# @overload Lazily open the file handle
|
28
|
-
def stream
|
29
|
-
synchronize { @stream or open! }
|
14
|
+
open do
|
15
|
+
@stream = ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
|
30
16
|
end
|
31
17
|
|
32
|
-
def open!
|
33
|
-
@stream = ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
|
34
|
-
@stream.sync = sync
|
35
18
|
|
36
|
-
|
37
|
-
end
|
19
|
+
private
|
38
20
|
|
39
21
|
def default_filename #:nodoc:
|
40
22
|
logdir = ::File.expand_path("log")
|
data/lib/yell/adapters/io.rb
CHANGED
@@ -20,8 +20,14 @@ module Yell #:nodoc:
|
|
20
20
|
setup do |options|
|
21
21
|
@stream = nil
|
22
22
|
|
23
|
+
# colorize the log output (default: false)
|
23
24
|
self.colors = options.fetch(:colors, false)
|
25
|
+
|
26
|
+
# format the log message (default: nil)
|
24
27
|
self.format = options.fetch(:format, nil)
|
28
|
+
|
29
|
+
# sync immediately to IO (default: true)
|
30
|
+
self.sync = options.fetch(:sync, true)
|
25
31
|
end
|
26
32
|
|
27
33
|
write do |event|
|
@@ -35,7 +41,12 @@ module Yell #:nodoc:
|
|
35
41
|
# add new line if there is none
|
36
42
|
message << "\n" unless message[-1] == ?\n
|
37
43
|
|
38
|
-
stream.
|
44
|
+
stream.syswrite( message )
|
45
|
+
end
|
46
|
+
|
47
|
+
open do
|
48
|
+
@stream.sync = self.sync if @stream.respond_to? :sync
|
49
|
+
@stream.flush if @stream.respond_to? :flush
|
39
50
|
end
|
40
51
|
|
41
52
|
close do
|
@@ -44,6 +55,12 @@ module Yell #:nodoc:
|
|
44
55
|
end
|
45
56
|
|
46
57
|
|
58
|
+
# Sets the “sync mode” to true or false.
|
59
|
+
#
|
60
|
+
# When true (default), every log event is immediately written to the file.
|
61
|
+
# When false, the log event is buffered internally.
|
62
|
+
attr_accessor :sync
|
63
|
+
|
47
64
|
# Sets colored output on or off (default off)
|
48
65
|
#
|
49
66
|
# @example Enable colors
|
@@ -67,8 +84,14 @@ module Yell #:nodoc:
|
|
67
84
|
# Adapter classes should provide their own implementation
|
68
85
|
# of this method.
|
69
86
|
def stream
|
70
|
-
|
87
|
+
synchronize { open! if @stream.nil?; @stream }
|
88
|
+
end
|
89
|
+
|
90
|
+
# @override
|
91
|
+
def inspectables
|
92
|
+
super.concat [:format, :colors, :sync]
|
71
93
|
end
|
94
|
+
|
72
95
|
end
|
73
96
|
|
74
97
|
end
|
@@ -5,22 +5,18 @@ module Yell #:nodoc:
|
|
5
5
|
|
6
6
|
class Stdout < Yell::Adapters::Io
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
# @overload Lazily open the STDOUT stream
|
11
|
-
def stream
|
12
|
-
@stream ||= $stdout.clone
|
8
|
+
open do
|
9
|
+
@stream = $stdout.clone
|
13
10
|
end
|
11
|
+
|
14
12
|
end
|
15
13
|
|
16
14
|
class Stderr < Yell::Adapters::Io
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
# @overload Lazily open the STDERR stream
|
21
|
-
def stream
|
22
|
-
@stream ||= $stderr.clone
|
16
|
+
open do
|
17
|
+
@stream = $stderr.clone
|
23
18
|
end
|
19
|
+
|
24
20
|
end
|
25
21
|
|
26
22
|
register( :stdout, Yell::Adapters::Stdout )
|
data/lib/yell/configuration.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require 'erb'
|
3
4
|
require 'yaml'
|
4
5
|
|
5
6
|
module Yell #:nodoc:
|
@@ -9,7 +10,11 @@ module Yell #:nodoc:
|
|
9
10
|
class Configuration
|
10
11
|
|
11
12
|
def self.load!( file )
|
12
|
-
|
13
|
+
# parse through ERB
|
14
|
+
yaml = ERB.new(File.read(file)).result
|
15
|
+
|
16
|
+
# parse through YAML
|
17
|
+
YAML.load(yaml)[Yell.env] || {}
|
13
18
|
end
|
14
19
|
|
15
20
|
end
|
data/lib/yell/event.rb
CHANGED
@@ -18,6 +18,7 @@ module Yell #:nodoc:
|
|
18
18
|
# Prefetch those values (no need to do that on every new instance)
|
19
19
|
@@hostname = Socket.gethostname rescue nil
|
20
20
|
@@pid = Process.pid
|
21
|
+
@@progname = $0
|
21
22
|
|
22
23
|
# Accessor to the log level
|
23
24
|
attr_reader :level
|
@@ -47,16 +48,21 @@ module Yell #:nodoc:
|
|
47
48
|
@method = nil
|
48
49
|
end
|
49
50
|
|
50
|
-
# Accessor to the
|
51
|
+
# Accessor to the hostname
|
51
52
|
def hostname
|
52
53
|
@@hostname
|
53
54
|
end
|
54
55
|
|
55
|
-
# Accessor to the
|
56
|
+
# Accessor to the pid
|
56
57
|
def pid
|
57
58
|
@@pid
|
58
59
|
end
|
59
60
|
|
61
|
+
# Accessor to the progname
|
62
|
+
def progname
|
63
|
+
@@progname
|
64
|
+
end
|
65
|
+
|
60
66
|
# Accessor to filename the log event occured
|
61
67
|
def file
|
62
68
|
_caller! if @file.nil?
|
data/lib/yell/formatter.rb
CHANGED
@@ -67,6 +67,7 @@ module Yell #:nodoc:
|
|
67
67
|
"d" => "date(event.time)", # ISO8601 Timestamp
|
68
68
|
"h" => "event.hostname", # Hostname
|
69
69
|
"p" => "event.pid", # PID
|
70
|
+
"P" => "event.progname", # Progname
|
70
71
|
"t" => "event.thread_id", # Thread ID
|
71
72
|
"F" => "event.file", # Path with filename where the logger was called
|
72
73
|
"f" => "File.basename(event.file)", # Filename where the loger was called
|
@@ -84,11 +85,21 @@ module Yell #:nodoc:
|
|
84
85
|
# message formatting.
|
85
86
|
def initialize( pattern = nil, date_pattern = nil )
|
86
87
|
@pattern = pattern || Yell::DefaultFormat
|
87
|
-
@date_pattern = date_pattern
|
88
|
+
@date_pattern = date_pattern || :iso8601
|
88
89
|
|
89
90
|
define!
|
90
91
|
end
|
91
92
|
|
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.
|
100
|
+
def inspect
|
101
|
+
"#<#{self.class.name} pattern: #{@pattern.inspect}, date_pattern: #{@date_pattern.inspect}>"
|
102
|
+
end
|
92
103
|
|
93
104
|
private
|
94
105
|
|
@@ -120,7 +131,10 @@ module Yell #:nodoc:
|
|
120
131
|
end
|
121
132
|
|
122
133
|
def date( t )
|
123
|
-
|
134
|
+
case @date_pattern
|
135
|
+
when String then t.strftime( @date_pattern )
|
136
|
+
else t.iso8601
|
137
|
+
end
|
124
138
|
end
|
125
139
|
|
126
140
|
def message( *messages )
|
@@ -129,14 +143,14 @@ module Yell #:nodoc:
|
|
129
143
|
|
130
144
|
def to_message( m )
|
131
145
|
case m
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
146
|
+
when Hash
|
147
|
+
m.map { |k,v| "#{k}: #{v}" }.join( ", " )
|
148
|
+
when Exception
|
149
|
+
backtrace = m.backtrace ? "\n\t#{m.backtrace.join("\n\t")}" : ""
|
150
|
+
|
151
|
+
"%s: %s%s" % [m.class, m.message, backtrace]
|
152
|
+
else
|
153
|
+
m
|
140
154
|
end
|
141
155
|
end
|
142
156
|
|
data/lib/yell/level.rb
CHANGED
@@ -138,6 +138,22 @@ 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.
|
148
|
+
def inspect
|
149
|
+
severities = Yell::Severities.each.with_index.inject( [] ) do |r, (l, i)|
|
150
|
+
r << l if @severities[i]
|
151
|
+
r
|
152
|
+
end
|
153
|
+
|
154
|
+
"#<#{self.class.name} severities: #{severities * ', '}>"
|
155
|
+
end
|
156
|
+
|
141
157
|
|
142
158
|
private
|
143
159
|
|
data/lib/yell/logger.rb
CHANGED
@@ -124,6 +124,16 @@ module Yell #:nodoc:
|
|
124
124
|
EOS
|
125
125
|
end
|
126
126
|
|
127
|
+
# Get a pretty string representation of the logger.
|
128
|
+
#
|
129
|
+
# @example Inspect the logger
|
130
|
+
# logger.inspect
|
131
|
+
#
|
132
|
+
# @return [String] The inspection string.
|
133
|
+
def inspect
|
134
|
+
inspection = inspectables.inject( [] ) { |r, c| r << "#{c}: #{send(c).inspect}" }
|
135
|
+
"#<#{self.class.name} #{inspection * ', '}, adapters: #{@adapters.map(&:inspect) * ', '}>"
|
136
|
+
end
|
127
137
|
|
128
138
|
private
|
129
139
|
|
@@ -149,6 +159,13 @@ module Yell #:nodoc:
|
|
149
159
|
@adapters.each { |a| a.write(event) }
|
150
160
|
end
|
151
161
|
|
162
|
+
# Get an array of inspected attributes for the adapter.
|
163
|
+
#
|
164
|
+
# @return [ String ] An array of pretty printed field values.
|
165
|
+
def inspectables
|
166
|
+
[ :name, :level ]
|
167
|
+
end
|
168
|
+
|
152
169
|
end
|
153
170
|
end
|
154
171
|
|
data/lib/yell/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -14,10 +14,10 @@ RSpec.configure do |config|
|
|
14
14
|
|
15
15
|
config.before do
|
16
16
|
Yell::Repository.loggers.clear
|
17
|
+
Dir[ fixture_path + "/*.log" ].each { |f| File.delete f }
|
17
18
|
end
|
18
19
|
|
19
20
|
config.after do
|
20
|
-
Dir[ fixture_path + "/*.log" ].each { |f| File.delete f }
|
21
21
|
end
|
22
22
|
|
23
23
|
private
|
@@ -40,12 +40,6 @@ describe Yell::Adapters::Io do
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
context :stream do
|
44
|
-
it "should raise" do
|
45
|
-
lambda { Yell::Adapters::Io.new.send :stream }.should raise_error("Not implemented" )
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
43
|
context :write do
|
50
44
|
let( :event ) { Yell::Event.new(1, "Hello World") }
|
51
45
|
let( :adapter ) { Yell::Adapters::Io.new }
|
@@ -63,7 +57,7 @@ describe Yell::Adapters::Io do
|
|
63
57
|
|
64
58
|
it "should print formatted message to stream" do
|
65
59
|
formatted = Yell::Formatter.new.format( event )
|
66
|
-
mock( stream ).
|
60
|
+
mock( stream ).syswrite( formatted << "\n" )
|
67
61
|
|
68
62
|
adapter.write( event )
|
69
63
|
end
|
data/spec/yell/event_spec.rb
CHANGED
data/spec/yell/formatter_spec.rb
CHANGED
@@ -14,37 +14,41 @@ describe Yell::Formatter do
|
|
14
14
|
|
15
15
|
context "%m" do
|
16
16
|
subject { "%m" }
|
17
|
-
it { format.should ==
|
17
|
+
it { format.should == event.messages.join(' ') }
|
18
18
|
end
|
19
19
|
|
20
20
|
context "%l" do
|
21
21
|
subject { "%l" }
|
22
|
-
it { format.should ==
|
22
|
+
it { format.should == Yell::Severities[event.level][0,1] }
|
23
23
|
end
|
24
24
|
|
25
25
|
context "%L" do
|
26
26
|
subject { "%L" }
|
27
|
-
it { format.should ==
|
27
|
+
it { format.should == Yell::Severities[event.level] }
|
28
28
|
end
|
29
29
|
|
30
30
|
context "%d" do
|
31
31
|
subject { "%d" }
|
32
|
-
it { format.should == time.iso8601 }
|
32
|
+
it { format.should == event.time.iso8601 }
|
33
33
|
end
|
34
34
|
|
35
35
|
context "%p" do
|
36
36
|
subject { "%p" }
|
37
|
-
it { format.should ==
|
37
|
+
it { format.should == event.pid.to_s }
|
38
38
|
end
|
39
39
|
|
40
|
+
context "%P" do
|
41
|
+
subject { "%P" }
|
42
|
+
it { format.should == event.progname }
|
43
|
+
end
|
40
44
|
context "%t" do
|
41
45
|
subject { "%t" }
|
42
|
-
it { format.should ==
|
46
|
+
it { format.should == event.thread_id.to_s }
|
43
47
|
end
|
44
48
|
|
45
49
|
context "%h" do
|
46
50
|
subject { "%h" }
|
47
|
-
it { format.should ==
|
51
|
+
it { format.should == event.hostname }
|
48
52
|
end
|
49
53
|
|
50
54
|
context "caller" do
|
data/spec/yell/logger_spec.rb
CHANGED
@@ -148,8 +148,8 @@ describe Yell::Logger do
|
|
148
148
|
factory = LoggerFactory.new
|
149
149
|
factory.logger = logger
|
150
150
|
|
151
|
-
mock( adapter.send(:stream) ).
|
152
|
-
mock( adapter.send(:stream) ).
|
151
|
+
mock( adapter.send(:stream) ).syswrite( "#{__FILE__}, 7: foo\n" )
|
152
|
+
mock( adapter.send(:stream) ).syswrite( "#{__FILE__}, 11: bar\n" )
|
153
153
|
|
154
154
|
factory.foo
|
155
155
|
factory.bar
|
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.
|
4
|
+
version: 1.2.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-10-
|
12
|
+
date: 2012-10-30 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
|
@@ -111,4 +111,3 @@ test_files:
|
|
111
111
|
- spec/yell/logger_spec.rb
|
112
112
|
- spec/yell/repository_spec.rb
|
113
113
|
- spec/yell_spec.rb
|
114
|
-
has_rdoc:
|