yell 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/yell.rb CHANGED
@@ -22,6 +22,7 @@
22
22
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
24
  require 'time'
25
+ require 'socket'
25
26
 
26
27
  begin
27
28
  require 'yell/event'
data/lib/yell/adapters.rb CHANGED
@@ -45,6 +45,7 @@ module Yell #:nodoc:
45
45
  end
46
46
  end
47
47
 
48
+ # Base for all adapters
48
49
  require 'yell/adapters/base'
49
50
 
50
51
  # IO based adapters
@@ -18,22 +18,39 @@ module Yell #:nodoc:
18
18
 
19
19
  @date = nil # default; do not override --R
20
20
 
21
+ keep options[:keep]
22
+
21
23
  super
22
24
  end
23
25
 
24
26
  # @overload Reset the file handle
25
27
  def close
26
- @filename = new_filename
28
+ @filename = filename_from( @date )
27
29
 
28
30
  super
29
31
  end
30
32
 
33
+ # Set the amount of logfiles to keep when rolling over
34
+ #
35
+ # @example Keep the last 5 logfiles
36
+ # keep 5
37
+ def keep( n = nil )
38
+ @keep = n
39
+ end
31
40
 
32
41
  private
33
42
 
34
43
  # @overload Close the file if date is expired
35
44
  def write!( event )
36
- close if close?
45
+ if close?
46
+ close
47
+
48
+ unless ::File.exist?( @filename )
49
+ cleanup if cleanup?
50
+
51
+ stream.print( "# -*- #{@date.iso8601} (#{@date.to_f}) [#{@date_pattern}] -*-\n" )
52
+ end
53
+ end
37
54
 
38
55
  super( event )
39
56
  end
@@ -44,8 +61,12 @@ module Yell #:nodoc:
44
61
  # If the current time hits the pattern, it closes the file stream.
45
62
  #
46
63
  # @return [Boolean] true or false
64
+ #
65
+ # TODO: This method causes the datefile adapter to be twice as slow as the file.
66
+ # Let's refactor this.
47
67
  def close?
48
- _date = Time.now.strftime( @date_pattern )
68
+ _date = Time.now
69
+
49
70
  if !@stream or _date != @date
50
71
  @date = _date
51
72
  return true
@@ -54,9 +75,31 @@ module Yell #:nodoc:
54
75
  false
55
76
  end
56
77
 
78
+ def cleanup
79
+ files = Dir[ @file_basename.sub( /(\.\w+)?$/, ".*\\1" ) ].map do |f|
80
+ [ f, metadata_from(f).last ]
81
+ end.select do |(f, p)|
82
+ @date_pattern == p
83
+ end
84
+
85
+ ::File.unlink( *files.map(&:first)[0..-(@keep)] )
86
+ end
87
+
88
+ def cleanup?
89
+ !!@keep
90
+ end
91
+
57
92
  # Sets the filename with the `:date_pattern` appended to it.
58
- def new_filename
59
- @file_basename.sub( /(\.\w+)?$/, ".#{@date}\\1" )
93
+ def filename_from( date )
94
+ @file_basename.sub( /(\.\w+)?$/, ".#{date.strftime(@date_pattern)}\\1" )
95
+ end
96
+
97
+ def metadata_from( file )
98
+ if m = ::File.open( file, &:readline ).match( /^# -\*- (.+) \((\d+\.\d+)\) \[(.+)\] -\*-$/ )
99
+ [ Time.at( m[2].to_f ), m[3] ]
100
+ else
101
+ [ ::File.mtime( file ), "" ]
102
+ end
60
103
  end
61
104
 
62
105
  end
@@ -65,3 +108,4 @@ module Yell #:nodoc:
65
108
 
66
109
  end
67
110
  end
111
+
@@ -67,7 +67,7 @@ module Yell
67
67
 
68
68
  stream.print( message )
69
69
  stream.flush
70
- rescue => e
70
+ rescue Exception => e
71
71
  close
72
72
 
73
73
  # re-raise the exception
data/lib/yell/event.rb CHANGED
@@ -21,6 +21,12 @@ module Yell #:nodoc:
21
21
  # Accessor to the method the log event occured
22
22
  attr_reader :method
23
23
 
24
+ # Accessor to the hostname
25
+ attr_reader :hostname
26
+
27
+ # Accessor to the pid
28
+ attr_reader :pid
29
+
24
30
 
25
31
  # Initialize a new log event
26
32
  def initialize( level, message = nil, &block )
@@ -28,6 +34,16 @@ module Yell #:nodoc:
28
34
  @level = level
29
35
  @message = block ? block.call : message
30
36
 
37
+ @hostname = Socket.gethostname rescue nil
38
+ @pid = Process.pid
39
+
40
+ _initialize_caller
41
+ end
42
+
43
+
44
+ private
45
+
46
+ def _initialize_caller
31
47
  if m = CallerRegexp.match( caller(4).first )
32
48
  @file, @line, @method = m[1..-1]
33
49
  else
@@ -51,16 +51,16 @@ module Yell #:nodoc:
51
51
  class Formatter
52
52
 
53
53
  PatternTable = {
54
- "m" => "event.message", # Message
55
- "l" => "event.level[0,1]", # Level (short), e.g.'I', 'W'
56
- "L" => "event.level", # Level, e.g. 'INFO', 'WARN'
57
- "d" => "date(event)", # ISO8601 Timestamp
58
- "p" => "Process.pid", # PID
59
- "h" => "hostname", # Hostname
60
- "F" => "event.file", # Path with filename where the logger was called
61
- "f" => "File.basename(event.file)", # Filename where the loger was called
62
- "M" => "event.method", # Method name where the logger was called
63
- "n" => "event.line" # Line where the logger was called
54
+ "m" => "event.message", # Message
55
+ "l" => "event.level[0,1]", # Level (short), e.g.'I', 'W'
56
+ "L" => "event.level", # Level, e.g. 'INFO', 'WARN'
57
+ "d" => "date(event)", # ISO8601 Timestamp
58
+ "p" => "event.pid", # PID
59
+ "h" => "event.hostname", # Hostname
60
+ "F" => "event.file", # Path with filename where the logger was called
61
+ "f" => "File.basename(event.file)", # Filename where the loger was called
62
+ "M" => "event.method", # Method name where the logger was called
63
+ "n" => "event.line" # Line where the logger was called
64
64
  }
65
65
 
66
66
  PatternRegexp = /([^%]*)(%\d*)?([#{PatternTable.keys.join}])?(.*)/
@@ -107,12 +107,6 @@ module Yell #:nodoc:
107
107
  @date_pattern ? event.time.strftime( @date_pattern ) : event.time.iso8601
108
108
  end
109
109
 
110
- def hostname
111
- require 'socket' unless defined? Socket
112
-
113
- Socket.gethostname rescue nil
114
- end
115
-
116
110
  end
117
111
  end
118
112
 
data/lib/yell/level.rb CHANGED
@@ -28,6 +28,8 @@ module Yell #:nodoc:
28
28
  # Yell::Level.new.at(:info)
29
29
  class Level
30
30
 
31
+ attr_reader :severities
32
+
31
33
  # Create a new level instance.
32
34
  #
33
35
  # @example Enable all severities
@@ -47,6 +49,7 @@ module Yell #:nodoc:
47
49
  @severities = Yell::Severities.map { true } # all levels allowed by default
48
50
 
49
51
  case severity
52
+ when Yell::Level then @severities = severity.severities
50
53
  when Array then at( *severity )
51
54
  when Range then gte(severity.first).lte(severity.last)
52
55
  when Integer, String, Symbol then gte(severity)
data/lib/yell/logger.rb CHANGED
@@ -34,8 +34,7 @@ module Yell #:nodoc:
34
34
  @options = args.last.is_a?(Hash) ? args.pop : {}
35
35
 
36
36
  # set the log level when given
37
- # level @options[:level] if @options[:level]
38
- level @options[:level] # default
37
+ level @options[:level]
39
38
 
40
39
  # check if filename was given as argument and put it into the @options
41
40
  if args.last.is_a?( String )
@@ -45,9 +44,6 @@ module Yell #:nodoc:
45
44
  # extract adapter
46
45
  adapter args.pop if args.any?
47
46
 
48
- # set the log level when given
49
- level @options[:level] if @options[:level]
50
-
51
47
  # eval the given block
52
48
  instance_eval( &block ) if block
53
49
 
data/lib/yell/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Yell #:nodoc:
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
 
4
4
  end
@@ -4,14 +4,12 @@ describe Yell::Event do
4
4
  let(:event) { Yell::Event.new 'INFO', 'Hello World!' }
5
5
 
6
6
  context :level do
7
- subject { event.level}
8
-
7
+ subject { event.level }
9
8
  it { should == 'INFO' }
10
9
  end
11
10
 
12
11
  context :message do
13
12
  subject { event.message }
14
-
15
13
  it { should == 'Hello World!' }
16
14
  end
17
15
 
@@ -27,6 +25,16 @@ describe Yell::Event do
27
25
  it { should == time }
28
26
  end
29
27
 
28
+ context :hostname do
29
+ subject { event.hostname }
30
+ it { should == Socket.gethostname }
31
+ end
32
+
33
+ context :pid do
34
+ subject { event.pid }
35
+ it { should == Process.pid }
36
+ end
37
+
30
38
  context :caller do
31
39
  let(:file) { "event.rb" }
32
40
  let(:line) { "123" }
@@ -40,19 +48,16 @@ describe Yell::Event do
40
48
 
41
49
  context :file do
42
50
  subject { event.file }
43
-
44
51
  it { should == file }
45
52
  end
46
53
 
47
54
  context :line do
48
55
  subject { event.line }
49
-
50
56
  it { should == line }
51
57
  end
52
58
 
53
59
  context :method do
54
60
  subject { event.method }
55
-
56
61
  it { should == method }
57
62
  end
58
63
  end
@@ -14,37 +14,31 @@ describe Yell::Formatter do
14
14
 
15
15
  context "%m" do
16
16
  subject { "%m" }
17
-
18
17
  it { format.should == "Hello World!" }
19
18
  end
20
19
 
21
20
  context "%l" do
22
21
  subject { "%l" }
23
-
24
22
  it { format.should == "I" }
25
23
  end
26
24
 
27
25
  context "%L" do
28
26
  subject { "%L" }
29
-
30
27
  it { format.should == "INFO" }
31
28
  end
32
29
 
33
30
  context "%d" do
34
31
  subject { "%d" }
35
-
36
32
  it { format.should == time.iso8601 }
37
33
  end
38
34
 
39
35
  context "%p" do
40
36
  subject { "%p" }
41
-
42
37
  it { format.should == Process.pid.to_s }
43
38
  end
44
39
 
45
40
  context "%h" do
46
41
  subject { "%h" }
47
-
48
42
  it { format.should == Socket.gethostname }
49
43
  end
50
44
 
@@ -59,44 +53,37 @@ describe Yell::Formatter do
59
53
 
60
54
  context "%F" do
61
55
  subject { "%F" }
62
-
63
56
  it { format.should == "/path/to/file.rb" }
64
57
  end
65
58
 
66
59
  context "%f" do
67
60
  subject { "%f" }
68
-
69
61
  it { format.should == "file.rb" }
70
62
  end
71
63
 
72
64
  context "%M" do
73
65
  subject { "%M" }
74
-
75
66
  it { format.should == "test_method" }
76
67
  end
77
68
 
78
69
  context "%n" do
79
70
  subject { "%n" }
80
-
81
71
  it { format.should == "123" }
82
72
  end
83
73
  end
84
74
 
85
75
  context "DefaultFormat" do
86
76
  subject { Yell::DefaultFormat }
87
-
88
77
  it { format.should == "#{time.iso8601} [ INFO] #{$$} : Hello World!" }
89
78
  end
90
79
 
91
80
  context "BasicFormat" do
92
81
  subject { Yell::BasicFormat }
93
-
94
82
  it { format.should == "I, #{time.iso8601} : Hello World!" }
95
83
  end
96
84
 
97
85
  context "ExtendedFormat" do
98
86
  subject { Yell::ExtendedFormat }
99
-
100
87
  it { format.should == "#{time.iso8601} [ INFO] #{$$} #{Socket.gethostname} : Hello World!" }
101
88
  end
102
89
 
@@ -67,9 +67,7 @@ describe Yell::Level do
67
67
  end
68
68
 
69
69
  context "given an Array" do
70
- let( :level ) { Yell::Level.new(subject) }
71
-
72
- subject { [:debug, :warn, :fatal] }
70
+ let( :level ) { Yell::Level.new( [:debug, :warn, :fatal] ) }
73
71
 
74
72
  it { level.at?(:debug).should be_true }
75
73
  it { level.at?(:info).should be_false }
@@ -79,9 +77,7 @@ describe Yell::Level do
79
77
  end
80
78
 
81
79
  context "given a Range" do
82
- let( :level ) { Yell::Level.new(subject) }
83
-
84
- subject { (1..3) }
80
+ let( :level ) { Yell::Level.new( (1..3) ) }
85
81
 
86
82
  it { level.at?(:debug).should be_false }
87
83
  it { level.at?(:info).should be_true }
@@ -90,4 +86,13 @@ describe Yell::Level do
90
86
  it { level.at?(:fatal).should be_false }
91
87
  end
92
88
 
89
+ context "given a Yell::Level instance" do
90
+ let( :level ) { Yell::Level.new( :warn ) }
91
+
92
+ it { level.at?(:debug).should be_false }
93
+ it { level.at?(:info).should be_false }
94
+ it { level.at?(:warn).should be_true }
95
+ it { level.at?(:error).should be_true }
96
+ it { level.at?(:fatal).should be_true }
97
+ end
93
98
  end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yell::Logger do
4
+
5
+ context "a Logger instance" do
6
+ let( :logger ) { Yell::Logger.new }
7
+
8
+ context "log methods" do
9
+ subject { logger }
10
+
11
+ it { should respond_to :debug }
12
+ it { should respond_to :debug? }
13
+
14
+ it { should respond_to :info }
15
+ it { should respond_to :info? }
16
+
17
+ it { should respond_to :warn }
18
+ it { should respond_to :warn? }
19
+
20
+ it { should respond_to :error }
21
+ it { should respond_to :error? }
22
+
23
+ it { should respond_to :fatal }
24
+ it { should respond_to :fatal? }
25
+
26
+ it { should respond_to :unknown }
27
+ it { should respond_to :unknown? }
28
+ end
29
+
30
+ context "default adapter" do
31
+ let( :adapters ) { logger.instance_variable_get :@adapters }
32
+
33
+ it { adapters.size.should == 1 }
34
+ it { adapters.first.should be_kind_of Yell::Adapters::File }
35
+ end
36
+ end
37
+
38
+ context "a Logger instance with a given :filename" do
39
+ it "should call adapter with :file" do
40
+ mock.proxy( Yell::Adapters::File ).new( anything )
41
+
42
+ Yell::Logger.new
43
+ end
44
+ end
45
+
46
+ context "a Logger instance with a given :stdout adapter" do
47
+ before do
48
+ mock.proxy( Yell::Adapters::Stdout ).new( anything )
49
+ end
50
+
51
+ it "should call adapter with :stdout" do
52
+ Yell::Logger.new STDOUT
53
+ end
54
+
55
+ it "should call adapter with :stdout" do
56
+ Yell::Logger.new :stdout
57
+ end
58
+ end
59
+
60
+ context "a Logger instance with a given :stderr adapter" do
61
+ before do
62
+ mock.proxy( Yell::Adapters::Stderr ).new( anything )
63
+ end
64
+
65
+ it "should call adapter with :stderr" do
66
+ Yell::Logger.new STDERR
67
+ end
68
+
69
+ it "should call adapter with :stderr" do
70
+ Yell::Logger.new :stderr
71
+ end
72
+ end
73
+
74
+ end
75
+
metadata CHANGED
@@ -1,96 +1,96 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: yell
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
4
5
  prerelease:
5
- version: 0.4.0
6
6
  platform: ruby
7
- authors:
8
- - Rudolf Schmidt
7
+ authors:
8
+ - Rudolf Schmidt
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-03-27 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rr
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- type: :development
25
- version_requirements: *id001
26
- description: An easy to use logging library to log into files and any other self-defined adapters
12
+ date: 2012-03-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rr
16
+ requirement: &70163680872100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70163680872100
25
+ description: An easy to use logging library to log into files and any other self-defined
26
+ adapters
27
27
  email:
28
28
  executables: []
29
-
30
29
  extensions: []
31
-
32
30
  extra_rdoc_files: []
33
-
34
- files:
35
- - .gitignore
36
- - .travis.yml
37
- - Gemfile
38
- - LICENSE.txt
39
- - README.md
40
- - Rakefile
41
- - examples/001-basic-usage.rb
42
- - examples/002.1-log-level-basics.rb
43
- - examples/002.2-log-level-on-certain-severities-only.rb
44
- - examples/002.3-log-level-within-range.rb
45
- - examples/003.1-formatting-DefaultFormat.rb
46
- - examples/003.2-formatting-BasicFormat.rb
47
- - examples/003.3-formatting-ExtendedFormat.rb
48
- - examples/003.4-formatting-on-your-own.rb
49
- - lib/yell.rb
50
- - lib/yell/adapters.rb
51
- - lib/yell/adapters/base.rb
52
- - lib/yell/adapters/datefile.rb
53
- - lib/yell/adapters/file.rb
54
- - lib/yell/adapters/io.rb
55
- - lib/yell/adapters/streams.rb
56
- - lib/yell/event.rb
57
- - lib/yell/formatter.rb
58
- - lib/yell/level.rb
59
- - lib/yell/logger.rb
60
- - lib/yell/version.rb
61
- - spec/spec_helper.rb
62
- - spec/yell/event_spec.rb
63
- - spec/yell/formatter_spec.rb
64
- - spec/yell/level_spec.rb
65
- - spec/yell_spec.rb
66
- - yell.gemspec
31
+ files:
32
+ - .gitignore
33
+ - .travis.yml
34
+ - Gemfile
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - examples/001-basic-usage.rb
39
+ - examples/002.1-log-level-basics.rb
40
+ - examples/002.2-log-level-on-certain-severities-only.rb
41
+ - examples/002.3-log-level-within-range.rb
42
+ - examples/003.1-formatting-DefaultFormat.rb
43
+ - examples/003.2-formatting-BasicFormat.rb
44
+ - examples/003.3-formatting-ExtendedFormat.rb
45
+ - examples/003.4-formatting-on-your-own.rb
46
+ - lib/yell.rb
47
+ - lib/yell/adapters.rb
48
+ - lib/yell/adapters/base.rb
49
+ - lib/yell/adapters/datefile.rb
50
+ - lib/yell/adapters/file.rb
51
+ - lib/yell/adapters/io.rb
52
+ - lib/yell/adapters/streams.rb
53
+ - lib/yell/event.rb
54
+ - lib/yell/formatter.rb
55
+ - lib/yell/level.rb
56
+ - lib/yell/logger.rb
57
+ - lib/yell/version.rb
58
+ - spec/spec_helper.rb
59
+ - spec/yell/event_spec.rb
60
+ - spec/yell/formatter_spec.rb
61
+ - spec/yell/level_spec.rb
62
+ - spec/yell/logger_spec.rb
63
+ - spec/yell_spec.rb
64
+ - yell.gemspec
67
65
  homepage: http://rubygems.org/gems/yell
68
66
  licenses: []
69
-
70
67
  post_install_message:
71
68
  rdoc_options: []
72
-
73
- require_paths:
74
- - lib
75
- required_ruby_version: !ruby/object:Gem::Requirement
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
76
72
  none: false
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: "0"
81
- required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
78
  none: false
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- version: "0"
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
87
83
  requirements: []
88
-
89
84
  rubyforge_project: yell
90
- rubygems_version: 1.8.9
85
+ rubygems_version: 1.8.17
91
86
  signing_key:
92
87
  specification_version: 3
93
88
  summary: Yell - Your Extensible Logging Library
94
- test_files: []
95
-
89
+ test_files:
90
+ - spec/spec_helper.rb
91
+ - spec/yell/event_spec.rb
92
+ - spec/yell/formatter_spec.rb
93
+ - spec/yell/level_spec.rb
94
+ - spec/yell/logger_spec.rb
95
+ - spec/yell_spec.rb
96
96
  has_rdoc: