syslogger-alex 1.2.2

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,44 @@
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
+ == Installation
7
+ $ gem install syslogger
8
+
9
+ == Usage
10
+ require 'syslogger'
11
+
12
+ # Will send all messages to the local0 facility, adding the process id in the message
13
+ logger = Syslogger.new("app_name", Syslog::LOG_PID, Syslog::LOG_LOCAL0)
14
+
15
+ # Send messages that are at least of the Logger::INFO level
16
+ logger.level = Logger::INFO # use Logger levels
17
+
18
+ logger.debug "will not appear"
19
+ logger.info "will appear"
20
+ logger.warn "will appear"
21
+
22
+ == Development
23
+ * Install +bundler+:
24
+
25
+ $ gem install bundler
26
+
27
+ * Install development dependencies:
28
+
29
+ $ bundle install
30
+
31
+ * Run tests:
32
+
33
+ $ bundle exec rake
34
+
35
+ * Package (do not forget to increment <tt>Syslogger:VERSION</tt>):
36
+
37
+ $ gem build syslogger.gemspec
38
+
39
+ == Contributions
40
+ * crhym3
41
+
42
+ == Copyright
43
+
44
+ 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,85 @@
1
+ require 'syslog'
2
+ require 'logger'
3
+
4
+ class Syslogger
5
+
6
+ VERSION = "1.2.2"
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. If nil, the method will call the block and use the result as the message string.
69
+ # +progname+:: optionally, a overwrite the program name that appears in the log message.
70
+ def add(severity, message = nil, progname = nil, &block)
71
+ progname ||= @ident
72
+ Syslog.open(progname, @options, @facility) { |s|
73
+ s.mask = Syslog::LOG_UPTO(MAPPING[@level])
74
+ # substitute '%' for '%%' before logging
75
+ # so that syslog won't complain abut malformed characters
76
+ s.log(MAPPING[severity], (message || block.call).to_s.gsub(/%/, '%%'))
77
+ }
78
+ end
79
+
80
+ # Sets the minimum level for messages to be written in the log.
81
+ # +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>
82
+ def level=(level)
83
+ @level = level
84
+ end
85
+ 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,121 @@
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 substitute '%' for '%%' before adding the :message" do
56
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
57
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "%%me%%ssage%%")
58
+ @logger.add(Logger::INFO, "%me%ssage%")
59
+ end
60
+ end # describe "add"
61
+
62
+ describe ":level? methods" do
63
+ before(:each) do
64
+ @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
65
+ end
66
+
67
+ %w{debug info warn error fatal}.each do |logger_method|
68
+ it "should respond to the #{logger_method}? method" do
69
+ @logger.should respond_to "#{logger_method}?".to_sym
70
+ end
71
+ end
72
+
73
+ it "should not have unknown? method" do
74
+ @logger.should_not respond_to :unknown?
75
+ end
76
+
77
+ it "should return true for all methods" do
78
+ @logger.level = Logger::DEBUG
79
+ %w{debug info warn error fatal}.each do |logger_method|
80
+ @logger.send("#{logger_method}?").should be_true
81
+ end
82
+ end
83
+
84
+ it "should return true for all except debug?" do
85
+ @logger.level = Logger::INFO
86
+ %w{info warn error fatal}.each do |logger_method|
87
+ @logger.send("#{logger_method}?").should be_true
88
+ end
89
+ @logger.debug?.should be_false
90
+ end
91
+
92
+ it "should return true for warn?, error? and fatal? when WARN" do
93
+ @logger.level = Logger::WARN
94
+ %w{warn error fatal}.each do |logger_method|
95
+ @logger.send("#{logger_method}?").should be_true
96
+ end
97
+ %w{debug info}.each do |logger_method|
98
+ @logger.send("#{logger_method}?").should be_false
99
+ end
100
+ end
101
+
102
+ it "should return true for error? and fatal? when ERROR" do
103
+ @logger.level = Logger::ERROR
104
+ %w{error fatal}.each do |logger_method|
105
+ @logger.send("#{logger_method}?").should be_true
106
+ end
107
+ %w{warn debug info}.each do |logger_method|
108
+ @logger.send("#{logger_method}?").should be_false
109
+ end
110
+ end
111
+
112
+ it "should return true only for fatal? when FATAL" do
113
+ @logger.level = Logger::FATAL
114
+ @logger.fatal?.should be_true
115
+ %w{error warn debug info}.each do |logger_method|
116
+ @logger.send("#{logger_method}?").should be_false
117
+ end
118
+ end
119
+ end # describe ":level? methods"
120
+
121
+ end # describe "Syslogger"
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syslogger-alex
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 2
10
+ version: 1.2.2
11
+ platform: ruby
12
+ authors:
13
+ - Cyril Rohr
14
+ - alex
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-01-06 00:00:00 +01: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
+ - cyril.rohr@gmail.com
55
+ - crhym3@gmail.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - lib/syslogger.rb
64
+ - spec/spec_helper.rb
65
+ - spec/syslogger_spec.rb
66
+ - Rakefile
67
+ - LICENSE
68
+ - README.rdoc
69
+ has_rdoc: true
70
+ homepage: http://github.com/crhym3/syslogger
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 31
84
+ segments:
85
+ - 1
86
+ - 8
87
+ version: "1.8"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 9
94
+ segments:
95
+ - 1
96
+ - 3
97
+ version: "1.3"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.7
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Dead simple Ruby Syslog logger
105
+ test_files:
106
+ - spec/spec_helper.rb
107
+ - spec/syslogger_spec.rb