spindance-syslogger 1.2.7

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 ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Cyril Rohr, INRIA Rennes-Bretagne Atlantique
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,50 @@
1
+ = syslogger
2
+
3
+ A drop-in replacement for the standard Logger Ruby library, that logs to the syslog instead of a log file.
4
+ Contrary to the SyslogLogger library, you can specify the facility and the syslog options.
5
+
6
+ This fork takes care of log messages spanning across multiple lines. Like in a Rails backtrace.
7
+
8
+ == Installation
9
+ $ gem install syslogger
10
+
11
+ == Usage
12
+ require 'syslogger'
13
+
14
+ # Will send all messages to the local0 facility, adding the process id in the message
15
+ logger = Syslogger.new("app_name", Syslog::LOG_PID, Syslog::LOG_LOCAL0)
16
+
17
+ # Send messages that are at least of the Logger::INFO level
18
+ logger.level = Logger::INFO # use Logger levels
19
+
20
+ logger.debug "will not appear"
21
+ logger.info "will appear"
22
+ logger.warn "will appear"
23
+
24
+ Documentation available at <http://rdoc.info/github/crohr/syslogger/master/file/README.rdoc>.
25
+
26
+ == Development
27
+ * Install +bundler+:
28
+
29
+ $ gem install bundler
30
+
31
+ * Install development dependencies:
32
+
33
+ $ bundle install
34
+
35
+ * Run tests:
36
+
37
+ $ bundle exec rake
38
+
39
+ * Package (do not forget to increment <tt>Syslogger:VERSION</tt>):
40
+
41
+ $ gem build syslogger.gemspec
42
+
43
+ == Contributions
44
+ * crhym3
45
+ * theflow
46
+ * smulube
47
+
48
+ == Copyright
49
+
50
+ Copyright (c) 2010 Cyril, INRIA Rennes-Bretagne Atlantique. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'spec/rake/spectask'
2
+ require 'rake/rdoctask'
3
+
4
+ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
5
+
6
+ Spec::Rake::SpecTask.new(:spec) do |spec|
7
+ spec.libs << 'lib' << 'spec'
8
+ spec.spec_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
12
+ spec.libs << 'lib' << 'spec'
13
+ spec.pattern = 'spec/**/*_spec.rb'
14
+ spec.rcov = true
15
+ end
16
+
17
+ Rake::RDocTask.new do |rdoc|
18
+ require 'syslogger'
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = "syslogger #{Syslogger::VERSION}"
21
+ rdoc.rdoc_files.include('README*')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ task :default => :spec
data/lib/syslogger.rb ADDED
@@ -0,0 +1,95 @@
1
+ require 'syslog'
2
+ require 'logger'
3
+
4
+ class Syslogger
5
+
6
+ VERSION = "1.2.7"
7
+
8
+ attr_reader :level, :ident, :options, :facility
9
+
10
+ MAPPING = {
11
+ Logger::DEBUG => Syslog::LOG_DEBUG,
12
+ Logger::INFO => Syslog::LOG_INFO,
13
+ Logger::WARN => Syslog::LOG_NOTICE,
14
+ Logger::ERROR => Syslog::LOG_WARNING,
15
+ Logger::FATAL => Syslog::LOG_ERR,
16
+ Logger::UNKNOWN => Syslog::LOG_ALERT
17
+ }
18
+
19
+ #
20
+ # Initializes default options for the logger
21
+ # <tt>ident</tt>:: the name of your program [default=$0].
22
+ # <tt>options</tt>:: syslog options [default=<tt>Syslog::LOG_PID | Syslog::LOG_CONS</tt>].
23
+ # Correct values are:
24
+ # LOG_CONS : writes the message on the console if an error occurs when sending the message;
25
+ # LOG_NDELAY : no delay before sending the message;
26
+ # LOG_PERROR : messages will also be written on STDERR;
27
+ # LOG_PID : adds the process number to the message (just after the program name)
28
+ # <tt>facility</tt>:: the syslog facility [default=nil] Correct values include:
29
+ # Syslog::LOG_DAEMON
30
+ # Syslog::LOG_USER
31
+ # Syslog::LOG_SYSLOG
32
+ # Syslog::LOG_LOCAL2
33
+ # Syslog::LOG_NEWS
34
+ # etc.
35
+ #
36
+ # Usage:
37
+ # logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
38
+ # logger.level = Logger::INFO # use Logger levels
39
+ # logger.warn "warning message"
40
+ # logger.debug "debug message"
41
+ #
42
+ def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
43
+ @ident = ident
44
+ @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
45
+ @facility = facility
46
+ @level = Logger::INFO
47
+ end
48
+
49
+ %w{debug info warn error fatal unknown}.each do |logger_method|
50
+ define_method logger_method.to_sym do |message|
51
+ add(Logger.const_get(logger_method.upcase), message)
52
+ end
53
+
54
+ unless logger_method == 'unknown'
55
+ define_method "#{logger_method}?".to_sym do
56
+ @level <= Logger.const_get(logger_method.upcase)
57
+ end
58
+ end
59
+ end
60
+
61
+ # Logs a message at the Logger::INFO level.
62
+ def <<(msg)
63
+ add(Logger::INFO, msg)
64
+ end
65
+
66
+ # Low level method to add a message.
67
+ # +severity+:: the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN
68
+ # +message+:: the message string.
69
+ # If nil, the method will call the block and use the result as the message string.
70
+ # If both are nil or no block is given, it will use the progname as per the behaviour of both the standard Ruby logger, and the Rails BufferedLogger.
71
+ # +progname+:: optionally, overwrite the program name that appears in the log message.
72
+ def add(severity, message = nil, progname = nil, &block)
73
+ progname ||= @ident
74
+ Syslog.open(progname, @options, @facility) { |s|
75
+ s.mask = Syslog::LOG_UPTO(MAPPING[@level])
76
+ s.log(
77
+ MAPPING[severity],
78
+ clean(message || (block && block.call) || progname)
79
+ )
80
+ }
81
+ end
82
+
83
+ # Sets the minimum level for messages to be written in the log.
84
+ # +level+:: one of <tt>Logger::DEBUG</tt>, <tt>Logger::INFO</tt>, <tt>Logger::WARN</tt>, <tt>Logger::ERROR</tt>, <tt>Logger::FATAL</tt>, <tt>Logger::UNKNOWN</tt>
85
+ def level=(level)
86
+ @level = level
87
+ end
88
+
89
+ protected
90
+
91
+ # Borrowed from SyslogLogger.
92
+ def clean(message)
93
+ message.to_s.each_line.map(&:strip).join(' >> ').gsub(/%/, '%%').gsub(/\e\[[^m]*m/, '')
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'syslogger'
@@ -0,0 +1,166 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Syslogger" do
4
+ it "should log to the default syslog facility, with the default options" do
5
+ logger = Syslogger.new
6
+ Syslog.should_receive(:open).with($0, Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(syslog=mock("syslog", :mask= => true))
7
+ syslog.should_receive(:log).with(Syslog::LOG_NOTICE, "Some message")
8
+ logger.warn "Some message"
9
+ end
10
+
11
+ it "should log to the user facility, with specific options" do
12
+ logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
13
+ Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
14
+ syslog.should_receive(:log).with(Syslog::LOG_NOTICE, "Some message")
15
+ logger.warn "Some message"
16
+ end
17
+
18
+ %w{debug info warn error fatal unknown}.each do |logger_method|
19
+ it "should respond to the #{logger_method.inspect} method" do
20
+ Syslogger.new.should respond_to logger_method.to_sym
21
+ end
22
+ end
23
+
24
+ it "should respond to <<" do
25
+ logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
26
+ logger.should respond_to(:<<)
27
+ Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
28
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "yop")
29
+ logger << "yop"
30
+ end
31
+
32
+ describe "add" do
33
+ before do
34
+ @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
35
+ end
36
+ it "should respond to add" do
37
+ @logger.should respond_to(:add)
38
+ end
39
+ it "should correctly log" do
40
+ Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
41
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
42
+ @logger.add(Logger::INFO, "message")
43
+ end
44
+ it "should take the message from the block if :message is nil" do
45
+ Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
46
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my message")
47
+ @logger.add(Logger::INFO) { "my message" }
48
+ end
49
+ it "should use the given progname" do
50
+ Syslog.should_receive(:open).with("progname", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
51
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
52
+ @logger.add(Logger::INFO, "message", "progname") { "my message" }
53
+ end
54
+
55
+ it "should not raise exception if asked to log with a nil message and body" do
56
+ Syslog.should_receive(:open).
57
+ with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
58
+ and_yield(syslog=mock("syslog", :mask= => true))
59
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my_app")
60
+ lambda {
61
+ @logger.add(Logger::INFO, nil)
62
+ }.should_not raise_error
63
+ end
64
+
65
+ it "should use given progname as the message if the message and block are nil" do
66
+ Syslog.should_receive(:open).
67
+ with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
68
+ and_yield(syslog=mock("syslog", :mask= => true))
69
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my_app")
70
+ @logger.add(Logger::INFO, nil)
71
+ end
72
+
73
+ context "message cleaner" do
74
+ it "should substitute '%' for '%%' before adding the :message" do
75
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
76
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "%%me%%ssage%%")
77
+ @logger.add(Logger::INFO, "%me%ssage%")
78
+ end
79
+
80
+ it "should strip the :message" do
81
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
82
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
83
+ @logger.add(Logger::INFO, " message ")
84
+ end
85
+
86
+ it "should glue together all newlines before adding the :message" do
87
+ backtrace = <<-EM
88
+ A FooException has been raised
89
+ And here
90
+ is your
91
+ really long
92
+ backtrace...
93
+ EM
94
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
95
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "A FooException has been raised >> And here >> is your >> really long >> backtrace...")
96
+ @logger.add(Logger::INFO, backtrace)
97
+ end
98
+
99
+ it "should stringify if an Exception is received" do
100
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
101
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "Invalidity!")
102
+ @logger.add(Logger::INFO, Exception.new("Invalidity!"))
103
+ end
104
+ end
105
+ end # describe "add"
106
+
107
+ describe ":level? methods" do
108
+ before(:each) do
109
+ @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
110
+ end
111
+
112
+ %w{debug info warn error fatal}.each do |logger_method|
113
+ it "should respond to the #{logger_method}? method" do
114
+ @logger.should respond_to "#{logger_method}?".to_sym
115
+ end
116
+ end
117
+
118
+ it "should not have unknown? method" do
119
+ @logger.should_not respond_to :unknown?
120
+ end
121
+
122
+ it "should return true for all methods" do
123
+ @logger.level = Logger::DEBUG
124
+ %w{debug info warn error fatal}.each do |logger_method|
125
+ @logger.send("#{logger_method}?").should be_true
126
+ end
127
+ end
128
+
129
+ it "should return true for all except debug?" do
130
+ @logger.level = Logger::INFO
131
+ %w{info warn error fatal}.each do |logger_method|
132
+ @logger.send("#{logger_method}?").should be_true
133
+ end
134
+ @logger.debug?.should be_false
135
+ end
136
+
137
+ it "should return true for warn?, error? and fatal? when WARN" do
138
+ @logger.level = Logger::WARN
139
+ %w{warn error fatal}.each do |logger_method|
140
+ @logger.send("#{logger_method}?").should be_true
141
+ end
142
+ %w{debug info}.each do |logger_method|
143
+ @logger.send("#{logger_method}?").should be_false
144
+ end
145
+ end
146
+
147
+ it "should return true for error? and fatal? when ERROR" do
148
+ @logger.level = Logger::ERROR
149
+ %w{error fatal}.each do |logger_method|
150
+ @logger.send("#{logger_method}?").should be_true
151
+ end
152
+ %w{warn debug info}.each do |logger_method|
153
+ @logger.send("#{logger_method}?").should be_false
154
+ end
155
+ end
156
+
157
+ it "should return true only for fatal? when FATAL" do
158
+ @logger.level = Logger::FATAL
159
+ @logger.fatal?.should be_true
160
+ %w{error warn debug info}.each do |logger_method|
161
+ @logger.send("#{logger_method}?").should be_false
162
+ end
163
+ end
164
+ end # describe ":level? methods"
165
+
166
+ end # describe "Syslogger"
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spindance-syslogger
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 7
10
+ version: 1.2.7
11
+ platform: ruby
12
+ authors:
13
+ - Cyril Rohr
14
+ - SpinDance, Inc.
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-06-21 00:00:00 -04:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rake
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 27
31
+ segments:
32
+ - 0
33
+ - 8
34
+ version: "0.8"
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 9
46
+ segments:
47
+ - 1
48
+ - 3
49
+ version: "1.3"
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: Same as SyslogLogger, but without the ridiculous number of dependencies and with the possibility to specify the syslog facility
53
+ email:
54
+ - rubygems@spindance.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - lib/syslogger.rb
63
+ - spec/spec_helper.rb
64
+ - spec/syslogger_spec.rb
65
+ - Rakefile
66
+ - LICENSE
67
+ - README.rdoc
68
+ has_rdoc: true
69
+ homepage: http://github.com/dvangeest/syslogger
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 31
83
+ segments:
84
+ - 1
85
+ - 8
86
+ version: "1.8"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 9
93
+ segments:
94
+ - 1
95
+ - 3
96
+ version: "1.3"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.4.2
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Dead simple Ruby Syslog logger
104
+ test_files:
105
+ - spec/spec_helper.rb
106
+ - spec/syslogger_spec.rb