logbert 0.6.14 → 0.6.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/logbert/handlers.rb +2 -44
- data/lib/logbert/handlers/base_handler.rb +30 -0
- data/lib/logbert/handlers/stream_handler.rb +31 -0
- data/lib/logbert/handlers/time_rotator.rb +80 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9c6e7cee9ea7cc78ff9fffdac0d78200909876e
|
4
|
+
data.tar.gz: a47fa209a40fb1186cc1317ac9b98670b5534d14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c84631b1cdb64102253b875c728c75592b03336bc261f93526aa11abb42135f10d513ea17a07d2b0895e4f5010a4111d8c08333af31f4aa85bc891583da32cda
|
7
|
+
data.tar.gz: f3dd689d7c3b4c25d27bfef241279153bd1ddb113c29d74ebf0884dc425b2b39787df79596a46a64664884b3f78fe0c083a6aead47058f0a8f29a7a8006f34a4
|
data/lib/logbert/handlers.rb
CHANGED
@@ -1,45 +1,3 @@
|
|
1
1
|
|
2
|
-
require 'logbert/
|
3
|
-
|
4
|
-
module Logbert
|
5
|
-
module Handlers
|
6
|
-
|
7
|
-
|
8
|
-
class BaseHandler
|
9
|
-
|
10
|
-
def formatter
|
11
|
-
@formatter ||= Logbert::Formatters::SimpleFormatter.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def formatter=(value)
|
15
|
-
@formatter = value
|
16
|
-
end
|
17
|
-
|
18
|
-
def publish(message)
|
19
|
-
emit self.formatter.format(message)
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
def emit(output)
|
24
|
-
raise NotImplementedError
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
class StreamHandler < BaseHandler
|
31
|
-
attr_accessor :stream
|
32
|
-
|
33
|
-
def initialize(stream = $stderr)
|
34
|
-
@stream = stream
|
35
|
-
end
|
36
|
-
|
37
|
-
def emit(output)
|
38
|
-
@stream.puts output
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
end
|
45
|
-
end
|
2
|
+
require 'logbert/handlers/stream_handler.rb'
|
3
|
+
require 'logbert/handlers/time_rotator.rb'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
require 'logbert/formatters'
|
3
|
+
|
4
|
+
module Logbert
|
5
|
+
module Handlers
|
6
|
+
|
7
|
+
|
8
|
+
class BaseHandler
|
9
|
+
|
10
|
+
def formatter
|
11
|
+
@formatter ||= Logbert::Formatters::SimpleFormatter.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def formatter=(value)
|
15
|
+
@formatter = value
|
16
|
+
end
|
17
|
+
|
18
|
+
def publish(message)
|
19
|
+
emit self.formatter.format(message)
|
20
|
+
end
|
21
|
+
|
22
|
+
def emit(output)
|
23
|
+
raise NotImplementedError
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
require 'logbert/handlers/base_handler'
|
3
|
+
|
4
|
+
module Logbert
|
5
|
+
|
6
|
+
module Handlers
|
7
|
+
|
8
|
+
class StreamHandler < Logbert::Handlers::BaseHandler
|
9
|
+
attr_reader :stream
|
10
|
+
|
11
|
+
def initialize(stream = $stderr)
|
12
|
+
@stream = stream
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.for_path(path)
|
16
|
+
fout = File.open(path, "ab")
|
17
|
+
StreamHandler.new fout
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def emit(output)
|
22
|
+
@stream.puts output
|
23
|
+
@stream.flush
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,80 @@
|
|
1
|
+
|
2
|
+
require 'fileutils'
|
3
|
+
require 'logbert/handlers/base_handler'
|
4
|
+
|
5
|
+
module Logbert
|
6
|
+
|
7
|
+
module Handlers
|
8
|
+
|
9
|
+
class LocaltimeFormatter
|
10
|
+
|
11
|
+
attr_accessor :format
|
12
|
+
|
13
|
+
def initialize(format = "%Y-%m-%d-%H%M")
|
14
|
+
@format = format
|
15
|
+
end
|
16
|
+
|
17
|
+
def format(time)
|
18
|
+
time.strftime(@format)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
class TimeRotator < Logbert::Handlers::BaseHandler
|
24
|
+
|
25
|
+
|
26
|
+
attr_reader :path, :stream, :timestamp_formatter
|
27
|
+
attr_reader :interval, :expiration_time
|
28
|
+
|
29
|
+
def initialize(path, options = {})
|
30
|
+
@path = path
|
31
|
+
@timestamp_formatter = options[:timestamp_formatter] || LocaltimeFormatter.new
|
32
|
+
@interval = options.fetch(:iterval, 24 * 60 * 60)
|
33
|
+
|
34
|
+
rotate_log!
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def rotation_needed?
|
39
|
+
Time.now > @expiration_time
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def rotate_log!
|
44
|
+
if @stream and not @stream.closed?
|
45
|
+
@stream.close
|
46
|
+
end
|
47
|
+
|
48
|
+
if File.exists? @path
|
49
|
+
FileUtils.mv @path, archive_destination
|
50
|
+
end
|
51
|
+
|
52
|
+
@stream = File.open(@path, "ab")
|
53
|
+
|
54
|
+
@expiration_time = Time.now + @interval
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
def emit(output)
|
60
|
+
rotate_log! if rotation_needed?
|
61
|
+
@stream.puts output
|
62
|
+
@stream.flush
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
|
69
|
+
def archive_destination
|
70
|
+
timestamp = @timestamp_formatter.format(File.ctime(@path))
|
71
|
+
dest = "#{path}.#{timestamp}"
|
72
|
+
return dest
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logbert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Lauber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Change your logging behaviors without mucking with your code!
|
14
14
|
email: constructible.truth@gmail.com
|
@@ -17,6 +17,9 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/logbert/formatters.rb
|
20
|
+
- lib/logbert/handlers/base_handler.rb
|
21
|
+
- lib/logbert/handlers/stream_handler.rb
|
22
|
+
- lib/logbert/handlers/time_rotator.rb
|
20
23
|
- lib/logbert/handlers.rb
|
21
24
|
- lib/logbert/levels.rb
|
22
25
|
- lib/logbert/logger.rb
|