buffered_syslogger 0.1.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.
- data/LICENSE +21 -0
- data/README.rdoc +30 -0
- data/lib/buffered_syslogger.rb +85 -0
- data/spec/buffered_syslogger_spec.rb +82 -0
- data/spec/spec_helper.rb +3 -0
- metadata +111 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 László Bácsi, Secret Sauce Partners, Inc.
|
2
|
+
Copyright (c) 2010 Cyril Rohr, INRIA Rennes-Bretagne Atlantique
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
= BufferedSyslogger
|
2
|
+
|
3
|
+
A drop-in replacement for the Rails 3 default BufferedLogger library,
|
4
|
+
that logs to syslog instead of a log file. Builds on the `syslogger` gem.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
Put it into your Gemfile:
|
9
|
+
|
10
|
+
gem 'buffered_syslogger'
|
11
|
+
|
12
|
+
== Usage
|
13
|
+
# Somewhere in you application initialization (e.g. production.rb)
|
14
|
+
# will send all messages to the local0 facility, adding the process id in the message
|
15
|
+
config.logger = BufferedSyslogger.new("app_name", Syslog::LOG_PID, Syslog::LOG_LOCAL0)
|
16
|
+
|
17
|
+
# Later...
|
18
|
+
# Send messages that are at least of the Logger::INFO level
|
19
|
+
Rails.logger.level = Logger::INFO # use Logger levels
|
20
|
+
|
21
|
+
Rails.logger.debug "will not appear"
|
22
|
+
Rails.logger.info "will appear"
|
23
|
+
Rails.logger.warn "will appear"
|
24
|
+
|
25
|
+
== Copyright
|
26
|
+
|
27
|
+
Copyright (c) 2010 László Bács, Secret Sauce Partners, Inc.
|
28
|
+
Copyright (c) 2010 Cyril, INRIA Rennes-Bretagne Atlantique.
|
29
|
+
|
30
|
+
See LICENSE for details.
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'syslog'
|
2
|
+
require 'logger'
|
3
|
+
require 'active_support/buffered_logger'
|
4
|
+
|
5
|
+
class BufferedSyslogger < ActiveSupport::BufferedLogger
|
6
|
+
attr_reader :ident, :options, :facility
|
7
|
+
|
8
|
+
MAPPING = {
|
9
|
+
Logger::DEBUG => Syslog::LOG_DEBUG,
|
10
|
+
Logger::INFO => Syslog::LOG_INFO,
|
11
|
+
Logger::WARN => Syslog::LOG_NOTICE,
|
12
|
+
Logger::ERROR => Syslog::LOG_WARNING,
|
13
|
+
Logger::FATAL => Syslog::LOG_ERR,
|
14
|
+
Logger::UNKNOWN => Syslog::LOG_ALERT
|
15
|
+
}
|
16
|
+
|
17
|
+
#
|
18
|
+
# Initializes default options for the logger
|
19
|
+
# <tt>ident</tt>:: the name of your program [default=$0].
|
20
|
+
# <tt>options</tt>:: syslog options [default=<tt>Syslog::LOG_PID | Syslog::LOG_CONS</tt>].
|
21
|
+
# Correct values are:
|
22
|
+
# LOG_CONS : writes the message on the console if an error occurs when sending the message;
|
23
|
+
# LOG_NDELAY : no delay before sending the message;
|
24
|
+
# LOG_PERROR : messages will also be written on STDERR;
|
25
|
+
# LOG_PID : adds the process number to the message (just after the program name)
|
26
|
+
# <tt>facility</tt>:: the syslog facility [default=nil] Correct values include:
|
27
|
+
# Syslog::LOG_DAEMON
|
28
|
+
# Syslog::LOG_USER
|
29
|
+
# Syslog::LOG_SYSLOG
|
30
|
+
# Syslog::LOG_LOCAL2
|
31
|
+
# Syslog::LOG_NEWS
|
32
|
+
# etc.
|
33
|
+
#
|
34
|
+
# Usage:
|
35
|
+
# logger = BufferedSyslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
|
36
|
+
# logger.level = Logger::INFO # use Logger levels
|
37
|
+
# logger.warn "warning message"
|
38
|
+
# logger.debug "debug message"
|
39
|
+
#
|
40
|
+
def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
|
41
|
+
@ident = ident
|
42
|
+
@options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
|
43
|
+
@facility = facility
|
44
|
+
|
45
|
+
@level = Logger::INFO
|
46
|
+
@buffer = {}
|
47
|
+
@auto_flushing = 1
|
48
|
+
@guard = Mutex.new
|
49
|
+
end
|
50
|
+
|
51
|
+
# Low level method to add a message.
|
52
|
+
# +severity+:: the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN
|
53
|
+
# +message+:: the message string. If nil, the method will call the block and use the result as the message string.
|
54
|
+
# +progname+:: unsupported, kept for compatibility
|
55
|
+
def add(severity, message = nil, progname = nil, &block)
|
56
|
+
return if @level > severity
|
57
|
+
(message || (block && block.call) || progname).to_s.chomp.tap do |m|
|
58
|
+
buffer << [severity, m]
|
59
|
+
auto_flush
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def flush
|
64
|
+
@guard.synchronize do
|
65
|
+
unless buffer.empty?
|
66
|
+
old_buffer = buffer
|
67
|
+
Syslog.open(@ident, @options, @facility) do |s|
|
68
|
+
s.mask = Syslog::LOG_UPTO(MAPPING[@level])
|
69
|
+
old_buffer.each do |severity, message|
|
70
|
+
message.split(/[\r\f\n]/).each { |m| s.log(MAPPING[severity], m) }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Important to do this even if buffer was empty or else @buffer will
|
76
|
+
# accumulate empty arrays for each request where nothing was logged.
|
77
|
+
clear_buffer
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def close
|
82
|
+
flush
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe BufferedSyslogger do
|
4
|
+
|
5
|
+
let(:syslog) { mock("syslog", :mask= => true) }
|
6
|
+
|
7
|
+
it "should log to the default syslog facility, with the default options" do
|
8
|
+
logger = BufferedSyslogger.new
|
9
|
+
Syslog.should_receive(:open).with($0, Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(syslog)
|
10
|
+
syslog.should_receive(:log).with(Syslog::LOG_NOTICE, "Some message")
|
11
|
+
logger.warn "Some message"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should log to the user facility, with specific options" do
|
15
|
+
logger = BufferedSyslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
|
16
|
+
Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog)
|
17
|
+
syslog.should_receive(:log).with(Syslog::LOG_NOTICE, "Some message")
|
18
|
+
logger.warn "Some message"
|
19
|
+
end
|
20
|
+
|
21
|
+
%w{debug info warn error fatal unknown}.each do |logger_method|
|
22
|
+
it "should respond to the #{logger_method.inspect} method" do
|
23
|
+
BufferedSyslogger.new.should respond_to logger_method.to_sym
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
%w{debug info warn error fatal unknown}.each do |logger_method|
|
28
|
+
it "should respond to the #{logger_method.inspect}? method" do
|
29
|
+
BufferedSyslogger.new.should respond_to "#{logger_method}?".to_sym
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "add" do
|
34
|
+
before do
|
35
|
+
@logger = BufferedSyslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
|
36
|
+
end
|
37
|
+
it "should respond to add" do
|
38
|
+
@logger.should respond_to(:add)
|
39
|
+
end
|
40
|
+
it "should correctly log" do
|
41
|
+
Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog)
|
42
|
+
syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
|
43
|
+
@logger.add(Logger::INFO, "message")
|
44
|
+
end
|
45
|
+
it "should take the message from the block if :message is nil" do
|
46
|
+
Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog)
|
47
|
+
syslog.should_receive(:log).with(Syslog::LOG_INFO, "my message")
|
48
|
+
@logger.add(Logger::INFO) { "my message" }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when used in buffered mode" do
|
53
|
+
before { subject.auto_flushing = false }
|
54
|
+
|
55
|
+
it "without flushing should not send anything to syslog" do
|
56
|
+
Syslog.should_not_receive(:open)
|
57
|
+
subject.warn "Self destruction in progress..."
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should send the messages to syslog when flushed" do
|
61
|
+
Syslog.should_receive(:open).and_yield(syslog)
|
62
|
+
syslog.should_receive(:log).with(Syslog::LOG_INFO, "Self destruction in 3...")
|
63
|
+
syslog.should_receive(:log).with(Syslog::LOG_NOTICE, "2..")
|
64
|
+
syslog.should_receive(:log).with(Syslog::LOG_WARNING, "1.")
|
65
|
+
subject.info "Self destruction in 3..."
|
66
|
+
subject.warn "2.."
|
67
|
+
subject.error "1."
|
68
|
+
subject.flush
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should split messages with multiple lines and send separately" do
|
72
|
+
Syslog.should_receive(:open).and_yield(syslog)
|
73
|
+
syslog.should_receive(:log).with(Syslog::LOG_WARNING, "Self destruction in 3...")
|
74
|
+
syslog.should_receive(:log).with(Syslog::LOG_WARNING, "2..")
|
75
|
+
syslog.should_receive(:log).with(Syslog::LOG_WARNING, "1.")
|
76
|
+
subject.error "Self destruction in 3...\n2..\n1."
|
77
|
+
subject.flush
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# TODO: test logger level
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: buffered_syslogger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- !binary |
|
14
|
+
TMOhc3psw7MgQsOhY3Np
|
15
|
+
|
16
|
+
- Cyril Rohr
|
17
|
+
autorequire:
|
18
|
+
bindir: bin
|
19
|
+
cert_chain: []
|
20
|
+
|
21
|
+
date: 2010-10-11 00:00:00 +02:00
|
22
|
+
default_executable:
|
23
|
+
dependencies:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: activesupport
|
26
|
+
prerelease: false
|
27
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
hash: 7
|
33
|
+
segments:
|
34
|
+
- 3
|
35
|
+
- 0
|
36
|
+
- 0
|
37
|
+
version: 3.0.0
|
38
|
+
type: :runtime
|
39
|
+
version_requirements: *id001
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: rspec
|
42
|
+
prerelease: false
|
43
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 15
|
49
|
+
segments:
|
50
|
+
- 2
|
51
|
+
- 0
|
52
|
+
- 0
|
53
|
+
version: 2.0.0
|
54
|
+
type: :development
|
55
|
+
version_requirements: *id002
|
56
|
+
description: |
|
57
|
+
A drop-in replacement for the Rails 3 default BufferedLogger library,
|
58
|
+
that logs to syslog instead of a log file. Builds on the syslogger gem.
|
59
|
+
|
60
|
+
email: lackac@secretsaucepartners.com
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
66
|
+
- LICENSE
|
67
|
+
- README.rdoc
|
68
|
+
files:
|
69
|
+
- lib/buffered_syslogger.rb
|
70
|
+
- spec/buffered_syslogger_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- README.rdoc
|
73
|
+
- LICENSE
|
74
|
+
has_rdoc: true
|
75
|
+
homepage: http://github.com/sspinc/buffered_syslogger
|
76
|
+
licenses: []
|
77
|
+
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options:
|
80
|
+
- --charset=UTF-8
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 23
|
98
|
+
segments:
|
99
|
+
- 1
|
100
|
+
- 3
|
101
|
+
- 6
|
102
|
+
version: 1.3.6
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project: nowarning
|
106
|
+
rubygems_version: 1.3.7
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Buffered syslogger based on the syslogger gem.
|
110
|
+
test_files: []
|
111
|
+
|