fluent-logger 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,121 +0,0 @@
1
- #
2
- # Fluent
3
- #
4
- # Copyright (C) 2011 FURUHASHI Sadayuki
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- module Fluent
19
- module Logger
20
-
21
-
22
- class FluentLogger < LoggerBase
23
- BUFFER_LIMIT = 8*1024*1024
24
-
25
- def initialize(tag, host, port=24224)
26
- super()
27
- require 'msgpack'
28
- require 'socket'
29
- require 'monitor'
30
- require 'logger'
31
- @mon = Monitor.new
32
-
33
- @tag = tag
34
-
35
- @pending = nil
36
- @host = host
37
- @port = port
38
-
39
- @limit = BUFFER_LIMIT
40
- @logger = ::Logger.new(STDERR)
41
-
42
- begin
43
- connect!
44
- rescue
45
- @logger.error "Failed to connect fluentd: #{$!}"
46
- @logger.error "Connection will be retried."
47
- end
48
-
49
- FluentLogger.close_on_exit(self)
50
- end
51
-
52
- attr_accessor :limit, :logger
53
-
54
- def post(tag, map)
55
- time = Time.now.to_i
56
- write ["#{@tag}.#{tag}", time, map]
57
- end
58
-
59
- def close
60
- if @pending
61
- @logger.error("FluentLogger: Can't send logs to #{@host}:#{@port}")
62
- end
63
- @con.close if @con
64
- @con = nil
65
- @pending = nil
66
- self
67
- end
68
-
69
- private
70
- def write(msg)
71
- data = msg.to_msgpack
72
- @mon.synchronize {
73
- if @pending
74
- @pending << data
75
- data = @pending
76
- end
77
- begin
78
- unless @con
79
- connect!
80
- end
81
- while true
82
- n = @con.syswrite(data)
83
- if n >= data.bytesize
84
- break
85
- end
86
- data = data[n..-1]
87
- end
88
- @pending = nil
89
- rescue
90
- if @pending
91
- if @pending.bytesize > @limit
92
- @logger.error("FluentLogger: Can't send logs to #{@host}:#{@port}")
93
- @pending = nil
94
- end
95
- else
96
- @pending = data
97
- end
98
- @con.close if @con
99
- @con = nil
100
- end
101
- }
102
- end
103
-
104
- def connect!
105
- @con = TCPSocket.new(@host, @port)
106
- end
107
-
108
- def self.close_on_exit(logger)
109
- ObjectSpace.define_finalizer(logger, self.finalizer(logger))
110
- end
111
-
112
- def self.finalizer(logger)
113
- proc {
114
- logger.close
115
- }
116
- end
117
- end
118
-
119
-
120
- end
121
- end
@@ -1,127 +0,0 @@
1
- #
2
- # Fluent
3
- #
4
- # Copyright (C) 2011 FURUHASHI Sadayuki
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- module Fluent
19
- module Logger
20
-
21
-
22
- class SyslogLogger < TextLogger
23
- def initialize(ident=$0, level=:info)
24
- super()
25
- require 'syslog'
26
-
27
- @ident = ident
28
- self.level = level
29
- #self.facility = facility
30
- Syslog.open(@ident)
31
- end
32
-
33
- attr_reader :level
34
-
35
- def level=(level)
36
- level = level.to_sym if level.is_a?(String)
37
- @level = case level
38
- when :emerg, Syslog::LOG_EMERG
39
- Syslog::LOG_EMERG
40
- when :alert, Syslog::LOG_ALERT
41
- Syslog::LOG_ALERT
42
- when :crit, Syslog::LOG_CRIT
43
- Syslog::LOG_CRIT
44
- when :err, :error, Syslog::LOG_ERR
45
- Syslog::LOG_ERR
46
- when :warning, :warn, Syslog::LOG_WARNING
47
- Syslog::LOG_WARNING
48
- when :notice, Syslog::LOG_NOTICE
49
- Syslog::LOG_NOTICE
50
- when :info, Syslog::LOG_INFO
51
- Syslog::LOG_INFO
52
- when :debug, Syslog::LOG_DEBUG
53
- Syslog::LOG_DEBUG
54
- else
55
- raise "Unknown level description #{level.inspect}"
56
- end
57
- end
58
-
59
- #def facility=(facility)
60
- # facility = facility.to_sym if facility.is_a?(String)
61
- # @facility = case facility
62
- # when :auth, Syslog::LOG_AUTH
63
- # Syslog::LOG_AUTH
64
- # when :authpriv, Syslog::LOG_AUTHPRIV
65
- # Syslog::LOG_AUTHPRIV
66
- # when :console, Syslog::LOG_CONSOLE
67
- # Syslog::LOG_CONSOLE
68
- # when :cron, Syslog::LOG_CRON
69
- # Syslog::LOG_CRON
70
- # when :daemon, Syslog::LOG_DAEMON
71
- # Syslog::LOG_DAEMON
72
- # when :ftp, Syslog::LOG_FTP
73
- # Syslog::LOG_FTP
74
- # when :kern, Syslog::LOG_KERN
75
- # Syslog::LOG_KERN
76
- # when :lpr, Syslog::LOG_LPR
77
- # Syslog::LOG_LPR
78
- # when :mail, Syslog::LOG_MAIL
79
- # Syslog::LOG_MAIL
80
- # when :news, Syslog::LOG_NEWS
81
- # Syslog::LOG_NEWS
82
- # when :ntp, Syslog::LOG_NTP
83
- # Syslog::LOG_NTP
84
- # when :security, Syslog::LOG_SECURITY
85
- # Syslog::LOG_SECURITY
86
- # when :syslog, Syslog::LOG_SYSLOG
87
- # Syslog::LOG_SYSLOG
88
- # when :user, Syslog::LOG_USER
89
- # Syslog::LOG_USER
90
- # when :uucp, Syslog::LOG_UUCP
91
- # Syslog::LOG_UUCP
92
- # when :local0, Syslog::LOG_LOCAL0
93
- # Syslog::LOG_LOCAL0
94
- # when :local1, Syslog::LOG_LOCAL1
95
- # Syslog::LOG_LOCAL1
96
- # when :local2, Syslog::LOG_LOCAL2
97
- # Syslog::LOG_LOCAL2
98
- # when :local3, Syslog::LOG_LOCAL3
99
- # Syslog::LOG_LOCAL3
100
- # when :local4, Syslog::LOG_LOCAL4
101
- # Syslog::LOG_LOCAL4
102
- # when :local5, Syslog::LOG_LOCAL5
103
- # Syslog::LOG_LOCAL5
104
- # when :local6, Syslog::LOG_LOCAL6
105
- # Syslog::LOG_LOCAL6
106
- # when :local7, Syslog::LOG_LOCAL7
107
- # Syslog::LOG_LOCAL7
108
- # else
109
- # raise "Unknown facility description #{facility.inspect}"
110
- # end
111
- # Syslog.reopen(@ident, Syslog::LOG_PID|Syslog::LOG_CONS, @facility)
112
- # facility
113
- #end
114
-
115
- def post_text(text)
116
- Syslog.log(@level, "%s", text)
117
- end
118
-
119
- def close
120
- Syslog.close
121
- self
122
- end
123
- end
124
-
125
-
126
- end
127
- end
@@ -1,183 +0,0 @@
1
- require File.dirname(__FILE__)+'/test_helper'
2
-
3
- class EventTest < Test::Unit::TestCase
4
- it 'keys' do
5
- g = Fluent::Logger::TestLogger.new
6
-
7
- assert_nothing_raised do
8
- e1 = g.create_event('e1', :k1)
9
- e1.k1('v1')
10
- end
11
-
12
- assert_raise(NoMethodError) do
13
- e2 = g.create_event('e2')
14
- e2.k1('v1')
15
- end
16
-
17
- assert_raise(NoMethodError) do
18
- e3 = g.create_event('e3', :k1)
19
- e3.k2('v2')
20
- end
21
- end
22
-
23
- it 'set_value' do
24
- g = Fluent::Logger::TestLogger.new
25
-
26
- e1 = g.create_event('e1', :k1)
27
- e1.k1('v1').post!
28
- assert_equal g.queue.last, {:k1=>'v1'}
29
-
30
- e2 = g.create_event('e2', :k1, :k2)
31
- e2.k1('v1').k2('v2').post!
32
- assert_equal g.queue.last, {:k1=>'v1', :k2=>'v2'}
33
-
34
- e3 = g.create_event('e3', :k1, :k2)
35
- e3.k1('v1').post!
36
- assert_equal g.queue.last, {:k1=>'v1'}
37
- end
38
-
39
- it 'default_value' do
40
- g = Fluent::Logger::TestLogger.new
41
-
42
- e1 = g.create_event('e1', :k1)
43
- e1.post!
44
- assert_equal g.queue.last, {}
45
-
46
- e2 = g.create_event('e2', :k1=>'v1')
47
- e2.post!
48
- assert_equal g.queue.last, {:k1=>'v1'}
49
-
50
- e3 = g.create_event('e3', :k1=>'v1', :k2=>'v2')
51
- e3.post!
52
- assert_equal g.queue.last, {:k1=>'v1', :k2=>'v2'}
53
-
54
- e4 = g.create_event('e4', :k1=>'v1', :k2=>'v2')
55
- e4.k2('v3').post!
56
- assert_equal g.queue.last, {:k1=>'v1', :k2=>'v3'}
57
- end
58
-
59
- it 'modify' do
60
- g = Fluent::Logger::TestLogger.new
61
-
62
- e1 = g.create_event('e1', :k1)
63
- e1.k1!('v1')
64
- e1.post!
65
- assert_equal g.queue.last, {:k1=>'v1'}
66
-
67
- e1.k1!('v2')
68
- e1.post!
69
- assert_equal g.queue.last, {:k1=>'v2'}
70
-
71
- e2 = g.create_event('e2', :k1=>'v1')
72
- e2.k1!('v2')
73
- e2.post!
74
- assert_equal g.queue.last, {:k1=>'v2'}
75
- end
76
-
77
- it 'no_modify' do
78
- g = Fluent::Logger::TestLogger.new
79
-
80
- e1 = g.create_event('e1', :k1)
81
- e1.k1('v1')
82
- e1.post!
83
- assert_equal g.queue.last, {}
84
-
85
- e1.k1('v2')
86
- e1.post!
87
- assert_equal g.queue.last, {}
88
-
89
- e2 = g.create_event('e1', :k1=>'v1')
90
- e2.k1('v2')
91
- e2.post!
92
- assert_equal g.queue.last, {:k1=>'v1'}
93
- end
94
-
95
- it 'with_map' do
96
- g = Fluent::Logger::TestLogger.new
97
-
98
- e1 = g.create_event('e1')
99
- e1.with(:k1=>'v1').post!
100
- assert_equal g.queue.last, {:k1=>'v1'}
101
-
102
- e2 = g.create_event('e2', :k1=>'v1')
103
- e2.with(:k1=>'v2').post!
104
- assert_equal g.queue.last, {:k1=>'v2'}
105
-
106
- e3 = g.create_event('e3', :k1=>'v1')
107
- e3.with(:k1=>'v2', :k2=>'v3').post!
108
- assert_equal g.queue.last, {:k1=>'v2', :k2=>'v3'}
109
- end
110
-
111
- it 'with_map_modify' do
112
- g = Fluent::Logger::TestLogger.new
113
-
114
- e1 = g.create_event('e2', :k1)
115
- e1.with!(:k1=>'v1')
116
- e1.post!
117
- assert_equal g.queue.last, {:k1=>'v1'}
118
-
119
- e1.with!(:k1=>'v2')
120
- e1.post!
121
- assert_equal g.queue.last, {:k1=>'v2'}
122
-
123
- e2 = g.create_event('e2')
124
- e2.with!(:k1=>'v1', :k2=>'v2')
125
- e2.post!
126
- assert_equal g.queue.last, {:k1=>'v1', :k2=>'v2'}
127
-
128
- e2.with!(:k1=>'v3')
129
- e2.post!
130
- assert_equal g.queue.last, {:k1=>'v3', :k2=>'v2'}
131
- end
132
-
133
- it 'with_event_keys' do
134
- g = Fluent::Logger::TestLogger.new
135
-
136
- assert_nothing_raised do
137
- e1 = g.create_event('e1', :k1)
138
- e2 = g.create_event('e2', :k2)
139
- e3 = e1.with(e2)
140
- e3.k1('v1').k2('v2')
141
- end
142
-
143
- assert_nothing_raised do
144
- e1 = g.create_event('e1', :k1)
145
- e2 = g.create_event('e2', :k1, :k2)
146
- e3 = e1.with(e2)
147
- e3.k1('v1').k2('v2')
148
- end
149
-
150
- assert_nothing_raised do
151
- e1 = g.create_event('e1', :k1)
152
- e2 = g.create_event('e2', :k2)
153
- e1.with!(e2)
154
- e1.k1('v1').k2('v2')
155
- end
156
-
157
- assert_raise(NoMethodError) do
158
- e1 = g.create_event('e1', :k1)
159
- e2 = g.create_event('e2', :k2)
160
- e3 = e1.with(e2)
161
- e1.k2('v1')
162
- end
163
-
164
- assert_raise(NoMethodError) do
165
- e1 = g.create_event('e1', :k1)
166
- e2 = g.create_event('e2', :k2)
167
- e1.with!(e2)
168
- e2.k1('v1')
169
- end
170
- end
171
-
172
- it 'tag' do
173
- g = Fluent::Logger::TestLogger.new
174
-
175
- e1 = g.create_event('e1')
176
- e1.post!
177
- assert_equal g.queue.last.tag, 'e1'
178
-
179
- e1.post!('e2')
180
- assert_equal g.queue.last.tag, 'e2'
181
- end
182
- end
183
-
@@ -1,34 +0,0 @@
1
- require File.dirname(__FILE__)+'/test_helper'
2
-
3
- class InstanceTest < Test::Unit::TestCase
4
- it 'default' do
5
- g1 = Fluent::Logger::TestLogger.open
6
- assert_equal g1.object_id, Fluent::Logger.default.object_id
7
-
8
- assert_equal false, g1.closed?
9
-
10
- g2 = Fluent::Logger::TestLogger.open
11
- assert_equal g2.object_id, Fluent::Logger.default.object_id
12
-
13
- assert_equal true, g1.closed?
14
-
15
- g3 = Fluent::Logger::TestLogger.new
16
- assert_equal g2.object_id, Fluent::Logger.default.object_id
17
-
18
- assert_equal false, g2.closed?
19
- Fluent::Logger.close
20
- assert_equal true, g2.closed?
21
- end
22
-
23
- it 'post' do
24
- g1 = Fluent::Logger::TestLogger.new
25
-
26
- g1.post :k1=>'v1'
27
- assert_equal g1.queue.last, {:k1=>'v1'}
28
-
29
- g2 = Fluent::Logger::TestLogger.open
30
-
31
- Fluent::Logger.post :k2=>'v2'
32
- assert_equal g2.queue.last, {:k2=>'v2'}
33
- end
34
- end