ssherman-sysloglogger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,12 @@
1
+ == 2.0 / Unreleased
2
+
3
+ * SyslogLogger is now Syslog::Logger
4
+ * Syslog::Logger has a formatter like Logger
5
+
6
+ == 1.4.0 / 2007-05-08
7
+
8
+ * Split from rails_analyzer_tools.
9
+ * Added eh methods for compatibility with Logger.
10
+ * Added syslog-ng instructions. Patch by Tom Lianza.
11
+ * Fixed require in documentation. Reported by Gianni Jacklone.
12
+
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/analyzer_tools/syslog_logger.rb
6
+ lib/syslog/logger.rb
7
+ test/test_syslog_logger.rb
data/README.txt ADDED
@@ -0,0 +1,17 @@
1
+ = SyslogLogger
2
+
3
+ * http://github.com/seattlerb/sysloglogger
4
+
5
+ == Description
6
+
7
+ SyslogLogger is a Logger replacement that logs to syslog. It is almost
8
+ drop-in with a few caveats.
9
+
10
+ == About
11
+
12
+ See SyslogLogger
13
+
14
+ == Install
15
+
16
+ sudo gem install SyslogLogger
17
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'jeweler'
2
+ Jeweler::Tasks.new do |gem|
3
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
4
+ gem.name = "ssherman-sysloglogger"
5
+ gem.homepage = "http://github.com/ssherman/sysloglogger"
6
+ gem.summary = %Q{wrapper around syslog that's rails friendly}
7
+ gem.description = %Q{wrapper around syslog that's rails friendly. Forked and added ability to set options}
8
+ gem.email = "shane.sherman@gmail.com"
9
+ gem.authors = ["Erik Hodel", "Shane Sherman"]
10
+ # dependencies defined in Gemfile
11
+ end
12
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ require 'syslog_logger' # for compatibility
@@ -0,0 +1,194 @@
1
+ require 'syslog'
2
+ require 'logger'
3
+
4
+ ##
5
+ # Syslog::Logger is a Logger work-alike that logs via syslog instead of to a
6
+ # file. You can use Syslog::Logger to aggregate logs between multiple
7
+ # machines.
8
+ #
9
+ # By default, Syslog::Logger uses the program name 'ruby', but this can be
10
+ # changed via the first argument to Syslog::Logger.new.
11
+ #
12
+ # NOTE! You can only set the Syslog::Logger program name when you initialize
13
+ # Syslog::Logger for the first time. This is a limitation of the way
14
+ # Syslog::Logger uses syslog (and in some ways, a limitation of the way
15
+ # syslog(3) works). Attempts to change Syslog::Logger's program name after
16
+ # the first initialization will be ignored.
17
+ #
18
+ # === Example
19
+ #
20
+ # The following will log to syslogd on your local machine:
21
+ #
22
+ # require 'syslog/logger'
23
+ #
24
+ # log = Syslog::Logger.new 'my_program'
25
+ # log.info 'this line will be logged via syslog(3)'
26
+ #
27
+ # You may need to perform some syslog.conf setup first. For a BSD machine add
28
+ # the following lines to /etc/syslog.conf:
29
+ #
30
+ # !my_program
31
+ # *.* /var/log/my_program.log
32
+ #
33
+ # Then touch /var/log/my_program.log and signal syslogd with a HUP
34
+ # (killall -HUP syslogd, on FreeBSD).
35
+ #
36
+ # If you wish to have logs automatically roll over and archive, see the
37
+ # newsyslog.conf(5) and newsyslog(8) man pages.
38
+
39
+ class Syslog::Logger
40
+ # Default formatter for log messages.
41
+ class Formatter
42
+ def call severity, time, progname, msg
43
+ clean msg
44
+ end
45
+
46
+ private
47
+
48
+ ##
49
+ # Clean up messages so they're nice and pretty.
50
+
51
+ def clean message
52
+ message = message.to_s.strip
53
+ message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
54
+ return message
55
+ end
56
+ end
57
+
58
+ ##
59
+ # The version of Syslog::Logger you are using.
60
+
61
+ VERSION = '2.0'
62
+
63
+ ##
64
+ # Maps Logger warning types to syslog(3) warning types.
65
+ #
66
+ # Messages from ruby applications are not considered as critical as messages
67
+ # from other system daemons using syslog(3), so most messages are reduced by
68
+ # one level. For example, a fatal message for ruby's Logger is considered
69
+ # an error for syslog(3).
70
+
71
+ LEVEL_MAP = {
72
+ ::Logger::UNKNOWN => Syslog::LOG_ALERT,
73
+ ::Logger::FATAL => Syslog::LOG_ERR,
74
+ ::Logger::ERROR => Syslog::LOG_WARNING,
75
+ ::Logger::WARN => Syslog::LOG_NOTICE,
76
+ ::Logger::INFO => Syslog::LOG_INFO,
77
+ ::Logger::DEBUG => Syslog::LOG_DEBUG,
78
+ }
79
+
80
+ ##
81
+ # Returns the internal Syslog object that is initialized when the
82
+ # first instance is created.
83
+
84
+ def self.syslog
85
+ @@syslog
86
+ end
87
+
88
+ ##
89
+ # Specifies the internal Syslog object to be used.
90
+
91
+ def self.syslog= syslog
92
+ @@syslog = syslog
93
+ end
94
+
95
+ ##
96
+ # Builds a methods for level +meth+.
97
+
98
+ def self.make_methods meth
99
+ level = ::Logger.const_get(meth.upcase)
100
+ eval <<-EOM, nil, __FILE__, __LINE__ + 1
101
+ def #{meth}(message = nil, &block)
102
+ add(#{level}, message, &block)
103
+ end
104
+
105
+ def #{meth}?
106
+ @level <= #{level}
107
+ end
108
+ EOM
109
+ end
110
+
111
+ ##
112
+ # :method: unknown
113
+ #
114
+ # Logs a +message+ at the unknown (syslog alert) log level, or logs the
115
+ # message returned from the block.
116
+
117
+ ##
118
+ # :method: fatal
119
+ #
120
+ # Logs a +message+ at the fatal (syslog err) log level, or logs the message
121
+ # returned from the block.
122
+
123
+ ##
124
+ # :method: error
125
+ #
126
+ # Logs a +message+ at the error (syslog warning) log level, or logs the
127
+ # message returned from the block.
128
+
129
+ ##
130
+ # :method: warn
131
+ #
132
+ # Logs a +message+ at the warn (syslog notice) log level, or logs the
133
+ # message returned from the block.
134
+
135
+ ##
136
+ # :method: info
137
+ #
138
+ # Logs a +message+ at the info (syslog info) log level, or logs the message
139
+ # returned from the block.
140
+
141
+ ##
142
+ # :method: debug
143
+ #
144
+ # Logs a +message+ at the debug (syslog debug) log level, or logs the
145
+ # message returned from the block.
146
+
147
+ Logger::Severity::constants.each do |severity|
148
+ make_methods severity.downcase
149
+ end
150
+
151
+ ##
152
+ # Log level for Logger compatibility.
153
+
154
+ attr_accessor :level
155
+
156
+ # Logging formatter, as a +Proc+ that will take four arguments and
157
+ # return the formatted message. The arguments are:
158
+ #
159
+ # +severity+:: The Severity of the log message.
160
+ # +time+:: A Time instance representing when the message was logged.
161
+ # +progname+:: The #progname configured, or passed to the logger method.
162
+ # +msg+:: The _Object_ the user passed to the log message; not necessarily a
163
+ # String.
164
+ #
165
+ # The block should return an Object that can be written to the logging
166
+ # device via +write+. The default formatter is used when no formatter is
167
+ # set.
168
+ attr_accessor :formatter
169
+
170
+ ##
171
+ # Fills in variables for Logger compatibility. If this is the first
172
+ # instance of Syslog::Logger, +program_name+ may be set to change the logged
173
+ # program name.
174
+ #
175
+ # Due to the way syslog works, only one program name may be chosen.
176
+
177
+ def initialize(program_name = 'ruby', options = nil, facility = nil)
178
+ @level = ::Logger::DEBUG
179
+ @formatter = Formatter.new
180
+
181
+ @@syslog ||= Syslog.open(program_name, options, facility)
182
+ end
183
+
184
+ ##
185
+ # Almost duplicates Logger#add. +progname+ is ignored.
186
+
187
+ def add severity, message = nil, progname = nil, &block
188
+ severity ||= ::Logger::UNKNOWN
189
+ @level <= severity and
190
+ @@syslog.log LEVEL_MAP[severity], '%s', formatter.call(severity, Time.now, progname, (message || block.call))
191
+ true
192
+ end
193
+ end
194
+
@@ -0,0 +1 @@
1
+ require 'syslog/logger'
@@ -0,0 +1,521 @@
1
+ require 'test/unit'
2
+ require 'tempfile'
3
+ begin
4
+ require 'syslog/logger'
5
+ rescue LoadError
6
+ # skip. see the bottom of this file.
7
+ end
8
+
9
+ # These tests ensure Syslog::Logger works like Logger
10
+
11
+ class TestSyslogRootLogger < Test::Unit::TestCase
12
+
13
+ module MockSyslog
14
+ LEVEL_LABEL_MAP = {}
15
+
16
+ class << self
17
+
18
+ @line = nil
19
+
20
+ %w[ALERT ERR WARNING NOTICE INFO DEBUG].each do |name|
21
+ level = Syslog.const_get("LOG_#{name}")
22
+ LEVEL_LABEL_MAP[level] = name
23
+
24
+ eval <<-EOM
25
+ def #{name.downcase}(format, *args)
26
+ log(#{level}, format, *args)
27
+ end
28
+ EOM
29
+ end
30
+
31
+ def log(level, format, *args)
32
+ @line = "#{LEVEL_LABEL_MAP[level]} - #{format % args}"
33
+ end
34
+
35
+ attr_reader :line
36
+ attr_reader :program_name
37
+
38
+ def open(program_name)
39
+ @program_name = program_name
40
+ end
41
+
42
+ def reset
43
+ @line = ''
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ Syslog::Logger.syslog = MockSyslog
50
+
51
+ LEVEL_LABEL_MAP = {
52
+ Logger::DEBUG => 'DEBUG',
53
+ Logger::INFO => 'INFO',
54
+ Logger::WARN => 'WARN',
55
+ Logger::ERROR => 'ERROR',
56
+ Logger::FATAL => 'FATAL',
57
+ Logger::UNKNOWN => 'ANY',
58
+ }
59
+
60
+ def setup
61
+ @logger = Logger.new(nil)
62
+ end
63
+
64
+ class Log
65
+ attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
66
+ def initialize(line)
67
+ @line = line
68
+ /\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ @line
69
+ @label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
70
+ end
71
+ end
72
+
73
+ def log_add(severity, msg, progname = nil, &block)
74
+ log(:add, severity, msg, progname, &block)
75
+ end
76
+
77
+ def log(msg_id, *arg, &block)
78
+ Log.new(log_raw(msg_id, *arg, &block))
79
+ end
80
+
81
+ def log_raw(msg_id, *arg, &block)
82
+ logdev = Tempfile.new(File.basename(__FILE__) + '.log')
83
+ @logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
84
+ assert_equal true, @logger.__send__(msg_id, *arg, &block)
85
+ logdev.open
86
+ msg = logdev.read
87
+ logdev.close
88
+ msg
89
+ end
90
+
91
+ def test_initialize
92
+ assert_equal Logger::DEBUG, @logger.level
93
+ end
94
+
95
+ def test_custom_formatter
96
+ @logger.formatter = Class.new {
97
+ def call severity, time, progname, msg
98
+ "hi mom!"
99
+ end
100
+ }.new
101
+
102
+ assert_match(/hi mom!/, log_raw(:fatal, 'fatal level message'))
103
+ end
104
+
105
+ def test_add
106
+ msg = log_add nil, 'unknown level message' # nil == unknown
107
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
108
+
109
+ msg = log_add Logger::FATAL, 'fatal level message'
110
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
111
+
112
+ msg = log_add Logger::ERROR, 'error level message'
113
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
114
+
115
+ msg = log_add Logger::WARN, 'warn level message'
116
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
117
+
118
+ msg = log_add Logger::INFO, 'info level message'
119
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
120
+
121
+ msg = log_add Logger::DEBUG, 'debug level message'
122
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
123
+ end
124
+
125
+ def test_add_level_unknown
126
+ @logger.level = Logger::UNKNOWN
127
+
128
+ msg = log_add nil, 'unknown level message' # nil == unknown
129
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
130
+
131
+ msg = log_add Logger::FATAL, 'fatal level message'
132
+ assert_equal '', msg.line
133
+
134
+ msg = log_add Logger::ERROR, 'error level message'
135
+ assert_equal '', msg.line
136
+
137
+ msg = log_add Logger::WARN, 'warn level message'
138
+ assert_equal '', msg.line
139
+
140
+ msg = log_add Logger::INFO, 'info level message'
141
+ assert_equal '', msg.line
142
+
143
+ msg = log_add Logger::DEBUG, 'debug level message'
144
+ assert_equal '', msg.line
145
+ end
146
+
147
+ def test_add_level_fatal
148
+ @logger.level = Logger::FATAL
149
+
150
+ msg = log_add nil, 'unknown level message' # nil == unknown
151
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
152
+
153
+ msg = log_add Logger::FATAL, 'fatal level message'
154
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
155
+
156
+ msg = log_add Logger::ERROR, 'error level message'
157
+ assert_equal '', msg.line
158
+
159
+ msg = log_add Logger::WARN, 'warn level message'
160
+ assert_equal '', msg.line
161
+
162
+ msg = log_add Logger::INFO, 'info level message'
163
+ assert_equal '', msg.line
164
+
165
+ msg = log_add Logger::DEBUG, 'debug level message'
166
+ assert_equal '', msg.line
167
+ end
168
+
169
+ def test_add_level_error
170
+ @logger.level = Logger::ERROR
171
+
172
+ msg = log_add nil, 'unknown level message' # nil == unknown
173
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
174
+
175
+ msg = log_add Logger::FATAL, 'fatal level message'
176
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
177
+
178
+ msg = log_add Logger::ERROR, 'error level message'
179
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
180
+
181
+ msg = log_add Logger::WARN, 'warn level message'
182
+ assert_equal '', msg.line
183
+
184
+ msg = log_add Logger::INFO, 'info level message'
185
+ assert_equal '', msg.line
186
+
187
+ msg = log_add Logger::DEBUG, 'debug level message'
188
+ assert_equal '', msg.line
189
+ end
190
+
191
+ def test_add_level_warn
192
+ @logger.level = Logger::WARN
193
+
194
+ msg = log_add nil, 'unknown level message' # nil == unknown
195
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
196
+
197
+ msg = log_add Logger::FATAL, 'fatal level message'
198
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
199
+
200
+ msg = log_add Logger::ERROR, 'error level message'
201
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
202
+
203
+ msg = log_add Logger::WARN, 'warn level message'
204
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
205
+
206
+ msg = log_add Logger::INFO, 'info level message'
207
+ assert_equal '', msg.line
208
+
209
+ msg = log_add Logger::DEBUG, 'debug level message'
210
+ assert_equal '', msg.line
211
+ end
212
+
213
+ def test_add_level_info
214
+ @logger.level = Logger::INFO
215
+
216
+ msg = log_add nil, 'unknown level message' # nil == unknown
217
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
218
+
219
+ msg = log_add Logger::FATAL, 'fatal level message'
220
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
221
+
222
+ msg = log_add Logger::ERROR, 'error level message'
223
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
224
+
225
+ msg = log_add Logger::WARN, 'warn level message'
226
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
227
+
228
+ msg = log_add Logger::INFO, 'info level message'
229
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
230
+
231
+ msg = log_add Logger::DEBUG, 'debug level message'
232
+ assert_equal '', msg.line
233
+ end
234
+
235
+ def test_add_level_debug
236
+ @logger.level = Logger::DEBUG
237
+
238
+ msg = log_add nil, 'unknown level message' # nil == unknown
239
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
240
+
241
+ msg = log_add Logger::FATAL, 'fatal level message'
242
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
243
+
244
+ msg = log_add Logger::ERROR, 'error level message'
245
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
246
+
247
+ msg = log_add Logger::WARN, 'warn level message'
248
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
249
+
250
+ msg = log_add Logger::INFO, 'info level message'
251
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
252
+
253
+ msg = log_add Logger::DEBUG, 'debug level message'
254
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
255
+ end
256
+
257
+ def test_unknown
258
+ msg = log :unknown, 'unknown level message'
259
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
260
+
261
+ @logger.level = Logger::UNKNOWN
262
+ msg = log :unknown, 'unknown level message'
263
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
264
+
265
+ @logger.level = Logger::FATAL
266
+ msg = log :unknown, 'unknown level message'
267
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
268
+
269
+ @logger.level = Logger::ERROR
270
+ msg = log :unknown, 'unknown level message'
271
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
272
+
273
+ @logger.level = Logger::WARN
274
+ msg = log :unknown, 'unknown level message'
275
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
276
+
277
+ @logger.level = Logger::INFO
278
+ msg = log :unknown, 'unknown level message'
279
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
280
+
281
+ @logger.level = Logger::DEBUG
282
+ msg = log :unknown, 'unknown level message'
283
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
284
+ end
285
+
286
+ def test_fatal
287
+ msg = log :fatal, 'fatal level message'
288
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
289
+
290
+ @logger.level = Logger::UNKNOWN
291
+ msg = log :fatal, 'fatal level message'
292
+ assert_equal '', msg.line
293
+
294
+ @logger.level = Logger::FATAL
295
+ msg = log :fatal, 'fatal level message'
296
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
297
+
298
+ @logger.level = Logger::ERROR
299
+ msg = log :fatal, 'fatal level message'
300
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
301
+
302
+ @logger.level = Logger::WARN
303
+ msg = log :fatal, 'fatal level message'
304
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
305
+
306
+ @logger.level = Logger::INFO
307
+ msg = log :fatal, 'fatal level message'
308
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
309
+
310
+ @logger.level = Logger::DEBUG
311
+ msg = log :fatal, 'fatal level message'
312
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
313
+ end
314
+
315
+ def test_fatal_eh
316
+ @logger.level = Logger::FATAL
317
+ assert_equal true, @logger.fatal?
318
+
319
+ @logger.level = Logger::UNKNOWN
320
+ assert_equal false, @logger.fatal?
321
+ end
322
+
323
+ def test_error
324
+ msg = log :error, 'error level message'
325
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
326
+
327
+ @logger.level = Logger::UNKNOWN
328
+ msg = log :error, 'error level message'
329
+ assert_equal '', msg.line
330
+
331
+ @logger.level = Logger::FATAL
332
+ msg = log :error, 'error level message'
333
+ assert_equal '', msg.line
334
+
335
+ @logger.level = Logger::ERROR
336
+ msg = log :error, 'error level message'
337
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
338
+
339
+ @logger.level = Logger::WARN
340
+ msg = log :error, 'error level message'
341
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
342
+
343
+ @logger.level = Logger::INFO
344
+ msg = log :error, 'error level message'
345
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
346
+
347
+ @logger.level = Logger::DEBUG
348
+ msg = log :error, 'error level message'
349
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
350
+ end
351
+
352
+ def test_error_eh
353
+ @logger.level = Logger::ERROR
354
+ assert_equal true, @logger.error?
355
+
356
+ @logger.level = Logger::FATAL
357
+ assert_equal false, @logger.error?
358
+ end
359
+
360
+ def test_warn
361
+ msg = log :warn, 'warn level message'
362
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
363
+
364
+ @logger.level = Logger::UNKNOWN
365
+ msg = log :warn, 'warn level message'
366
+ assert_equal '', msg.line
367
+
368
+ @logger.level = Logger::FATAL
369
+ msg = log :warn, 'warn level message'
370
+ assert_equal '', msg.line
371
+
372
+ @logger.level = Logger::ERROR
373
+ msg = log :warn, 'warn level message'
374
+ assert_equal '', msg.line
375
+
376
+ @logger.level = Logger::WARN
377
+ msg = log :warn, 'warn level message'
378
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
379
+
380
+ @logger.level = Logger::INFO
381
+ msg = log :warn, 'warn level message'
382
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
383
+
384
+ @logger.level = Logger::DEBUG
385
+ msg = log :warn, 'warn level message'
386
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
387
+ end
388
+
389
+ def test_warn_eh
390
+ @logger.level = Logger::WARN
391
+ assert_equal true, @logger.warn?
392
+
393
+ @logger.level = Logger::ERROR
394
+ assert_equal false, @logger.warn?
395
+ end
396
+
397
+ def test_info
398
+ msg = log :info, 'info level message'
399
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
400
+
401
+ @logger.level = Logger::UNKNOWN
402
+ msg = log :info, 'info level message'
403
+ assert_equal '', msg.line
404
+
405
+ @logger.level = Logger::FATAL
406
+ msg = log :info, 'info level message'
407
+ assert_equal '', msg.line
408
+
409
+ @logger.level = Logger::ERROR
410
+ msg = log :info, 'info level message'
411
+ assert_equal '', msg.line
412
+
413
+ @logger.level = Logger::WARN
414
+ msg = log :info, 'info level message'
415
+ assert_equal '', msg.line
416
+
417
+ @logger.level = Logger::INFO
418
+ msg = log :info, 'info level message'
419
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
420
+
421
+ @logger.level = Logger::DEBUG
422
+ msg = log :info, 'info level message'
423
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
424
+ end
425
+
426
+ def test_info_eh
427
+ @logger.level = Logger::INFO
428
+ assert_equal true, @logger.info?
429
+
430
+ @logger.level = Logger::WARN
431
+ assert_equal false, @logger.info?
432
+ end
433
+
434
+ def test_debug
435
+ msg = log :debug, 'debug level message'
436
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
437
+
438
+ @logger.level = Logger::UNKNOWN
439
+ msg = log :debug, 'debug level message'
440
+ assert_equal '', msg.line
441
+
442
+ @logger.level = Logger::FATAL
443
+ msg = log :debug, 'debug level message'
444
+ assert_equal '', msg.line
445
+
446
+ @logger.level = Logger::ERROR
447
+ msg = log :debug, 'debug level message'
448
+ assert_equal '', msg.line
449
+
450
+ @logger.level = Logger::WARN
451
+ msg = log :debug, 'debug level message'
452
+ assert_equal '', msg.line
453
+
454
+ @logger.level = Logger::INFO
455
+ msg = log :debug, 'debug level message'
456
+ assert_equal '', msg.line
457
+
458
+ @logger.level = Logger::DEBUG
459
+ msg = log :debug, 'debug level message'
460
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
461
+ end
462
+
463
+ def test_debug_eh
464
+ @logger.level = Logger::DEBUG
465
+ assert_equal true, @logger.debug?
466
+
467
+ @logger.level = Logger::INFO
468
+ assert_equal false, @logger.debug?
469
+ end
470
+
471
+ end if defined?(Syslog)
472
+
473
+ class TestSyslogLogger < TestSyslogRootLogger
474
+
475
+ def setup
476
+ super
477
+ @logger = Syslog::Logger.new
478
+ end
479
+
480
+ SEVERITY_MAP = {}.tap { |map|
481
+ level2severity = Syslog::Logger::LEVEL_MAP.invert
482
+
483
+ MockSyslog::LEVEL_LABEL_MAP.each { |level, name|
484
+ map[name] = TestSyslogRootLogger::LEVEL_LABEL_MAP[level2severity[level]]
485
+ }
486
+ }
487
+
488
+ class Log
489
+ attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
490
+ def initialize(line)
491
+ @line = line
492
+ return unless /\A(\w+) - (.*)\Z/ =~ @line
493
+ severity, @msg = $1, $2
494
+ @severity = SEVERITY_MAP[severity]
495
+ end
496
+ end
497
+
498
+ def log_add(severity, msg, progname = nil, &block)
499
+ log(:add, severity, msg, progname, &block)
500
+ end
501
+
502
+ def log(msg_id, *arg, &block)
503
+ Log.new(log_raw(msg_id, *arg, &block))
504
+ end
505
+
506
+ def log_raw(msg_id, *arg, &block)
507
+ assert_equal true, @logger.__send__(msg_id, *arg, &block)
508
+ msg = MockSyslog.line
509
+ MockSyslog.reset
510
+ return msg
511
+ end
512
+
513
+ def test_unknown_eh
514
+ @logger.level = Logger::UNKNOWN
515
+ assert_equal true, @logger.unknown?
516
+
517
+ @logger.level = Logger::UNKNOWN + 1
518
+ assert_equal false, @logger.unknown?
519
+ end
520
+
521
+ end if defined?(Syslog)
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssherman-sysloglogger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erik Hodel
9
+ - Shane Sherman
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-12-07 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: wrapper around syslog that's rails friendly. Forked and added ability
16
+ to set options
17
+ email: shane.sherman@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files:
21
+ - README.txt
22
+ files:
23
+ - History.txt
24
+ - Manifest.txt
25
+ - README.txt
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/analyzer_tools/syslog_logger.rb
29
+ - lib/syslog/logger.rb
30
+ - lib/sysloglogger.rb
31
+ - test/test_syslog_logger.rb
32
+ homepage: http://github.com/ssherman/sysloglogger
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: wrapper around syslog that's rails friendly
56
+ test_files: []