SyslogLogger 1.4.1 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,7 @@
1
- == 1.4.1
1
+ == 2.0 / Unreleased
2
2
 
3
- * Bug fixes
4
- * Messages for syslog() are no longer cleaned twice. Pull Request #3 by
5
- Lourens Naudé
3
+ * SyslogLogger is now Syslog::Logger
4
+ * Syslog::Logger has a formatter like Logger
6
5
 
7
6
  == 1.4.0 / 2007-05-08
8
7
 
@@ -3,5 +3,5 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  lib/analyzer_tools/syslog_logger.rb
6
- lib/syslog_logger.rb
6
+ lib/syslog/logger.rb
7
7
  test/test_syslog_logger.rb
data/README.txt CHANGED
@@ -1,56 +1,17 @@
1
1
  = SyslogLogger
2
2
 
3
- Documentation :: http://docs.seattlerb.org/SyslogLogger
4
- Source :: https://github.com/seattlerb/sysloglogger
5
- Bugtracker :: https://github.com/seattlerb/sysloglogger/issues
3
+ * http://github.com/seattlerb/sysloglogger
6
4
 
7
- == DESCRIPTION
5
+ == Description
8
6
 
9
7
  SyslogLogger is a Logger replacement that logs to syslog. It is almost
10
- drop-in with a few differences.
8
+ drop-in with a few caveats.
11
9
 
12
- == FEATURES
10
+ == About
13
11
 
14
- * Works like Logger
15
- * Logs to syslog(3) mapping Logger levels to syslog(3) levels
12
+ See SyslogLogger
16
13
 
17
- == SYNOPSIS
14
+ == Install
18
15
 
19
- require 'syslog_logger'
20
-
21
- logger = SyslogLogger.new 'your_application_name'
22
-
23
- logger.info 'did something cool'
24
-
25
- == INSTALL
26
-
27
- gem install SyslogLogger
28
-
29
- You may need to configure your syslog.conf to place application logs in a
30
- particular file. See SyslogLogger for details.
31
-
32
- == LICENSE
33
-
34
- (The MIT License)
35
-
36
- Copyright (c) Eric Hodel
37
-
38
- Permission is hereby granted, free of charge, to any person obtaining
39
- a copy of this software and associated documentation files (the
40
- 'Software'), to deal in the Software without restriction, including
41
- without limitation the rights to use, copy, modify, merge, publish,
42
- distribute, sublicense, and/or sell copies of the Software, and to
43
- permit persons to whom the Software is furnished to do so, subject to
44
- the following conditions:
45
-
46
- The above copyright notice and this permission notice shall be
47
- included in all copies or substantial portions of the Software.
48
-
49
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
50
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ sudo gem install SyslogLogger
56
17
 
data/Rakefile CHANGED
@@ -2,14 +2,13 @@
2
2
 
3
3
  require 'hoe'
4
4
 
5
- Hoe.plugin :travis
5
+ Hoe.plugin :seattlerb
6
6
  Hoe.plugin :git
7
7
 
8
8
  Hoe.spec 'SyslogLogger' do
9
9
  developer 'Eric Hodel', 'drbrain@segment7.net'
10
10
 
11
- rdoc_locations <<
12
- 'docs.seattlerb.org:/data/www/docs.seattlerb.org/SyslogLogger'
11
+ self.rubyforge_name = 'seattlerb'
13
12
  end
14
13
 
15
14
  # vim: syntax=Ruby
@@ -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'
178
+ @level = ::Logger::DEBUG
179
+ @formatter = Formatter.new
180
+
181
+ @@syslog ||= Syslog.open(program_name)
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
+
@@ -1,37 +1,52 @@
1
1
  require 'test/unit'
2
2
  require 'tempfile'
3
- require 'syslog_logger'
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
4
12
 
5
- module MockSyslog; end
13
+ module MockSyslog
14
+ LEVEL_LABEL_MAP = {}
6
15
 
7
- class << MockSyslog
16
+ class << self
8
17
 
9
- @line = nil
18
+ @line = nil
10
19
 
11
- SyslogLogger::LOGGER_MAP.values.uniq.each do |level|
12
- eval <<-EOM
13
- def #{level}(message)
14
- @line = "#{level.to_s.upcase} - \#{message}"
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
15
29
  end
16
- EOM
17
- end
18
30
 
19
- attr_reader :line
20
- attr_reader :program_name
31
+ def log(level, format, *args)
32
+ @line = "#{LEVEL_LABEL_MAP[level]} - #{format % args}"
33
+ end
21
34
 
22
- def open(program_name)
23
- @program_name = program_name
24
- end
35
+ attr_reader :line
36
+ attr_reader :program_name
25
37
 
26
- def reset
27
- @line = ''
28
- end
38
+ def open(program_name)
39
+ @program_name = program_name
40
+ end
29
41
 
30
- end
42
+ def reset
43
+ @line = ''
44
+ end
31
45
 
32
- SyslogLogger.const_set :SYSLOG, MockSyslog
46
+ end
47
+ end
33
48
 
34
- class TestLogger < Test::Unit::TestCase
49
+ Syslog::Logger.syslog = MockSyslog
35
50
 
36
51
  LEVEL_LABEL_MAP = {
37
52
  Logger::DEBUG => 'DEBUG',
@@ -77,6 +92,16 @@ class TestLogger < Test::Unit::TestCase
77
92
  assert_equal Logger::DEBUG, @logger.level
78
93
  end
79
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
+
80
105
  def test_add
81
106
  msg = log_add nil, 'unknown level message' # nil == unknown
82
107
  assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
@@ -443,24 +468,30 @@ class TestLogger < Test::Unit::TestCase
443
468
  assert_equal false, @logger.debug?
444
469
  end
445
470
 
446
- end
471
+ end if defined?(Syslog)
447
472
 
448
- class TestSyslogLogger < TestLogger
473
+ class TestSyslogLogger < TestSyslogRootLogger
449
474
 
450
475
  def setup
451
476
  super
452
- @logger = SyslogLogger.new
477
+ @logger = Syslog::Logger.new
453
478
  end
454
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
+
455
488
  class Log
456
489
  attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
457
490
  def initialize(line)
458
491
  @line = line
459
492
  return unless /\A(\w+) - (.*)\Z/ =~ @line
460
493
  severity, @msg = $1, $2
461
- severity = SyslogLogger::LOGGER_MAP.invert[severity.downcase.intern]
462
- @severity = severity.to_s.upcase
463
- @severity = 'ANY' if @severity == 'UNKNOWN'
494
+ @severity = SEVERITY_MAP[severity]
464
495
  end
465
496
  end
466
497
 
@@ -487,5 +518,4 @@ class TestSyslogLogger < TestLogger
487
518
  assert_equal false, @logger.unknown?
488
519
  end
489
520
 
490
- end
491
-
521
+ end if defined?(Syslog)
metadata CHANGED
@@ -1,129 +1,93 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: SyslogLogger
3
- version: !ruby/object:Gem::Version
4
- hash: 5
3
+ version: !ruby/object:Gem::Version
4
+ version: '2.0'
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 4
9
- - 1
10
- version: 1.4.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Eric Hodel
14
9
  autorequire:
15
10
  bindir: bin
16
- cert_chain:
17
- - |
18
- -----BEGIN CERTIFICATE-----
19
- MIIDeDCCAmCgAwIBAgIBATANBgkqhkiG9w0BAQUFADBBMRAwDgYDVQQDDAdkcmJy
20
- YWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZFgNu
21
- ZXQwHhcNMTIwMjI4MTc1NDI1WhcNMTMwMjI3MTc1NDI1WjBBMRAwDgYDVQQDDAdk
22
- cmJyYWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZ
23
- FgNuZXQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCbbgLrGLGIDE76
24
- LV/cvxdEzCuYuS3oG9PrSZnuDweySUfdp/so0cDq+j8bqy6OzZSw07gdjwFMSd6J
25
- U5ddZCVywn5nnAQ+Ui7jMW54CYt5/H6f2US6U0hQOjJR6cpfiymgxGdfyTiVcvTm
26
- Gj/okWrQl0NjYOYBpDi+9PPmaH2RmLJu0dB/NylsDnW5j6yN1BEI8MfJRR+HRKZY
27
- mUtgzBwF1V4KIZQ8EuL6I/nHVu07i6IkrpAgxpXUfdJQJi0oZAqXurAV3yTxkFwd
28
- g62YrrW26mDe+pZBzR6bpLE+PmXCzz7UxUq3AE0gPHbiMXie3EFE0oxnsU3lIduh
29
- sCANiQ8BAgMBAAGjezB5MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW
30
- BBS5k4Z75VSpdM0AclG2UvzFA/VW5DAfBgNVHREEGDAWgRRkcmJyYWluQHNlZ21l
31
- bnQ3Lm5ldDAfBgNVHRIEGDAWgRRkcmJyYWluQHNlZ21lbnQ3Lm5ldDANBgkqhkiG
32
- 9w0BAQUFAAOCAQEAPeWzFnrcvC6eVzdlhmjUub2s6qieBkongKRDHQz5MEeQv4LS
33
- SARnoHY+uCAVL/1xGAhmpzqQ3fJGWK9eBacW/e8E5GF9xQcV3mE1bA0WNaiDlX5j
34
- U2aI+ZGSblqvHUCxKBHR1s7UMHsbz1saOmgdRTyPx0juJs68ocbUTeYBLWu9V4KP
35
- zdGAG2JXO2gONg3b4tYDvpBLbry+KOX27iAJulUaH9TiTOULL4ITJVFsK0mYVqmR
36
- Q8Tno9S3e4XGGP1ZWfLrTWEJbavFfhGHut2iMRwfC7s/YILAHNATopaJdH9DNpd1
37
- U81zGHMUBOvz/VGT6wJwYJ3emS2nfA2NOHFfgA==
38
- -----END CERTIFICATE-----
39
-
40
- date: 2012-03-22 00:00:00 Z
41
- dependencies:
42
- - !ruby/object:Gem::Dependency
11
+ cert_chain: []
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
43
15
  name: rdoc
44
- prerelease: false
45
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
46
17
  none: false
47
- requirements:
18
+ requirements:
48
19
  - - ~>
49
- - !ruby/object:Gem::Version
50
- hash: 19
51
- segments:
52
- - 3
53
- - 10
54
- version: "3.10"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
55
22
  type: :development
56
- version_requirements: *id001
57
- - !ruby/object:Gem::Dependency
58
- name: hoe
59
23
  prerelease: false
60
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
61
25
  none: false
62
- requirements:
26
+ requirements:
63
27
  - - ~>
64
- - !ruby/object:Gem::Version
65
- hash: 35
66
- segments:
67
- - 2
68
- - 16
69
- version: "2.16"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.10'
30
+ - !ruby/object:Gem::Dependency
31
+ name: hoe
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
70
38
  type: :development
71
- version_requirements: *id002
72
- description: |-
73
- SyslogLogger is a Logger replacement that logs to syslog. It is almost
74
- drop-in with a few differences.
75
- email:
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ description: ! 'SyslogLogger is a Logger replacement that logs to syslog. It is almost
47
+
48
+ drop-in with a few caveats.'
49
+ email:
76
50
  - drbrain@segment7.net
77
51
  executables: []
78
-
79
52
  extensions: []
80
-
81
- extra_rdoc_files:
53
+ extra_rdoc_files:
82
54
  - History.txt
83
55
  - Manifest.txt
84
56
  - README.txt
85
- files:
57
+ files:
86
58
  - History.txt
87
59
  - Manifest.txt
88
60
  - README.txt
89
61
  - Rakefile
90
62
  - lib/analyzer_tools/syslog_logger.rb
91
- - lib/syslog_logger.rb
63
+ - lib/syslog/logger.rb
92
64
  - test/test_syslog_logger.rb
93
65
  - .gemtest
94
- homepage: http://docs.seattlerb.org/SyslogLogger
66
+ homepage: http://github.com/seattlerb/sysloglogger
95
67
  licenses: []
96
-
97
68
  post_install_message:
98
- rdoc_options:
69
+ rdoc_options:
99
70
  - --main
100
71
  - README.txt
101
- require_paths:
72
+ require_paths:
102
73
  - lib
103
- required_ruby_version: !ruby/object:Gem::Requirement
74
+ required_ruby_version: !ruby/object:Gem::Requirement
104
75
  none: false
105
- requirements:
106
- - - ">="
107
- - !ruby/object:Gem::Version
108
- hash: 3
109
- segments:
110
- - 0
111
- version: "0"
112
- required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
81
  none: false
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- hash: 3
118
- segments:
119
- - 0
120
- version: "0"
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
121
86
  requirements: []
122
-
123
- rubyforge_project: sysloglogger
124
- rubygems_version: 1.8.21
87
+ rubyforge_project: seattlerb
88
+ rubygems_version: 1.8.24
125
89
  signing_key:
126
90
  specification_version: 3
127
91
  summary: SyslogLogger is a Logger replacement that logs to syslog
128
- test_files:
92
+ test_files:
129
93
  - test/test_syslog_logger.rb
data.tar.gz.sig DELETED
@@ -1,4 +0,0 @@
1
- (P0��(����d$Nތ�$�A������Zj T��V�R�+�
2
- y3�U���-�& -��B�sw�E��(8H� hks�v0��dž��8_�PSM���"��"�Ļ��$��
3
- 4�X_���z$~]�I
4
- Cމ�S�זN'�[�P�ia�+W���3�B;�X),0DSf7�U���h��PS�@5�wjke��xa�^��8���1)P�m@""����›�P�^$(>�q(�{�s������!�WCFU�=@� 6���5t~
@@ -1,230 +0,0 @@
1
- require 'syslog'
2
- require 'logger'
3
-
4
- ##
5
- # SyslogLogger is a Logger work-alike that logs via syslog instead of to a
6
- # file. You can add SyslogLogger to your Rails production environment to
7
- # aggregate logs between multiple machines.
8
- #
9
- # By default, SyslogLogger uses the program name 'rails', but this can be
10
- # changed via the first argument to SyslogLogger.new.
11
- #
12
- # NOTE! You can only set the SyslogLogger program name when you initialize
13
- # SyslogLogger for the first time. This is a limitation of the way
14
- # SyslogLogger uses syslog (and in some ways, a limitation of the way
15
- # syslog(3) works). Attempts to change SyslogLogger's program name after the
16
- # first initialization will be ignored.
17
- #
18
- # = Sample usage with Rails
19
- #
20
- # == config/environment/production.rb
21
- #
22
- # Add the following lines:
23
- #
24
- # require 'syslog_logger'
25
- # RAILS_DEFAULT_LOGGER = SyslogLogger.new
26
- #
27
- # == config/environment.rb
28
- #
29
- # In 0.10.0, change this line:
30
- #
31
- # RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log")
32
- #
33
- # to:
34
- #
35
- # RAILS_DEFAULT_LOGGER ||= Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log")
36
- #
37
- # Other versions of Rails should have a similar change.
38
- #
39
- # == BSD syslog setup
40
- #
41
- # === /etc/syslog.conf
42
- #
43
- # Add the following lines:
44
- #
45
- # !rails
46
- # *.* /var/log/production.log
47
- #
48
- # Then touch /var/log/production.log and signal syslogd with a HUP
49
- # (killall -HUP syslogd, on FreeBSD).
50
- #
51
- # === /etc/newsyslog.conf
52
- #
53
- # Add the following line:
54
- #
55
- # /var/log/production.log 640 7 * @T00 Z
56
- #
57
- # This creates a log file that is rotated every day at midnight, gzip'd, then
58
- # kept for 7 days. Consult newsyslog.conf(5) for more details.
59
- #
60
- # == syslog-ng setup
61
- #
62
- # === syslog-ng.conf
63
- #
64
- # destination rails_log { file("/var/log/production.log"); };
65
- # filter f_rails { program("rails.*"); };
66
- # log { source(src); filter(f_rails); destination(rails_log); };
67
- #
68
- # == Starting
69
- #
70
- # Now restart your Rails app. Your production logs should now be showing up
71
- # in /var/log/production.log. If you have mulitple machines, you can log them
72
- # all to a central machine with remote syslog logging for analysis. Consult
73
- # your syslogd(8) manpage for further details.
74
-
75
- class SyslogLogger
76
-
77
- ##
78
- # The version of SyslogLogger you are using.
79
-
80
- VERSION = '1.4.1'
81
-
82
- ##
83
- # Maps Logger warning types to syslog(3) warning types.
84
- #
85
- # Messages from ruby applications are not considered as critical as messages
86
- # from other processes using syslog(3), so most messages are reduced by one
87
- # level. For example, a fatal message for ruby's Logger is considered an
88
- # error for syslog(3).
89
-
90
- LOGGER_MAP = {
91
- :unknown => :alert,
92
- :fatal => :err,
93
- :error => :warning,
94
- :warn => :notice,
95
- :info => :info,
96
- :debug => :debug,
97
- }
98
-
99
- ##
100
- # Maps Logger log levels to their values so we can silence.
101
-
102
- LOGGER_LEVEL_MAP = {}
103
-
104
- LOGGER_MAP.each_key do |key|
105
- LOGGER_LEVEL_MAP[key] = Logger.const_get key.to_s.upcase
106
- end
107
-
108
- ##
109
- # Maps Logger log level values to syslog log levels.
110
-
111
- LEVEL_LOGGER_MAP = {}
112
-
113
- LOGGER_LEVEL_MAP.invert.each do |level, severity|
114
- LEVEL_LOGGER_MAP[level] = LOGGER_MAP[severity]
115
- end
116
-
117
- ##
118
- # Builds a methods for level +meth+.
119
-
120
- def self.make_methods(meth)
121
- eval <<-EOM, nil, __FILE__, __LINE__ + 1
122
- def #{meth}(message = nil)
123
- return true if #{LOGGER_LEVEL_MAP[meth]} < @level
124
- SYSLOG.#{LOGGER_MAP[meth]} clean(message || yield)
125
- return true
126
- end
127
-
128
- def #{meth}?
129
- @level <= Logger::#{meth.to_s.upcase}
130
- end
131
- EOM
132
- end
133
-
134
- ##
135
- # :method: unknown
136
- #
137
- # Logs a +message+ at the unknown (syslog alert) log level, or logs the
138
- # message returned from the block.
139
-
140
- ##
141
- # :method: fatal
142
- #
143
- # Logs a +message+ at the fatal (syslog err) log level, or logs the message
144
- # returned from the block.
145
-
146
- ##
147
- # :method: error
148
- #
149
- # Logs a +message+ at the error (syslog warning) log level, or logs the
150
- # message returned from the block.
151
-
152
- ##
153
- # :method: warn
154
- #
155
- # Logs a +message+ at the warn (syslog notice) log level, or logs the
156
- # message returned from the block.
157
-
158
- ##
159
- # :method: info
160
- #
161
- # Logs a +message+ at the info (syslog info) log level, or logs the message
162
- # returned from the block.
163
-
164
- ##
165
- # :method: debug
166
- #
167
- # Logs a +message+ at the debug (syslog debug) log level, or logs the
168
- # message returned from the block.
169
-
170
- LOGGER_MAP.each_key do |level|
171
- make_methods level
172
- end
173
-
174
- ##
175
- # Log level for Logger compatibility.
176
-
177
- attr_accessor :level
178
-
179
- ##
180
- # Fills in variables for Logger compatibility. If this is the first
181
- # instance of SyslogLogger, +program_name+ may be set to change the logged
182
- # program name.
183
- #
184
- # Due to the way syslog works, only one program name may be chosen.
185
-
186
- def initialize(program_name = 'rails')
187
- @level = Logger::DEBUG
188
-
189
- return if defined? SYSLOG
190
- self.class.const_set :SYSLOG, Syslog.open(program_name)
191
- end
192
-
193
- ##
194
- # Almost duplicates Logger#add. +progname+ is ignored.
195
-
196
- def add(severity, message = nil, progname = nil, &block)
197
- severity ||= Logger::UNKNOWN
198
- return true if severity < @level
199
- SYSLOG.send LEVEL_LOGGER_MAP[severity], clean(message || block.call)
200
- return true
201
- end
202
-
203
- ##
204
- # Allows messages of a particular log level to be ignored temporarily.
205
- #
206
- # Can you say "Broken Windows"?
207
-
208
- def silence(temporary_level = Logger::ERROR)
209
- old_logger_level = @level
210
- @level = temporary_level
211
- yield
212
- ensure
213
- @level = old_logger_level
214
- end
215
-
216
- private
217
-
218
- ##
219
- # Clean up messages so they're nice and pretty.
220
-
221
- def clean(message)
222
- message = message.to_s.dup
223
- message.strip!
224
- message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
225
- message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
226
- return message
227
- end
228
-
229
- end
230
-
metadata.gz.sig DELETED
Binary file