yell 0.11.0 → 0.12.0
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/adapters/datefile.rb +49 -29
- data/lib/yell/adapters/file.rb +1 -1
- data/lib/yell/logger.rb +22 -22
- data/lib/yell/version.rb +1 -1
- data/spec/yell/adapters/datefile_spec.rb +24 -3
- metadata +2 -2
@@ -10,26 +10,32 @@ module Yell #:nodoc:
|
|
10
10
|
# The default date pattern, e.g. "19820114" (14 Jan 1982)
|
11
11
|
DefaultDatePattern = "%Y%m%d"
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
# Metadata
|
14
|
+
Metadata = lambda { |date, pattern| "# -*- #{date.iso8601} (#{date.to_f}) [#{pattern}] -*-" }
|
15
|
+
MetadataRegexp = /^# -\*- (.+) \((\d+\.\d+)\) \[(.+)\] -\*-$/
|
16
16
|
|
17
|
-
@file_basename = options[:filename] || default_filename
|
18
|
-
options[:filename] = @file_basename
|
19
17
|
|
18
|
+
setup do |options|
|
20
19
|
@date = nil # default; do not override --R
|
20
|
+
|
21
|
+
self.date_pattern = options.fetch( :date_pattern, DefaultDatePattern )
|
22
|
+
self.symlink_original_filename = options.fetch( :symlink_original_filename, false )
|
23
|
+
self.keep = options.fetch( :keep, 0 )
|
24
|
+
|
25
|
+
@original_filename = options.fetch( :filename, default_filename )
|
26
|
+
options[:filename] = @original_filename
|
21
27
|
end
|
22
28
|
|
23
29
|
write do |event|
|
24
|
-
|
25
|
-
|
30
|
+
return unless close? # do nothing when not closing
|
31
|
+
close
|
26
32
|
|
27
|
-
|
28
|
-
cleanup if keep > 0
|
33
|
+
return if ::File.exist?( @filename ) # do nothing when file ready present
|
29
34
|
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
cleanup! if cleanup?
|
36
|
+
symlink_original_filename! if symlink_original_filename
|
37
|
+
|
38
|
+
stream.puts( Metadata.call(@date, date_pattern) )
|
33
39
|
end
|
34
40
|
|
35
41
|
close do
|
@@ -37,20 +43,32 @@ module Yell #:nodoc:
|
|
37
43
|
end
|
38
44
|
|
39
45
|
|
40
|
-
#
|
46
|
+
# The pattern to be used for the files
|
47
|
+
#
|
48
|
+
# @example
|
49
|
+
# date_pattern = "%Y%m%d" # default
|
50
|
+
# date_pattern = "%Y-week-%V"
|
41
51
|
attr_accessor :date_pattern
|
42
52
|
|
43
|
-
#
|
44
|
-
|
53
|
+
# Tell the adapter to create a symlink onto the currently
|
54
|
+
# active (timestamped) file. Upon rollover, the symlink is
|
55
|
+
# set to the newly created file, and so on.
|
56
|
+
#
|
57
|
+
# @example
|
58
|
+
# symlink_original_filename = true
|
59
|
+
attr_accessor :symlink_original_filename
|
45
60
|
|
46
|
-
# Set the amount of logfiles to keep when rolling over
|
61
|
+
# Set the amount of logfiles to keep when rolling over.
|
62
|
+
# By default, no files will be cleaned up.
|
47
63
|
#
|
48
64
|
# @example Keep the last 5 logfiles
|
49
65
|
# keep = 5
|
50
66
|
# keep = '10'
|
51
|
-
|
52
|
-
|
53
|
-
|
67
|
+
#
|
68
|
+
# @example Do not clean up any files
|
69
|
+
# keep = 0
|
70
|
+
attr_accessor :keep
|
71
|
+
|
54
72
|
|
55
73
|
private
|
56
74
|
|
@@ -60,9 +78,6 @@ module Yell #:nodoc:
|
|
60
78
|
# If the current time hits the pattern, it closes the file stream.
|
61
79
|
#
|
62
80
|
# @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.
|
66
81
|
def close?
|
67
82
|
_date = Time.now
|
68
83
|
|
@@ -75,27 +90,32 @@ module Yell #:nodoc:
|
|
75
90
|
end
|
76
91
|
|
77
92
|
# Cleanup old files
|
78
|
-
def cleanup
|
79
|
-
files = Dir[ @
|
93
|
+
def cleanup!
|
94
|
+
files = Dir[ @original_filename.sub( /(\.\w+)?$/, ".*\\1" ) ].map do |f|
|
80
95
|
[ f, metadata_from(f).last ]
|
81
96
|
end.select do |(_, p)|
|
82
|
-
|
97
|
+
date_pattern == p
|
83
98
|
end
|
84
99
|
|
85
|
-
::File.unlink( *files.map(&:first)[0..-
|
100
|
+
::File.unlink( *files.map(&:first)[0..-keep] )
|
86
101
|
end
|
87
102
|
|
88
103
|
def cleanup?
|
89
|
-
|
104
|
+
keep.to_i > 0
|
105
|
+
end
|
106
|
+
|
107
|
+
def symlink_original_filename!
|
108
|
+
::File.unlink( @original_filename ) if ::File.symlink?( @original_filename )
|
109
|
+
::File.symlink( @filename, @original_filename )
|
90
110
|
end
|
91
111
|
|
92
112
|
# Sets the filename with the `:date_pattern` appended to it.
|
93
113
|
def filename_from( date )
|
94
|
-
@
|
114
|
+
@original_filename.sub( /(\.\w+)?$/, ".#{date.strftime(date_pattern)}\\1" )
|
95
115
|
end
|
96
116
|
|
97
117
|
def metadata_from( file )
|
98
|
-
if m = ::File.open( file, &:readline ).match(
|
118
|
+
if m = ::File.open( file, &:readline ).match( MetadataRegexp )
|
99
119
|
[ Time.at( m[2].to_f ), m[3] ]
|
100
120
|
else
|
101
121
|
[ ::File.mtime( file ), "" ]
|
data/lib/yell/adapters/file.rb
CHANGED
data/lib/yell/logger.rb
CHANGED
@@ -6,31 +6,31 @@ module Yell #:nodoc:
|
|
6
6
|
#
|
7
7
|
# A +Yell::Logger+ instance holds all your adapters and sends the log events
|
8
8
|
# to them if applicable. There are multiple ways of how to create a new logger.
|
9
|
-
#
|
10
|
-
# @example A standard file logger
|
11
|
-
# Yell::Logger.new
|
12
|
-
# Yell::Logger.new 'development.log'
|
13
|
-
#
|
14
|
-
# @example A standard datefile logger
|
15
|
-
# Yell::Logger.new :datefile
|
16
|
-
# Yell::Logger.new :datefile, 'development.log'
|
17
|
-
#
|
18
|
-
# @example Setting the log level
|
19
|
-
# Yell::Logger.new :level => :warn
|
20
|
-
#
|
21
|
-
# Yell::Logger.new do |l|
|
22
|
-
# l.level = :warn
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# @example Combined settings
|
26
|
-
# Yell::Logger.new 'development.log', :level => :warn
|
27
|
-
#
|
28
|
-
# Yell::Logger.new :datefile, 'development.log' do |l|
|
29
|
-
# l.level = :info
|
30
|
-
# end
|
31
9
|
class Logger
|
32
10
|
include Yell::Level::Helpers
|
33
11
|
|
12
|
+
# Initialize a new Logger
|
13
|
+
#
|
14
|
+
# @example A standard file logger
|
15
|
+
# Yell::Logger.new 'development.log'
|
16
|
+
#
|
17
|
+
# @example A standard datefile logger
|
18
|
+
# Yell::Logger.new :datefile
|
19
|
+
# Yell::Logger.new :datefile, 'development.log'
|
20
|
+
#
|
21
|
+
# @example Setting the log level
|
22
|
+
# Yell::Logger.new :level => :warn
|
23
|
+
#
|
24
|
+
# Yell::Logger.new do |l|
|
25
|
+
# l.level = :warn
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @example Combined settings
|
29
|
+
# Yell::Logger.new 'development.log', :level => :warn
|
30
|
+
#
|
31
|
+
# Yell::Logger.new :datefile, 'development.log' do |l|
|
32
|
+
# l.level = :info
|
33
|
+
# end
|
34
34
|
def initialize( *args, &block )
|
35
35
|
@adapters = []
|
36
36
|
|
data/lib/yell/version.rb
CHANGED
@@ -28,7 +28,7 @@ describe Yell::Adapters::Datefile do
|
|
28
28
|
let( :tomorrow ) { Time.now + 86400 }
|
29
29
|
let( :tomorrow_datefile_filename ) { fixture_path + "/test.#{tomorrow.strftime(Yell::Adapters::Datefile::DefaultDatePattern)}.log" }
|
30
30
|
|
31
|
-
it "should
|
31
|
+
it "should rotate when date has passed" do
|
32
32
|
mock( File ).open( datefile_filename, anything ) { File.new('/dev/null', 'w') }
|
33
33
|
adapter.write( event )
|
34
34
|
|
@@ -41,9 +41,9 @@ describe Yell::Adapters::Datefile do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
describe :keep do
|
44
|
-
let( :adapter ) { Yell::Adapters::Datefile.new(:keep => 2, :filename => filename, :date_pattern => "%M"
|
44
|
+
let( :adapter ) { Yell::Adapters::Datefile.new(:keep => 2, :filename => filename, :date_pattern => "%M") }
|
45
45
|
|
46
|
-
it "should keep the specified number or files upon
|
46
|
+
it "should keep the specified number or files upon rollover" do
|
47
47
|
adapter.write( event )
|
48
48
|
Dir[ fixture_path + '/*.log' ].size.should == 1
|
49
49
|
|
@@ -59,6 +59,27 @@ describe Yell::Adapters::Datefile do
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
describe :symlink_original_filename do
|
63
|
+
let( :adapter ) { Yell::Adapters::Datefile.new(:symlink_original_filename => true, :filename => filename, :date_pattern => "%M") }
|
64
|
+
let( :time ) { Time.now }
|
65
|
+
|
66
|
+
it "should symlink to the orignal given :filename" do
|
67
|
+
Timecop.freeze( time ) do
|
68
|
+
adapter.write( event )
|
69
|
+
|
70
|
+
File.symlink?( filename ).should be_true
|
71
|
+
File.readlink( filename ).should == datefile_filename( adapter.date_pattern )
|
72
|
+
end
|
73
|
+
|
74
|
+
Timecop.freeze( time + 60 ) do
|
75
|
+
adapter.write( event )
|
76
|
+
|
77
|
+
File.symlink?( filename ).should be_true
|
78
|
+
File.readlink( filename ).should == datefile_filename( adapter.date_pattern )
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
62
83
|
|
63
84
|
private
|
64
85
|
|
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.
|
4
|
+
version: 0.12.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-
|
12
|
+
date: 2012-06-29 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
|