yell 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -83,6 +83,15 @@ end
83
83
  [How To: Different Adapters for Different Log Levels](https://github.com/rudionrails/yell/wiki/101-different-adapters-for-different-log-levels)
84
84
 
85
85
 
86
+ ### Additional Adapters
87
+ [Syslog](https://github.com/rudionrails/yell/wiki/additional-adapters-syslog)
88
+ [Graylog2 (GELF)](https://github.com/rudionrails/yell/wiki/additional-adapters-gelf)
89
+
90
+
91
+ ### Development
92
+
93
+ [How To: Writing Your Own Adapter](https://github.com/rudionrails/yell/wiki/Writing-your-own-adapter)
94
+
86
95
  You can find further examples and additional adapters in the [wiki](https://github.com/rudionrails/yell/wiki).
87
96
  or have a look into the examples folder.
88
97
 
@@ -1,8 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'monitor'
4
+
3
5
  module Yell #:nodoc:
4
6
  module Adapters #:nodoc:
5
7
 
8
+ class Mutex
9
+ include MonitorMixin
10
+ end
11
+
6
12
  # This class provides the basic interface for all allowed operations on any
7
13
  # adapter implementation. Other adapters should inherit from it for the methods
8
14
  # used by the {Yell::Logger}.
@@ -111,6 +117,8 @@ module Yell #:nodoc:
111
117
  #
112
118
  # You should not overload the constructor, use #setup instead.
113
119
  def initialize( options = {}, &block )
120
+ @mutex = Yell::Adapters::Mutex.new
121
+
114
122
  setup!(options)
115
123
 
116
124
  block.call(self) if block
@@ -121,10 +129,10 @@ module Yell #:nodoc:
121
129
  # The method receives the log `event` and determines whether to
122
130
  # actually write or not.
123
131
  def write( event )
124
- write!( event ) if write?( event )
132
+ synchronize { write!(event) } if write?(event)
125
133
  rescue Exception => e
126
134
  # make sure the adapter is closed and re-raise the exception
127
- close
135
+ synchronize { close }
128
136
 
129
137
  raise( e )
130
138
  end
@@ -176,6 +184,10 @@ module Yell #:nodoc:
176
184
  @level.nil? || @level.at?( event.level )
177
185
  end
178
186
 
187
+ def synchronize( &block )
188
+ @mutex.synchronize( &block )
189
+ end
190
+
179
191
  end
180
192
 
181
193
  end
@@ -11,7 +11,8 @@ module Yell #:nodoc:
11
11
  DefaultDatePattern = "%Y%m%d"
12
12
 
13
13
  setup do |options|
14
- @date_pattern = options[:date_pattern] || DefaultDatePattern
14
+ self.date_pattern = options[:date_pattern] || DefaultDatePattern
15
+ self.keep = options[:keep]
15
16
 
16
17
  @file_basename = options[:filename] || default_filename
17
18
  options[:filename] = @file_basename
@@ -20,14 +21,37 @@ module Yell #:nodoc:
20
21
  end
21
22
 
22
23
  write do |event|
23
- close if close?
24
+ if close?
25
+ close
26
+
27
+ unless ::File.exist?( @filename )
28
+ cleanup if keep > 0
29
+
30
+ stream.print( "# -*- #{@date.iso8601} (#{@date.to_f}) [#{date_pattern}] -*-\n" )
31
+ end
32
+ end
24
33
  end
25
34
 
26
35
  close do
27
- @filename = @file_basename.sub( /(\.\w+)?$/, ".#{@date}\\1" )
36
+ @filename = filename_from( @date )
28
37
  end
29
38
 
30
39
 
40
+ # Accesor to the date_pattern
41
+ attr_accessor :date_pattern
42
+
43
+ # Accessor to keep
44
+ attr_reader :keep
45
+
46
+ # Set the amount of logfiles to keep when rolling over
47
+ #
48
+ # @example Keep the last 5 logfiles
49
+ # keep = 5
50
+ # keep = '10'
51
+ def keep=( val )
52
+ @keep = val.to_i
53
+ end
54
+
31
55
  private
32
56
 
33
57
  # Determines whether to close the file handle or not.
@@ -36,9 +60,13 @@ module Yell #:nodoc:
36
60
  # If the current time hits the pattern, it closes the file stream.
37
61
  #
38
62
  # @return [Boolean] true or false
63
+ #
64
+ # TODO: This method causes the datefile adapter to be twice as slow as the file.
65
+ # Let's refactor this.
39
66
  def close?
40
- _date = Time.now.strftime( @date_pattern )
41
- if !@stream or _date != @date
67
+ _date = Time.now
68
+
69
+ if @stream.nil? or _date != @date
42
70
  @date = _date
43
71
  return true
44
72
  end
@@ -46,9 +74,38 @@ module Yell #:nodoc:
46
74
  false
47
75
  end
48
76
 
77
+ # Cleanup old files
78
+ def cleanup
79
+ files = Dir[ @file_basename.sub( /(\.\w+)?$/, ".*\\1" ) ].map do |f|
80
+ [ f, metadata_from(f).last ]
81
+ end.select do |(_, 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
+
92
+ # Sets the filename with the `:date_pattern` appended to it.
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
103
+ end
104
+
49
105
  end
50
106
 
51
107
  register( :datefile, Yell::Adapters::Datefile )
52
108
 
53
109
  end
54
110
  end
111
+
@@ -16,7 +16,14 @@ module Yell #:nodoc:
16
16
 
17
17
  # @overload Lazily open the file handle
18
18
  def stream
19
- @stream ||= ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
19
+ @stream or open!
20
+ end
21
+
22
+ def open!
23
+ @stream = ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
24
+ @stream.sync = true
25
+
26
+ @stream
20
27
  end
21
28
 
22
29
  def default_filename #:nodoc:
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'monitor'
4
+
3
5
  module Yell #:nodoc:
4
6
  module Adapters #:nodoc:
5
7
 
@@ -18,6 +20,8 @@ module Yell #:nodoc:
18
20
  }
19
21
 
20
22
  setup do |options|
23
+ @stream = nil
24
+
21
25
  self.colors = options[:colors]
22
26
  self.format = options[:format]
23
27
  end
@@ -32,8 +36,7 @@ module Yell #:nodoc:
32
36
 
33
37
  message << "\n" unless message[-1] == ?\n # add new line if there is none
34
38
 
35
- stream.print( message )
36
- stream.flush
39
+ stream.write( message )
37
40
  end
38
41
 
39
42
  close do
@@ -57,7 +60,6 @@ module Yell #:nodoc:
57
60
  def stream
58
61
  raise 'Not implemented'
59
62
  end
60
-
61
63
  end
62
64
 
63
65
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Yell #:nodoc:
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
 
6
6
  end
7
7
 
@@ -63,9 +63,7 @@ describe Yell::Adapters::Io do
63
63
 
64
64
  it "should print formatted message to stream" do
65
65
  formatted = Yell::Formatter.new.format( event )
66
-
67
- mock( stream ).print( formatted << "\n" )
68
- mock( stream ).flush
66
+ mock( stream ).write( formatted << "\n" )
69
67
 
70
68
  adapter.write( event )
71
69
  end
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.6.0
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-12 00:00:00.000000000 Z
12
+ date: 2012-04-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &17866520 !ruby/object:Gem::Requirement
16
+ requirement: &70348235288880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *17866520
24
+ version_requirements: *70348235288880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rr
27
- requirement: &17866000 !ruby/object:Gem::Requirement
27
+ requirement: &70348235288440 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *17866000
35
+ version_requirements: *70348235288440
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: timecop
38
- requirement: &17865580 !ruby/object:Gem::Requirement
38
+ requirement: &70348235288020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *17865580
46
+ version_requirements: *70348235288020
47
47
  description: Yell - Your Extensible Logging Library. Define multiple adapters, various
48
48
  log level combinations or message formatting options like you've never done before
49
49
  email:
@@ -111,9 +111,21 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  requirements: []
113
113
  rubyforge_project: yell
114
- rubygems_version: 1.8.10
114
+ rubygems_version: 1.8.17
115
115
  signing_key:
116
116
  specification_version: 3
117
117
  summary: Yell - Your Extensible Logging Library
118
- test_files: []
118
+ test_files:
119
+ - spec/spec_helper.rb
120
+ - spec/yell/adapters/base_spec.rb
121
+ - spec/yell/adapters/datefile_spec.rb
122
+ - spec/yell/adapters/file_spec.rb
123
+ - spec/yell/adapters/io_spec.rb
124
+ - spec/yell/adapters/streams_spec.rb
125
+ - spec/yell/adapters_spec.rb
126
+ - spec/yell/event_spec.rb
127
+ - spec/yell/formatter_spec.rb
128
+ - spec/yell/level_spec.rb
129
+ - spec/yell/logger_spec.rb
130
+ - spec/yell_spec.rb
119
131
  has_rdoc: