ashleym1972-syslog_logger 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,111 @@
1
+ = SyslogLogger
2
+
3
+ http://github.com/ashleym1972/sysloglogger/tree
4
+
5
+ == DESCRIPTION:
6
+
7
+ SyslogLogger is a Logger replacement that logs to syslog. It is almost drop-in
8
+ with a few caveats. You can add SyslogLogger to your Rails production
9
+ environment to aggregate logs between multiple machines.
10
+
11
+ NOTE! You can only set the SyslogLogger program name and facility when you initialize
12
+ SyslogLogger for the first time. This is a limitation of the way SyslogLogger
13
+ uses syslog (and in some ways, a limitation of the way syslog(3) works).
14
+ Attempts to change SyslogLogger's program name or facility after the first initialization
15
+ will be ignored.
16
+
17
+ This particular SyslogLogger improves the original by correctly mapping Rails log severities to
18
+ the Syslog counterparts. It also adds the ability to select a syslog facility other than "user."
19
+
20
+ == SYNOPSIS:
21
+
22
+ === config/environment.rb
23
+
24
+ config.gem "ashleym1972-SyslogLogger", :source=>'http://gems.github.com', :lib=>'syslog_logger'
25
+
26
+ === config/environments/production.rb
27
+
28
+ (Only this environment, since you probably only want to enable it for Production, and not Test or Development.)
29
+
30
+ require 'syslog_logger'
31
+ RAILS_DEFAULT_LOGGER = SyslogLogger.new
32
+
33
+ By default, SyslogLogger uses the program name 'rails' and the facility 'user', but this can be
34
+ changed via the arguments to SyslogLogger.new:
35
+
36
+ RAILS_DEFAULT_LOGGER = SyslogLogger.new('mygreatapp', Syslog::LOG_LOCAL7)
37
+
38
+ === BSD syslog setup
39
+
40
+ ==== /etc/syslog.conf
41
+
42
+ Add the following lines:
43
+
44
+ !rails
45
+ *.* /var/log/production.log
46
+
47
+ Then touch /var/log/production.log and signal syslogd with a HUP
48
+ (killall -HUP syslogd, on FreeBSD).
49
+
50
+ ==== /etc/newsyslog.conf
51
+
52
+ Add the following line:
53
+
54
+ /var/log/production.log 640 7 * @T00 Z
55
+
56
+ This creates a log file that is rotated every day at midnight, gzip'd, then
57
+ kept for 7 days. Consult newsyslog.conf(5) for more details.
58
+
59
+ === syslog-ng setup
60
+
61
+ ==== syslog-ng.conf
62
+
63
+ A basic setup:
64
+
65
+ destination rails_log { file("/var/log/production.log"); };
66
+ filter f_rails { program("rails.*"); };
67
+ log { source(src); filter(f_rails); destination(rails_log); };
68
+
69
+ A more advanced setup with formatting, etc:
70
+
71
+ destination rails { file("/var/log/rails_apps" owner("rails") group("rails") perm(0664) template("$DATE $HOST $PROGRAM [$LEVEL] $MSGONLY\n") template_escape(no) ); };
72
+ filter f_rails { (program("railsappone") or program("railsapptwo")) and not match("SQL ") and not match("SELECT") and not match("INSERT INTO") and not match("UPDATE"); };
73
+ log { source(src); filter(f_rails); destination(rails); };
74
+
75
+ === Starting
76
+
77
+ Now restart your Rails app. Your production logs should now be showing up in
78
+ /var/log/production.log. If you have mulitple machines, you can log them all
79
+ to a central machine with remote syslog logging for analysis. Consult your
80
+ syslogd(8) manpage for further details.
81
+
82
+ == INSTALL:
83
+
84
+ $ gem sources -a http://gems.github.com (you only have to do this once)
85
+ $ sudo gem install ashleym1972-SyslogLogger
86
+
87
+ == LICENSE:
88
+
89
+ Copyright (c) 2008, 2009 Eric Hodel, Christopher Powell, Ian Lesperance,
90
+ Dana Contreras, Brian Smith, Ashley Martens
91
+
92
+ Permission is hereby granted, free of charge, to any person obtaining
93
+ a copy of this software and associated documentation files (the
94
+ 'Software'), to deal in the Software without restriction, including
95
+ without limitation the rights to use, copy, modify, merge, publish,
96
+ distribute, sublicense, and/or sell copies of the Software, and to
97
+ permit persons to whom the Software is furnished to do so, subject to
98
+ the following conditions:
99
+
100
+ The above copyright notice and this permission notice shall be
101
+ included in all copies or substantial portions of the Software.
102
+
103
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
104
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
105
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
106
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
107
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
108
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
109
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
110
+
111
+
@@ -0,0 +1,115 @@
1
+ require 'syslog'
2
+ require 'logger'
3
+
4
+ class SyslogLogger
5
+ include Logger::Severity
6
+
7
+ # The version of SyslogLogger you are using.
8
+ VERSION = '1.6.0'
9
+
10
+ # From 'man syslog.h':
11
+ # LOG_EMERG A panic condition was reported to all processes.
12
+ # LOG_ALERT A condition that should be corrected immediately.
13
+ # LOG_CRIT A critical condition.
14
+ # LOG_ERR An error message.
15
+ # LOG_WARNING A warning message.
16
+ # LOG_NOTICE A condition requiring special handling.
17
+ # LOG_INFO A general information message.
18
+ # LOG_DEBUG A message useful for debugging programs.
19
+
20
+ # From logger rdoc:
21
+ # FATAL: an unhandleable error that results in a program crash
22
+ # ERROR: a handleable error condition
23
+ # WARN: a warning
24
+ # INFO: generic (useful) information about system operation
25
+ # DEBUG: low-level information for developers
26
+
27
+ # Maps Logger warning types to syslog(3) warning types.
28
+ LOGGER_MAP = {
29
+ :unknown => :alert,
30
+ :fatal => :alert,
31
+ :error => :err,
32
+ :warn => :warning,
33
+ :info => :info,
34
+ :debug => :debug
35
+ }
36
+
37
+ # Maps Logger log levels to their values so we can silence.
38
+ LOGGER_LEVEL_MAP = {}
39
+
40
+ LOGGER_MAP.each_key do |key|
41
+ LOGGER_LEVEL_MAP[key] = Logger.const_get key.to_s.upcase
42
+ end
43
+
44
+ # Maps Logger log level values to syslog log levels.
45
+ LEVEL_LOGGER_MAP = {}
46
+
47
+ LOGGER_LEVEL_MAP.invert.each do |level, severity|
48
+ LEVEL_LOGGER_MAP[level] = LOGGER_MAP[severity]
49
+ end
50
+
51
+ # Builds a methods for level +meth+.
52
+ for severity in Logger::Severity.constants
53
+ class_eval <<-EOT, __FILE__, __LINE__
54
+ def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
55
+ add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
56
+ end # end
57
+ #
58
+ def #{severity.downcase}? # def debug?
59
+ #{severity} >= @level # DEBUG >= @level
60
+ end # end
61
+ EOT
62
+ end
63
+
64
+ # Log level for Logger compatibility.
65
+ attr_accessor :level
66
+
67
+ # Fills in variables for Logger compatibility. If this is the first
68
+ # instance of SyslogLogger, +program_name+ may be set to change the logged
69
+ # program name and +facility+ may be set to specify a custom facility
70
+ # with your syslog daemon.
71
+ #
72
+ # Due to the way syslog works, only one program name may be chosen.
73
+ def initialize(program_name = 'rails', facility = Syslog::LOG_USER)
74
+ @level = Logger::DEBUG
75
+
76
+ return if defined? SYSLOG
77
+ self.class.const_set :SYSLOG, Syslog.open(program_name, nil, facility)
78
+ end
79
+
80
+ # Almost duplicates Logger#add. +progname+ is ignored.
81
+ def add(severity, message = nil, progname = nil, &block)
82
+ severity ||= Logger::UNKNOWN
83
+ return true if severity < @level
84
+ message = clean(message || block.call)
85
+ SYSLOG.send LEVEL_LOGGER_MAP[severity], clean(message)
86
+ return true
87
+ end
88
+
89
+ # Allows messages of a particular log level to be ignored temporarily.
90
+ def silence(temporary_level = Logger::ERROR)
91
+ old_logger_level = @level
92
+ @level = temporary_level
93
+ yield
94
+ ensure
95
+ @level = old_logger_level
96
+ end
97
+
98
+ # In Logger, this dumps the raw message; the closest equivalent
99
+ # would be Logger::UNKNOWN
100
+ def <<(message)
101
+ add(Logger::UNKNOWN, message)
102
+ end
103
+
104
+ private
105
+
106
+ # Clean up messages so they're nice and pretty.
107
+ def clean(message)
108
+ message = message.to_s.dup
109
+ message.strip!
110
+ message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
111
+ message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
112
+ return message
113
+ end
114
+
115
+ end
@@ -0,0 +1,38 @@
1
+ # Adds some default information to syslog messages.
2
+ # If you pass a User or Device object as the first parameter, it will log that user/device's id
3
+ # If you pass a block from a controller, it will automatically log the current user id
4
+ # If you pass a block, the class name of the block's context will be added
5
+ #
6
+ # Examples
7
+ # logger.debug "O'Doyle Rules!"
8
+ # #=> [development] [DEBUG: 2008-01-25 14:16:12.12345] O'Doyle Rules!
9
+ #
10
+ # #from within a controller...
11
+ # logger.error {"Something is messed up!"}
12
+ # #=> [development] [ERROR: 2008-01-25 14:16:12.12345] [123] [ClassName] Something is messed up!
13
+
14
+ class SyslogLogger
15
+
16
+ # short names for "DEBUG", "INFO", ...
17
+ # must be ordered to correspond to severity constants defined in
18
+ # ActiveSupport::BufferedLogger::Severity
19
+ #@@log_level_names = %w( D I W E F U )
20
+ #LOG_NAME_FIELD_WIDTH = 1
21
+
22
+ @@log_level_names = %w( DEBUG INFO WARN ERROR FATAL UNKNOWN )
23
+ LOG_NAME_FIELD_WIDTH = 7
24
+
25
+ def add_with_formatting(severity, message = nil, progname = nil, &block)
26
+ severity ||= Logger::UNKNOWN
27
+ message = "[#{RAILS_ENV}] [#{@@log_level_names[severity].ljust(LOG_NAME_FIELD_WIDTH)}: #{time.strftime("%Y-%m-%d %H:%M:%S")}.#{time.usec.to_s.rjust(6, '0')}] #{message}"
28
+
29
+ if(block)
30
+ add_without_formatting(severity, message, progname,
31
+ &Proc.new{g_log_formatter(severity, nil, user, &block)})
32
+ else
33
+ add_without_formatting(severity, message, progname)
34
+ end
35
+ end
36
+ alias_method_chain :add, :formatting
37
+
38
+ end
@@ -0,0 +1,491 @@
1
+ require 'test/unit'
2
+ require 'tempfile'
3
+ require 'syslog_logger'
4
+
5
+ module MockSyslog; end
6
+
7
+ class << MockSyslog
8
+
9
+ @line = nil
10
+
11
+ SyslogLogger::LOGGER_MAP.values.uniq.each do |level|
12
+ eval <<-EOM
13
+ def #{level}(message)
14
+ @line = "#{level.to_s.upcase} - \#{message}"
15
+ end
16
+ EOM
17
+ end
18
+
19
+ attr_reader :line
20
+ attr_reader :program_name
21
+
22
+ def open(program_name)
23
+ @program_name = program_name
24
+ end
25
+
26
+ def reset
27
+ @line = ''
28
+ end
29
+
30
+ end
31
+
32
+ SyslogLogger.const_set :SYSLOG, MockSyslog
33
+
34
+ class TestLogger < Test::Unit::TestCase
35
+
36
+ LEVEL_LABEL_MAP = {
37
+ Logger::DEBUG => 'DEBUG',
38
+ Logger::INFO => 'INFO',
39
+ Logger::WARN => 'WARN',
40
+ Logger::ERROR => 'ERROR',
41
+ Logger::FATAL => 'FATAL',
42
+ Logger::UNKNOWN => 'ANY',
43
+ }
44
+
45
+ def setup
46
+ @logger = Logger.new(nil)
47
+ end
48
+
49
+ class Log
50
+ attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
51
+ def initialize(line)
52
+ @line = line
53
+ /\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ @line
54
+ @label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
55
+ end
56
+ end
57
+
58
+ def log_add(severity, msg, progname = nil, &block)
59
+ log(:add, severity, msg, progname, &block)
60
+ end
61
+
62
+ def log(msg_id, *arg, &block)
63
+ Log.new(log_raw(msg_id, *arg, &block))
64
+ end
65
+
66
+ def log_raw(msg_id, *arg, &block)
67
+ logdev = Tempfile.new(File.basename(__FILE__) + '.log')
68
+ @logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
69
+ assert_equal true, @logger.__send__(msg_id, *arg, &block)
70
+ logdev.open
71
+ msg = logdev.read
72
+ logdev.close
73
+ msg
74
+ end
75
+
76
+ def test_initialize
77
+ assert_equal Logger::DEBUG, @logger.level
78
+ end
79
+
80
+ def test_add
81
+ msg = log_add nil, 'unknown level message' # nil == unknown
82
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
83
+
84
+ msg = log_add Logger::FATAL, 'fatal level message'
85
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
86
+
87
+ msg = log_add Logger::ERROR, 'error level message'
88
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
89
+
90
+ msg = log_add Logger::WARN, 'warn level message'
91
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
92
+
93
+ msg = log_add Logger::INFO, 'info level message'
94
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
95
+
96
+ msg = log_add Logger::DEBUG, 'debug level message'
97
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
98
+ end
99
+
100
+ def test_add_level_unknown
101
+ @logger.level = Logger::UNKNOWN
102
+
103
+ msg = log_add nil, 'unknown level message' # nil == unknown
104
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
105
+
106
+ msg = log_add Logger::FATAL, 'fatal level message'
107
+ assert_equal '', msg.line
108
+
109
+ msg = log_add Logger::ERROR, 'error level message'
110
+ assert_equal '', msg.line
111
+
112
+ msg = log_add Logger::WARN, 'warn level message'
113
+ assert_equal '', msg.line
114
+
115
+ msg = log_add Logger::INFO, 'info level message'
116
+ assert_equal '', msg.line
117
+
118
+ msg = log_add Logger::DEBUG, 'debug level message'
119
+ assert_equal '', msg.line
120
+ end
121
+
122
+ def test_add_level_fatal
123
+ @logger.level = Logger::FATAL
124
+
125
+ msg = log_add nil, 'unknown level message' # nil == unknown
126
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
127
+
128
+ msg = log_add Logger::FATAL, 'fatal level message'
129
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
130
+
131
+ msg = log_add Logger::ERROR, 'error level message'
132
+ assert_equal '', msg.line
133
+
134
+ msg = log_add Logger::WARN, 'warn level message'
135
+ assert_equal '', msg.line
136
+
137
+ msg = log_add Logger::INFO, 'info level message'
138
+ assert_equal '', msg.line
139
+
140
+ msg = log_add Logger::DEBUG, 'debug level message'
141
+ assert_equal '', msg.line
142
+ end
143
+
144
+ def test_add_level_error
145
+ @logger.level = Logger::ERROR
146
+
147
+ msg = log_add nil, 'unknown level message' # nil == unknown
148
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
149
+
150
+ msg = log_add Logger::FATAL, 'fatal level message'
151
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
152
+
153
+ msg = log_add Logger::ERROR, 'error level message'
154
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
155
+
156
+ msg = log_add Logger::WARN, 'warn level message'
157
+ assert_equal '', msg.line
158
+
159
+ msg = log_add Logger::INFO, 'info level message'
160
+ assert_equal '', msg.line
161
+
162
+ msg = log_add Logger::DEBUG, 'debug level message'
163
+ assert_equal '', msg.line
164
+ end
165
+
166
+ def test_add_level_warn
167
+ @logger.level = Logger::WARN
168
+
169
+ msg = log_add nil, 'unknown level message' # nil == unknown
170
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
171
+
172
+ msg = log_add Logger::FATAL, 'fatal level message'
173
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
174
+
175
+ msg = log_add Logger::ERROR, 'error level message'
176
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
177
+
178
+ msg = log_add Logger::WARN, 'warn level message'
179
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
180
+
181
+ msg = log_add Logger::INFO, 'info level message'
182
+ assert_equal '', msg.line
183
+
184
+ msg = log_add Logger::DEBUG, 'debug level message'
185
+ assert_equal '', msg.line
186
+ end
187
+
188
+ def test_add_level_info
189
+ @logger.level = Logger::INFO
190
+
191
+ msg = log_add nil, 'unknown level message' # nil == unknown
192
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
193
+
194
+ msg = log_add Logger::FATAL, 'fatal level message'
195
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
196
+
197
+ msg = log_add Logger::ERROR, 'error level message'
198
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
199
+
200
+ msg = log_add Logger::WARN, 'warn level message'
201
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
202
+
203
+ msg = log_add Logger::INFO, 'info level message'
204
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
205
+
206
+ msg = log_add Logger::DEBUG, 'debug level message'
207
+ assert_equal '', msg.line
208
+ end
209
+
210
+ def test_add_level_debug
211
+ @logger.level = Logger::DEBUG
212
+
213
+ msg = log_add nil, 'unknown level message' # nil == unknown
214
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
215
+
216
+ msg = log_add Logger::FATAL, 'fatal level message'
217
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
218
+
219
+ msg = log_add Logger::ERROR, 'error level message'
220
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
221
+
222
+ msg = log_add Logger::WARN, 'warn level message'
223
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
224
+
225
+ msg = log_add Logger::INFO, 'info level message'
226
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
227
+
228
+ msg = log_add Logger::DEBUG, 'debug level message'
229
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
230
+ end
231
+
232
+ def test_unknown
233
+ msg = log :unknown, 'unknown level message'
234
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
235
+
236
+ @logger.level = Logger::UNKNOWN
237
+ msg = log :unknown, 'unknown level message'
238
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
239
+
240
+ @logger.level = Logger::FATAL
241
+ msg = log :unknown, 'unknown level message'
242
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
243
+
244
+ @logger.level = Logger::ERROR
245
+ msg = log :unknown, 'unknown level message'
246
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
247
+
248
+ @logger.level = Logger::WARN
249
+ msg = log :unknown, 'unknown level message'
250
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
251
+
252
+ @logger.level = Logger::INFO
253
+ msg = log :unknown, 'unknown level message'
254
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
255
+
256
+ @logger.level = Logger::DEBUG
257
+ msg = log :unknown, 'unknown level message'
258
+ assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
259
+ end
260
+
261
+ # def test_unknown_eh
262
+ # @logger.level = Logger::UNKNOWN
263
+ # assert_equal true, @logger.unknown?
264
+ #
265
+ # @logger.level = Logger::UNKNOWN + 1
266
+ # assert_equal false, @logger.unknown?
267
+ # end
268
+
269
+ def test_fatal
270
+ msg = log :fatal, 'fatal level message'
271
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
272
+
273
+ @logger.level = Logger::UNKNOWN
274
+ msg = log :fatal, 'fatal level message'
275
+ assert_equal '', msg.line
276
+
277
+ @logger.level = Logger::FATAL
278
+ msg = log :fatal, 'fatal level message'
279
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
280
+
281
+ @logger.level = Logger::ERROR
282
+ msg = log :fatal, 'fatal level message'
283
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
284
+
285
+ @logger.level = Logger::WARN
286
+ msg = log :fatal, 'fatal level message'
287
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
288
+
289
+ @logger.level = Logger::INFO
290
+ msg = log :fatal, 'fatal level message'
291
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
292
+
293
+ @logger.level = Logger::DEBUG
294
+ msg = log :fatal, 'fatal level message'
295
+ assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
296
+ end
297
+
298
+ def test_fatal_eh
299
+ @logger.level = Logger::FATAL
300
+ assert_equal true, @logger.fatal?
301
+
302
+ @logger.level = Logger::UNKNOWN
303
+ assert_equal false, @logger.fatal?
304
+ end
305
+
306
+ def test_error
307
+ msg = log :error, 'error level message'
308
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
309
+
310
+ @logger.level = Logger::UNKNOWN
311
+ msg = log :error, 'error level message'
312
+ assert_equal '', msg.line
313
+
314
+ @logger.level = Logger::FATAL
315
+ msg = log :error, 'error level message'
316
+ assert_equal '', msg.line
317
+
318
+ @logger.level = Logger::ERROR
319
+ msg = log :error, 'error level message'
320
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
321
+
322
+ @logger.level = Logger::WARN
323
+ msg = log :error, 'error level message'
324
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
325
+
326
+ @logger.level = Logger::INFO
327
+ msg = log :error, 'error level message'
328
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
329
+
330
+ @logger.level = Logger::DEBUG
331
+ msg = log :error, 'error level message'
332
+ assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
333
+ end
334
+
335
+ def test_error_eh
336
+ @logger.level = Logger::ERROR
337
+ assert_equal true, @logger.error?
338
+
339
+ @logger.level = Logger::FATAL
340
+ assert_equal false, @logger.error?
341
+ end
342
+
343
+ def test_warn
344
+ msg = log :warn, 'warn level message'
345
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
346
+
347
+ @logger.level = Logger::UNKNOWN
348
+ msg = log :warn, 'warn level message'
349
+ assert_equal '', msg.line
350
+
351
+ @logger.level = Logger::FATAL
352
+ msg = log :warn, 'warn level message'
353
+ assert_equal '', msg.line
354
+
355
+ @logger.level = Logger::ERROR
356
+ msg = log :warn, 'warn level message'
357
+ assert_equal '', msg.line
358
+
359
+ @logger.level = Logger::WARN
360
+ msg = log :warn, 'warn level message'
361
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
362
+
363
+ @logger.level = Logger::INFO
364
+ msg = log :warn, 'warn level message'
365
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
366
+
367
+ @logger.level = Logger::DEBUG
368
+ msg = log :warn, 'warn level message'
369
+ assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
370
+ end
371
+
372
+ def test_warn_eh
373
+ @logger.level = Logger::WARN
374
+ assert_equal true, @logger.warn?
375
+
376
+ @logger.level = Logger::ERROR
377
+ assert_equal false, @logger.warn?
378
+ end
379
+
380
+ def test_info
381
+ msg = log :info, 'info level message'
382
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
383
+
384
+ @logger.level = Logger::UNKNOWN
385
+ msg = log :info, 'info level message'
386
+ assert_equal '', msg.line
387
+
388
+ @logger.level = Logger::FATAL
389
+ msg = log :info, 'info level message'
390
+ assert_equal '', msg.line
391
+
392
+ @logger.level = Logger::ERROR
393
+ msg = log :info, 'info level message'
394
+ assert_equal '', msg.line
395
+
396
+ @logger.level = Logger::WARN
397
+ msg = log :info, 'info level message'
398
+ assert_equal '', msg.line
399
+
400
+ @logger.level = Logger::INFO
401
+ msg = log :info, 'info level message'
402
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
403
+
404
+ @logger.level = Logger::DEBUG
405
+ msg = log :info, 'info level message'
406
+ assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
407
+ end
408
+
409
+ def test_info_eh
410
+ @logger.level = Logger::INFO
411
+ assert_equal true, @logger.info?
412
+
413
+ @logger.level = Logger::WARN
414
+ assert_equal false, @logger.info?
415
+ end
416
+
417
+ def test_debug
418
+ msg = log :debug, 'debug level message'
419
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
420
+
421
+ @logger.level = Logger::UNKNOWN
422
+ msg = log :debug, 'debug level message'
423
+ assert_equal '', msg.line
424
+
425
+ @logger.level = Logger::FATAL
426
+ msg = log :debug, 'debug level message'
427
+ assert_equal '', msg.line
428
+
429
+ @logger.level = Logger::ERROR
430
+ msg = log :debug, 'debug level message'
431
+ assert_equal '', msg.line
432
+
433
+ @logger.level = Logger::WARN
434
+ msg = log :debug, 'debug level message'
435
+ assert_equal '', msg.line
436
+
437
+ @logger.level = Logger::INFO
438
+ msg = log :debug, 'debug level message'
439
+ assert_equal '', msg.line
440
+
441
+ @logger.level = Logger::DEBUG
442
+ msg = log :debug, 'debug level message'
443
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
444
+ end
445
+
446
+ def test_debug_eh
447
+ @logger.level = Logger::DEBUG
448
+ assert_equal true, @logger.debug?
449
+
450
+ @logger.level = Logger::INFO
451
+ assert_equal false, @logger.debug?
452
+ end
453
+
454
+ end
455
+
456
+ class TestSyslogLogger < TestLogger
457
+
458
+ def setup
459
+ super
460
+ @logger = SyslogLogger.new
461
+ end
462
+
463
+ class Log
464
+ attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
465
+ def initialize(line)
466
+ @line = line
467
+ return unless /\A(\w+) - (.*)\Z/ =~ @line
468
+ severity, @msg = $1, $2
469
+ severity = SyslogLogger::LOGGER_MAP.invert[severity.downcase.intern]
470
+ @severity = severity.to_s.upcase
471
+ @severity = 'ANY' if @severity == 'UNKNOWN'
472
+ end
473
+ end
474
+
475
+ def log_add(severity, msg, progname = nil, &block)
476
+ log(:add, severity, msg, progname, &block)
477
+ end
478
+
479
+ def log(msg_id, *arg, &block)
480
+ Log.new(log_raw(msg_id, *arg, &block))
481
+ end
482
+
483
+ def log_raw(msg_id, *arg, &block)
484
+ assert_equal true, @logger.__send__(msg_id, *arg, &block)
485
+ msg = MockSyslog.line
486
+ MockSyslog.reset
487
+ return msg
488
+ end
489
+
490
+ end
491
+
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ashleym1972-syslog_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Hodel; Chris Powell; Matthew Boeh; Ian Lesperance; Dana Danger; Brian Smith; Ashley Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: An improved Logger replacement that logs to syslog. It is almost drop-in with a few caveats.
17
+ email: drbrain@segment7.net; cpowell@prylis.com; mboeh@desperance.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/syslog_logger.rb
26
+ - lib/syslog_logger_env_formatting.rb
27
+ - README.rdoc
28
+ has_rdoc: true
29
+ homepage: http://github.com/ashleym1972/syslog_logger
30
+ licenses:
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --line-numbers
34
+ - --inline-source
35
+ - --title
36
+ - SyslogLogger
37
+ - --main
38
+ - README.rdoc
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project: sysloglogger
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: An improved Logger replacement that logs to syslog. It is almost drop-in with a few caveats.
60
+ test_files:
61
+ - test/test_syslog_logger.rb