logger-syslog 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +115 -0
- data/lib/logger-syslog.rb +177 -0
- data/lib/syslog-formatter.rb +32 -0
- data/test/test_syslog_logger.rb +524 -0
- metadata +90 -0
data/README.rdoc
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
= SyslogLogger
|
2
|
+
|
3
|
+
http://github.com/ngmoco/sysloglogger
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Logger::Syslog is a Logger replacement that logs to syslog. It is almost drop-in
|
8
|
+
with a few caveats. You can add Logger::Syslog to your Rails production
|
9
|
+
environment to aggregate logs between multiple machines.
|
10
|
+
|
11
|
+
NOTE! You can only set the Logger::Syslog program name and facility when you initialize
|
12
|
+
Logger::Syslog for the first time. This is a limitation of the way Logger::Syslog
|
13
|
+
uses syslog (and in some ways, a limitation of the way syslog(3) works).
|
14
|
+
Attempts to change Logger::Syslog's program name or facility after the first initialization
|
15
|
+
will be ignored.
|
16
|
+
|
17
|
+
This particular Logger::Syslog 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
|
+
Version 1.6.7 takes a formatter as logger does and uses call to format the message.
|
21
|
+
|
22
|
+
== SYNOPSIS:
|
23
|
+
|
24
|
+
=== config/environment.rb
|
25
|
+
|
26
|
+
config.gem 'syslog-logger'
|
27
|
+
|
28
|
+
=== Gemfile
|
29
|
+
|
30
|
+
gem 'syslog-logger'
|
31
|
+
|
32
|
+
=== config/environments/production.rb
|
33
|
+
|
34
|
+
(Only this environment, since you probably only want to enable it for Production, and not Test or Development.)
|
35
|
+
|
36
|
+
RAILS_DEFAULT_LOGGER = Logger::Syslog.new
|
37
|
+
|
38
|
+
By default, Logger::Syslog uses the program name 'rails' and the facility 'user', but this can be
|
39
|
+
changed via the arguments to Logger::Syslog.new:
|
40
|
+
|
41
|
+
RAILS_DEFAULT_LOGGER = Logger::Syslog.new('mygreatapp', Syslog::LOG_LOCAL7)
|
42
|
+
|
43
|
+
=== BSD syslog setup
|
44
|
+
|
45
|
+
==== /etc/syslog.conf
|
46
|
+
|
47
|
+
Add the following lines:
|
48
|
+
|
49
|
+
!rails
|
50
|
+
*.* /var/log/production.log
|
51
|
+
|
52
|
+
Or, in Mac OS X:
|
53
|
+
|
54
|
+
local7.* /var/log/production.log
|
55
|
+
|
56
|
+
Then touch /var/log/production.log and signal syslogd with a HUP
|
57
|
+
(killall -HUP syslogd, on FreeBSD and OS X).
|
58
|
+
|
59
|
+
==== /etc/newsyslog.conf
|
60
|
+
|
61
|
+
Add the following line:
|
62
|
+
|
63
|
+
/var/log/production.log 640 7 * @T00 Z
|
64
|
+
|
65
|
+
This creates a log file that is rotated every day at midnight, gzip'd, then
|
66
|
+
kept for 7 days. Consult newsyslog.conf(5) for more details.
|
67
|
+
|
68
|
+
=== syslog-ng setup
|
69
|
+
|
70
|
+
==== syslog-ng.conf
|
71
|
+
|
72
|
+
A basic setup:
|
73
|
+
|
74
|
+
destination rails_log { file("/var/log/production.log"); };
|
75
|
+
filter f_rails { program("rails.*"); };
|
76
|
+
log { source(src); filter(f_rails); destination(rails_log); };
|
77
|
+
|
78
|
+
A more advanced setup with formatting, etc:
|
79
|
+
|
80
|
+
destination rails { file("/var/log/rails_apps" owner("rails") group("rails") perm(0664) template("$DATE $HOST $PROGRAM [$LEVEL] $MSGONLY\n") template_escape(no) ); };
|
81
|
+
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"); };
|
82
|
+
log { source(src); filter(f_rails); destination(rails); };
|
83
|
+
|
84
|
+
=== Starting
|
85
|
+
|
86
|
+
Now restart your Rails app. Your production logs should now be showing up in
|
87
|
+
/var/log/production.log. If you have mulitple machines, you can log them all
|
88
|
+
to a central machine with remote syslog logging for analysis. Consult your
|
89
|
+
syslogd(8) manpage for further details.
|
90
|
+
|
91
|
+
== LICENSE:
|
92
|
+
|
93
|
+
Copyright (c) 2008, 2009 Eric Hodel, Christopher Powell, Ian Lesperance,
|
94
|
+
Dana Contreras, Brian Smith, Ashley Martens
|
95
|
+
|
96
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
97
|
+
a copy of this software and associated documentation files (the
|
98
|
+
'Software'), to deal in the Software without restriction, including
|
99
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
100
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
101
|
+
permit persons to whom the Software is furnished to do so, subject to
|
102
|
+
the following conditions:
|
103
|
+
|
104
|
+
The above copyright notice and this permission notice shall be
|
105
|
+
included in all copies or substantial portions of the Software.
|
106
|
+
|
107
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
108
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
109
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
110
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
111
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
112
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
113
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
114
|
+
|
115
|
+
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'syslog'
|
2
|
+
require 'logger'
|
3
|
+
require 'syslog-formatter'
|
4
|
+
|
5
|
+
class Logger::Syslog
|
6
|
+
include Logger::Severity
|
7
|
+
|
8
|
+
# The version of Logger::Syslog you are using.
|
9
|
+
VERSION = '1.7.0'
|
10
|
+
|
11
|
+
# From 'man syslog.h':
|
12
|
+
# LOG_EMERG A panic condition was reported to all processes.
|
13
|
+
# LOG_ALERT A condition that should be corrected immediately.
|
14
|
+
# LOG_CRIT A critical condition.
|
15
|
+
# LOG_ERR An error message.
|
16
|
+
# LOG_WARNING A warning message.
|
17
|
+
# LOG_NOTICE A condition requiring special handling.
|
18
|
+
# LOG_INFO A general information message.
|
19
|
+
# LOG_DEBUG A message useful for debugging programs.
|
20
|
+
|
21
|
+
# From logger rdoc:
|
22
|
+
# FATAL: an unhandleable error that results in a program crash
|
23
|
+
# ERROR: a handleable error condition
|
24
|
+
# WARN: a warning
|
25
|
+
# INFO: generic (useful) information about system operation
|
26
|
+
# DEBUG: low-level information for developers
|
27
|
+
|
28
|
+
# Maps Logger warning types to syslog(3) warning types.
|
29
|
+
LOGGER_MAP = {
|
30
|
+
:unknown => Syslog::LOG_ALERT,
|
31
|
+
:fatal => Syslog::LOG_CRIT,
|
32
|
+
:error => Syslog::LOG_ERR,
|
33
|
+
:warn => Syslog::LOG_WARNING,
|
34
|
+
:info => Syslog::LOG_INFO,
|
35
|
+
:debug => Syslog::LOG_DEBUG
|
36
|
+
}
|
37
|
+
|
38
|
+
# Maps Logger log levels to their values so we can silence.
|
39
|
+
LOGGER_LEVEL_MAP = {}
|
40
|
+
|
41
|
+
LOGGER_MAP.each_key do |key|
|
42
|
+
LOGGER_LEVEL_MAP[key] = Logger.const_get key.to_s.upcase
|
43
|
+
end
|
44
|
+
|
45
|
+
# Maps Logger log level values to syslog log levels.
|
46
|
+
LEVEL_LOGGER_MAP = {}
|
47
|
+
|
48
|
+
LOGGER_LEVEL_MAP.invert.each do |level, severity|
|
49
|
+
LEVEL_LOGGER_MAP[level] = LOGGER_MAP[severity]
|
50
|
+
end
|
51
|
+
|
52
|
+
# Builds a methods for level +meth+.
|
53
|
+
for severity in Logger::Severity.constants
|
54
|
+
class_eval <<-EOT, __FILE__, __LINE__
|
55
|
+
def #{severity.downcase}(progname = nil, &block) # def debug(progname = nil, &block)
|
56
|
+
add(#{severity}, nil, progname, &block) # add(DEBUG, nil, progname, &block)
|
57
|
+
end # end
|
58
|
+
#
|
59
|
+
def #{severity.downcase}? # def debug?
|
60
|
+
@level <= #{severity} # @level <= DEBUG
|
61
|
+
end # end
|
62
|
+
EOT
|
63
|
+
end
|
64
|
+
|
65
|
+
# Log level for Logger compatibility.
|
66
|
+
attr_accessor :level
|
67
|
+
|
68
|
+
# Logging program name.
|
69
|
+
attr_accessor :progname
|
70
|
+
|
71
|
+
# Logging date-time format (string passed to +strftime+).
|
72
|
+
def datetime_format=(datetime_format)
|
73
|
+
@default_formatter.datetime_format = datetime_format
|
74
|
+
end
|
75
|
+
|
76
|
+
def datetime_format
|
77
|
+
@default_formatter.datetime_format
|
78
|
+
end
|
79
|
+
|
80
|
+
# Logging formatter. formatter#call is invoked with 4 arguments; severity,
|
81
|
+
# time, progname and msg for each log. Bear in mind that time is a Time and
|
82
|
+
# msg is an Object that user passed and it could not be a String. It is
|
83
|
+
# expected to return a logdev#write-able Object. Default formatter is used
|
84
|
+
# when no formatter is set.
|
85
|
+
attr_accessor :formatter
|
86
|
+
|
87
|
+
# This is the facility the logger will log to. You can have several loggers
|
88
|
+
# that log to different facilities.
|
89
|
+
attr_accessor :facility
|
90
|
+
|
91
|
+
alias sev_threshold level
|
92
|
+
alias sev_threshold= level=
|
93
|
+
|
94
|
+
# Fills in variables for Logger compatibility. If this is the first
|
95
|
+
# instance of Logger::Syslog, +program_name+ may be set to change the logged
|
96
|
+
# program name and +facility+ may be set to specify a custom facility
|
97
|
+
# with your syslog daemon.
|
98
|
+
#
|
99
|
+
# Due to the way syslog works, only one program name may be chosen.
|
100
|
+
def initialize(program_name = 'rails', facility = Syslog::LOG_USER, logopts=nil)
|
101
|
+
@default_formatter = Logger::SyslogFormatter.new
|
102
|
+
@formatter = nil
|
103
|
+
@progname = nil
|
104
|
+
@level = Logger::DEBUG
|
105
|
+
@facility = facility
|
106
|
+
|
107
|
+
return if defined? SYSLOG
|
108
|
+
self.class.const_set :SYSLOG, Syslog.open(program_name, logopts, facility)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Almost duplicates Logger#add. +progname+ is ignored.
|
112
|
+
def add(severity, message = nil, progname = nil, &block)
|
113
|
+
severity ||= Logger::UNKNOWN
|
114
|
+
if severity < @level
|
115
|
+
return true
|
116
|
+
end
|
117
|
+
if message.nil?
|
118
|
+
if block_given?
|
119
|
+
message = yield
|
120
|
+
else
|
121
|
+
message = progname
|
122
|
+
progname = @progname
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# breakup multiple lines into multiple syslog messages
|
127
|
+
message.each_line do | msg |
|
128
|
+
SYSLOG.log(LEVEL_LOGGER_MAP[severity] | @facility, format_message(format_severity(severity), Time.now, progname, clean(msg.chop)))
|
129
|
+
end
|
130
|
+
true
|
131
|
+
end
|
132
|
+
|
133
|
+
# Allows messages of a particular log level to be ignored temporarily.
|
134
|
+
def silence(temporary_level = Logger::ERROR)
|
135
|
+
old_logger_level = @level
|
136
|
+
@level = temporary_level
|
137
|
+
yield
|
138
|
+
ensure
|
139
|
+
@level = old_logger_level
|
140
|
+
end
|
141
|
+
|
142
|
+
# In Logger, this dumps the raw message; the closest equivalent
|
143
|
+
# would be Logger::UNKNOWN
|
144
|
+
def <<(message)
|
145
|
+
add(Logger::UNKNOWN, message)
|
146
|
+
end
|
147
|
+
|
148
|
+
private
|
149
|
+
|
150
|
+
# Severity label for logging. (max 5 char)
|
151
|
+
SEV_LABEL = {
|
152
|
+
Logger::DEBUG => "DEBUG",
|
153
|
+
Logger::INFO => "INFO",
|
154
|
+
Logger::WARN => "WARN",
|
155
|
+
Logger::ERROR => "ERROR",
|
156
|
+
Logger::FATAL => "FATAL",
|
157
|
+
Logger::UNKNOWN => "ANY"
|
158
|
+
}
|
159
|
+
|
160
|
+
def format_severity(severity)
|
161
|
+
SEV_LABEL[severity] || 'ANY'
|
162
|
+
end
|
163
|
+
|
164
|
+
def format_message(severity, datetime, progname, msg)
|
165
|
+
(@formatter || @default_formatter).call(severity, datetime, progname, msg)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Clean up messages so they're nice and pretty.
|
169
|
+
def clean(message)
|
170
|
+
message = message.to_s.dup
|
171
|
+
message.strip!
|
172
|
+
message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
|
173
|
+
message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
|
174
|
+
return message
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Adds some default information to syslog messages.
|
2
|
+
# Log format:
|
3
|
+
# [Time.mSec] [SeverityLabel]: message
|
4
|
+
|
5
|
+
class Logger::SyslogFormatter < Logger::Formatter
|
6
|
+
Format = "[%5s] %s\n"
|
7
|
+
|
8
|
+
attr_accessor :datetime_format
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@datetime_format = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(severity, time, progname, msg)
|
15
|
+
Format % [severity, msg2str(msg)]
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def msg2str(msg)
|
21
|
+
case msg
|
22
|
+
when ::String
|
23
|
+
msg
|
24
|
+
when ::Exception
|
25
|
+
"#{ msg.message } (#{ msg.class })\n" <<
|
26
|
+
(msg.backtrace || []).join("\n")
|
27
|
+
else
|
28
|
+
msg.inspect
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,524 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'logger-syslog'
|
4
|
+
|
5
|
+
module MockSyslog; end
|
6
|
+
|
7
|
+
class << MockSyslog
|
8
|
+
|
9
|
+
@line = nil
|
10
|
+
|
11
|
+
attr_reader :line
|
12
|
+
attr_reader :program_name
|
13
|
+
|
14
|
+
def open(program_name)
|
15
|
+
@program_name = program_name
|
16
|
+
end
|
17
|
+
|
18
|
+
def log(priority, message)
|
19
|
+
@line = message
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset
|
23
|
+
@line = ''
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
Logger::Syslog.const_set :SYSLOG, MockSyslog
|
29
|
+
|
30
|
+
class TestLogger < Test::Unit::TestCase
|
31
|
+
|
32
|
+
LEVEL_LABEL_MAP = {
|
33
|
+
Logger::DEBUG => 'DEBUG',
|
34
|
+
Logger::INFO => 'INFO',
|
35
|
+
Logger::WARN => 'WARN',
|
36
|
+
Logger::ERROR => 'ERROR',
|
37
|
+
Logger::FATAL => 'FATAL',
|
38
|
+
Logger::UNKNOWN => 'ANY',
|
39
|
+
}
|
40
|
+
|
41
|
+
def setup
|
42
|
+
@logger = Logger.new(nil)
|
43
|
+
end
|
44
|
+
|
45
|
+
class Log
|
46
|
+
attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
|
47
|
+
def initialize(line)
|
48
|
+
@line = line
|
49
|
+
/\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ @line
|
50
|
+
@label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def log_add(severity, msg, progname = nil, &block)
|
55
|
+
log(:add, severity, msg, progname, &block)
|
56
|
+
end
|
57
|
+
|
58
|
+
def log(msg_id, *arg, &block)
|
59
|
+
Log.new(log_raw(msg_id, *arg, &block))
|
60
|
+
end
|
61
|
+
|
62
|
+
def log_raw(msg_id, *arg, &block)
|
63
|
+
logdev = Tempfile.new(File.basename(__FILE__) + '.log')
|
64
|
+
@logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
|
65
|
+
assert_equal true, @logger.__send__(msg_id, *arg, &block)
|
66
|
+
logdev.open
|
67
|
+
msg = logdev.read
|
68
|
+
logdev.close
|
69
|
+
msg
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_initialize
|
73
|
+
assert_equal Logger::DEBUG, @logger.level
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_add
|
77
|
+
msg = log_add nil, 'unknown level message' # nil == unknown
|
78
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
79
|
+
|
80
|
+
msg = log_add Logger::FATAL, 'fatal level message'
|
81
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
82
|
+
|
83
|
+
msg = log_add Logger::ERROR, 'error level message'
|
84
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
85
|
+
|
86
|
+
msg = log_add Logger::WARN, 'warn level message'
|
87
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
88
|
+
|
89
|
+
msg = log_add Logger::INFO, 'info level message'
|
90
|
+
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
91
|
+
|
92
|
+
msg = log_add Logger::DEBUG, 'debug level message'
|
93
|
+
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_add_level_unknown
|
97
|
+
@logger.level = Logger::UNKNOWN
|
98
|
+
|
99
|
+
msg = log_add nil, 'unknown level message' # nil == unknown
|
100
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
101
|
+
|
102
|
+
msg = log_add Logger::FATAL, 'fatal level message'
|
103
|
+
assert_equal '', msg.line
|
104
|
+
|
105
|
+
msg = log_add Logger::ERROR, 'error level message'
|
106
|
+
assert_equal '', msg.line
|
107
|
+
|
108
|
+
msg = log_add Logger::WARN, 'warn level message'
|
109
|
+
assert_equal '', msg.line
|
110
|
+
|
111
|
+
msg = log_add Logger::INFO, 'info level message'
|
112
|
+
assert_equal '', msg.line
|
113
|
+
|
114
|
+
msg = log_add Logger::DEBUG, 'debug level message'
|
115
|
+
assert_equal '', msg.line
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_add_level_fatal
|
119
|
+
@logger.level = Logger::FATAL
|
120
|
+
|
121
|
+
msg = log_add nil, 'unknown level message' # nil == unknown
|
122
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
123
|
+
|
124
|
+
msg = log_add Logger::FATAL, 'fatal level message'
|
125
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
126
|
+
|
127
|
+
msg = log_add Logger::ERROR, 'error level message'
|
128
|
+
assert_equal '', msg.line
|
129
|
+
|
130
|
+
msg = log_add Logger::WARN, 'warn level message'
|
131
|
+
assert_equal '', msg.line
|
132
|
+
|
133
|
+
msg = log_add Logger::INFO, 'info level message'
|
134
|
+
assert_equal '', msg.line
|
135
|
+
|
136
|
+
msg = log_add Logger::DEBUG, 'debug level message'
|
137
|
+
assert_equal '', msg.line
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_add_level_error
|
141
|
+
@logger.level = Logger::ERROR
|
142
|
+
|
143
|
+
msg = log_add nil, 'unknown level message' # nil == unknown
|
144
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
145
|
+
|
146
|
+
msg = log_add Logger::FATAL, 'fatal level message'
|
147
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
148
|
+
|
149
|
+
msg = log_add Logger::ERROR, 'error level message'
|
150
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
151
|
+
|
152
|
+
msg = log_add Logger::WARN, 'warn level message'
|
153
|
+
assert_equal '', msg.line
|
154
|
+
|
155
|
+
msg = log_add Logger::INFO, 'info level message'
|
156
|
+
assert_equal '', msg.line
|
157
|
+
|
158
|
+
msg = log_add Logger::DEBUG, 'debug level message'
|
159
|
+
assert_equal '', msg.line
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_add_level_warn
|
163
|
+
@logger.level = Logger::WARN
|
164
|
+
|
165
|
+
msg = log_add nil, 'unknown level message' # nil == unknown
|
166
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
167
|
+
|
168
|
+
msg = log_add Logger::FATAL, 'fatal level message'
|
169
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
170
|
+
|
171
|
+
msg = log_add Logger::ERROR, 'error level message'
|
172
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
173
|
+
|
174
|
+
msg = log_add Logger::WARN, 'warn level message'
|
175
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
176
|
+
|
177
|
+
msg = log_add Logger::INFO, 'info level message'
|
178
|
+
assert_equal '', msg.line
|
179
|
+
|
180
|
+
msg = log_add Logger::DEBUG, 'debug level message'
|
181
|
+
assert_equal '', msg.line
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_add_level_info
|
185
|
+
@logger.level = Logger::INFO
|
186
|
+
|
187
|
+
msg = log_add nil, 'unknown level message' # nil == unknown
|
188
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
189
|
+
|
190
|
+
msg = log_add Logger::FATAL, 'fatal level message'
|
191
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
192
|
+
|
193
|
+
msg = log_add Logger::ERROR, 'error level message'
|
194
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
195
|
+
|
196
|
+
msg = log_add Logger::WARN, 'warn level message'
|
197
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
198
|
+
|
199
|
+
msg = log_add Logger::INFO, 'info level message'
|
200
|
+
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
201
|
+
|
202
|
+
msg = log_add Logger::DEBUG, 'debug level message'
|
203
|
+
assert_equal '', msg.line
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_add_level_debug
|
207
|
+
@logger.level = Logger::DEBUG
|
208
|
+
|
209
|
+
msg = log_add nil, 'unknown level message' # nil == unknown
|
210
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
211
|
+
|
212
|
+
msg = log_add Logger::FATAL, 'fatal level message'
|
213
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
214
|
+
|
215
|
+
msg = log_add Logger::ERROR, 'error level message'
|
216
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
217
|
+
|
218
|
+
msg = log_add Logger::WARN, 'warn level message'
|
219
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
220
|
+
|
221
|
+
msg = log_add Logger::INFO, 'info level message'
|
222
|
+
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
223
|
+
|
224
|
+
msg = log_add Logger::DEBUG, 'debug level message'
|
225
|
+
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_unknown
|
229
|
+
msg = log :unknown, 'unknown level message'
|
230
|
+
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
231
|
+
|
232
|
+
assert_raise(ArgumentError) do
|
233
|
+
log :unknown, 'unknown level message', :dummy_progname
|
234
|
+
end
|
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
|
+
assert_raise(ArgumentError) do
|
274
|
+
log :fatal, 'fatal level message', :dummy_progname
|
275
|
+
end
|
276
|
+
|
277
|
+
@logger.level = Logger::UNKNOWN
|
278
|
+
msg = log :fatal, 'fatal level message'
|
279
|
+
assert_equal '', msg.line
|
280
|
+
|
281
|
+
@logger.level = Logger::FATAL
|
282
|
+
msg = log :fatal, 'fatal level message'
|
283
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
284
|
+
|
285
|
+
@logger.level = Logger::ERROR
|
286
|
+
msg = log :fatal, 'fatal level message'
|
287
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
288
|
+
|
289
|
+
@logger.level = Logger::WARN
|
290
|
+
msg = log :fatal, 'fatal level message'
|
291
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
292
|
+
|
293
|
+
@logger.level = Logger::INFO
|
294
|
+
msg = log :fatal, 'fatal level message'
|
295
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
296
|
+
|
297
|
+
@logger.level = Logger::DEBUG
|
298
|
+
msg = log :fatal, 'fatal level message'
|
299
|
+
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_fatal_eh
|
303
|
+
@logger.level = Logger::FATAL
|
304
|
+
assert_equal true, @logger.fatal?
|
305
|
+
|
306
|
+
@logger.level = Logger::UNKNOWN
|
307
|
+
assert_equal false, @logger.fatal?
|
308
|
+
end
|
309
|
+
|
310
|
+
def test_error
|
311
|
+
msg = log :error, 'error level message'
|
312
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
313
|
+
|
314
|
+
assert_raise(ArgumentError) do
|
315
|
+
log :error, 'error level message', :dummy_progname
|
316
|
+
end
|
317
|
+
|
318
|
+
@logger.level = Logger::UNKNOWN
|
319
|
+
msg = log :error, 'error level message'
|
320
|
+
assert_equal '', msg.line
|
321
|
+
|
322
|
+
@logger.level = Logger::FATAL
|
323
|
+
msg = log :error, 'error level message'
|
324
|
+
assert_equal '', msg.line
|
325
|
+
|
326
|
+
@logger.level = Logger::ERROR
|
327
|
+
msg = log :error, 'error level message'
|
328
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
329
|
+
|
330
|
+
@logger.level = Logger::WARN
|
331
|
+
msg = log :error, 'error level message'
|
332
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
333
|
+
|
334
|
+
@logger.level = Logger::INFO
|
335
|
+
msg = log :error, 'error level message'
|
336
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
337
|
+
|
338
|
+
@logger.level = Logger::DEBUG
|
339
|
+
msg = log :error, 'error level message'
|
340
|
+
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
341
|
+
end
|
342
|
+
|
343
|
+
def test_error_eh
|
344
|
+
@logger.level = Logger::ERROR
|
345
|
+
assert_equal true, @logger.error?
|
346
|
+
|
347
|
+
@logger.level = Logger::FATAL
|
348
|
+
assert_equal false, @logger.error?
|
349
|
+
end
|
350
|
+
|
351
|
+
def test_warn
|
352
|
+
msg = log :warn, 'warn level message'
|
353
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
354
|
+
|
355
|
+
assert_raise(ArgumentError) do
|
356
|
+
log :warn, 'warn level message', :dummy_progname
|
357
|
+
end
|
358
|
+
|
359
|
+
@logger.level = Logger::UNKNOWN
|
360
|
+
msg = log :warn, 'warn level message'
|
361
|
+
assert_equal '', msg.line
|
362
|
+
|
363
|
+
@logger.level = Logger::FATAL
|
364
|
+
msg = log :warn, 'warn level message'
|
365
|
+
assert_equal '', msg.line
|
366
|
+
|
367
|
+
@logger.level = Logger::ERROR
|
368
|
+
msg = log :warn, 'warn level message'
|
369
|
+
assert_equal '', msg.line
|
370
|
+
|
371
|
+
@logger.level = Logger::WARN
|
372
|
+
msg = log :warn, 'warn level message'
|
373
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
374
|
+
|
375
|
+
@logger.level = Logger::INFO
|
376
|
+
msg = log :warn, 'warn level message'
|
377
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
378
|
+
|
379
|
+
@logger.level = Logger::DEBUG
|
380
|
+
msg = log :warn, 'warn level message'
|
381
|
+
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
382
|
+
end
|
383
|
+
|
384
|
+
def test_warn_eh
|
385
|
+
@logger.level = Logger::WARN
|
386
|
+
assert_equal true, @logger.warn?
|
387
|
+
|
388
|
+
@logger.level = Logger::ERROR
|
389
|
+
assert_equal false, @logger.warn?
|
390
|
+
end
|
391
|
+
|
392
|
+
def test_info
|
393
|
+
msg = log :info, 'info level message'
|
394
|
+
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
395
|
+
|
396
|
+
assert_raise(ArgumentError) do
|
397
|
+
log :info, 'info level message', :dummy_progname
|
398
|
+
end
|
399
|
+
|
400
|
+
@logger.level = Logger::UNKNOWN
|
401
|
+
msg = log :info, 'info level message'
|
402
|
+
assert_equal '', msg.line
|
403
|
+
|
404
|
+
@logger.level = Logger::FATAL
|
405
|
+
msg = log :info, 'info level message'
|
406
|
+
assert_equal '', msg.line
|
407
|
+
|
408
|
+
@logger.level = Logger::ERROR
|
409
|
+
msg = log :info, 'info level message'
|
410
|
+
assert_equal '', msg.line
|
411
|
+
|
412
|
+
@logger.level = Logger::WARN
|
413
|
+
msg = log :info, 'info level message'
|
414
|
+
assert_equal '', msg.line
|
415
|
+
|
416
|
+
@logger.level = Logger::INFO
|
417
|
+
msg = log :info, 'info level message'
|
418
|
+
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
419
|
+
|
420
|
+
@logger.level = Logger::DEBUG
|
421
|
+
msg = log :info, 'info level message'
|
422
|
+
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
423
|
+
end
|
424
|
+
|
425
|
+
def test_info_eh
|
426
|
+
@logger.level = Logger::INFO
|
427
|
+
assert_equal true, @logger.info?
|
428
|
+
|
429
|
+
@logger.level = Logger::WARN
|
430
|
+
assert_equal false, @logger.info?
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_debug
|
434
|
+
msg = log :debug, 'debug level message'
|
435
|
+
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
436
|
+
|
437
|
+
assert_raise(ArgumentError) do
|
438
|
+
log :debug, 'debug level message', :dummy_progname
|
439
|
+
end
|
440
|
+
|
441
|
+
@logger.level = Logger::UNKNOWN
|
442
|
+
msg = log :debug, 'debug level message'
|
443
|
+
assert_equal '', msg.line
|
444
|
+
|
445
|
+
@logger.level = Logger::FATAL
|
446
|
+
msg = log :debug, 'debug level message'
|
447
|
+
assert_equal '', msg.line
|
448
|
+
|
449
|
+
@logger.level = Logger::ERROR
|
450
|
+
msg = log :debug, 'debug level message'
|
451
|
+
assert_equal '', msg.line
|
452
|
+
|
453
|
+
@logger.level = Logger::WARN
|
454
|
+
msg = log :debug, 'debug level message'
|
455
|
+
assert_equal '', msg.line
|
456
|
+
|
457
|
+
@logger.level = Logger::INFO
|
458
|
+
msg = log :debug, 'debug level message'
|
459
|
+
assert_equal '', msg.line
|
460
|
+
|
461
|
+
@logger.level = Logger::DEBUG
|
462
|
+
msg = log :debug, 'debug level message'
|
463
|
+
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
464
|
+
end
|
465
|
+
|
466
|
+
def test_debug_eh
|
467
|
+
@logger.level = Logger::DEBUG
|
468
|
+
assert_equal true, @logger.debug?
|
469
|
+
|
470
|
+
@logger.level = Logger::INFO
|
471
|
+
assert_equal false, @logger.debug?
|
472
|
+
end
|
473
|
+
|
474
|
+
[:unknown, :fatal, :error, :warn, :info, :debug].each do |severity|
|
475
|
+
class_eval <<-EOS, __FILE__, __LINE__
|
476
|
+
def test_#{severity}_block
|
477
|
+
msg = log :#{severity}, '#{severity} level message'
|
478
|
+
msg_blk = log(:#{severity}){ '#{severity} level message' }
|
479
|
+
[:severity, :msg].each do |attr|
|
480
|
+
assert_equal msg.send(attr), msg_blk.send(attr)
|
481
|
+
end
|
482
|
+
|
483
|
+
msg_blk = log(:#{severity}, :dummy_progname){ '#{severity} level message' }
|
484
|
+
[:severity, :msg].each do |attr|
|
485
|
+
assert_equal msg.send(attr), msg_blk.send(attr)
|
486
|
+
end
|
487
|
+
end
|
488
|
+
EOS
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
class TestSyslogLogger < TestLogger
|
493
|
+
|
494
|
+
def setup
|
495
|
+
super
|
496
|
+
@logger = Logger::Syslog.new
|
497
|
+
end
|
498
|
+
|
499
|
+
class Log
|
500
|
+
attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
|
501
|
+
def initialize(line)
|
502
|
+
@line = line
|
503
|
+
return unless /\A\[\s*([^\]]+)\] ([\x0-\xff]*)/ =~ @line
|
504
|
+
@severity, @msg = $1, $2
|
505
|
+
end
|
506
|
+
end
|
507
|
+
|
508
|
+
def log_add(severity, msg, progname = nil, &block)
|
509
|
+
log(:add, severity, msg, progname, &block)
|
510
|
+
end
|
511
|
+
|
512
|
+
def log(msg_id, *arg, &block)
|
513
|
+
Log.new(log_raw(msg_id, *arg, &block))
|
514
|
+
end
|
515
|
+
|
516
|
+
def log_raw(msg_id, *arg, &block)
|
517
|
+
assert_equal true, @logger.__send__(msg_id, *arg, &block)
|
518
|
+
msg = MockSyslog.line
|
519
|
+
MockSyslog.reset
|
520
|
+
return msg
|
521
|
+
end
|
522
|
+
|
523
|
+
end
|
524
|
+
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logger-syslog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 11
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 1.7.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sergio Cambra; Eric Hodel; Chris Powell; Matthew Boeh; Ian Lesperance; Dana Danger; Brian Smith; Ashley Martens
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-17 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: bundler
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 19
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
version: 1.1.0
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
description: An improved Logger replacement that logs to syslog. It is almost drop-in with a few caveats.
|
37
|
+
email:
|
38
|
+
- sergio@programatica.es
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/syslog-formatter.rb
|
47
|
+
- lib/logger-syslog.rb
|
48
|
+
- README.rdoc
|
49
|
+
- test/test_syslog_logger.rb
|
50
|
+
homepage: https://github.com/scambra/logger-syslog
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --main
|
56
|
+
- README.rdoc
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 59
|
66
|
+
segments:
|
67
|
+
- 1
|
68
|
+
- 8
|
69
|
+
- 6
|
70
|
+
version: 1.8.6
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 23
|
77
|
+
segments:
|
78
|
+
- 1
|
79
|
+
- 3
|
80
|
+
- 6
|
81
|
+
version: 1.3.6
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.23
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: An improved Logger replacement that logs to syslog. It is almost drop-in with a few caveats.
|
89
|
+
test_files:
|
90
|
+
- test/test_syslog_logger.rb
|