le1t0-syslogger 1.2.4.001

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,48 @@
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
+ Documentation available at <http://rdoc.info/github/crohr/syslogger/master/file/README.rdoc>.
23
+
24
+ == Development
25
+ * Install +bundler+:
26
+
27
+ $ gem install bundler
28
+
29
+ * Install development dependencies:
30
+
31
+ $ bundle install
32
+
33
+ * Run tests:
34
+
35
+ $ bundle exec rake
36
+
37
+ * Package (do not forget to increment <tt>Syslogger:VERSION</tt>):
38
+
39
+ $ gem build syslogger.gemspec
40
+
41
+ == Contributions
42
+ * crhym3
43
+ * theflow
44
+ * smulube
45
+
46
+ == Copyright
47
+
48
+ 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,101 @@
1
+ require 'syslog'
2
+ require 'logger'
3
+
4
+ class Syslogger
5
+
6
+ VERSION = "1.2.4.001"
7
+
8
+ attr_reader :level, :ident, :options, :facility, :prefix, :suffix
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, prefix = nil, suffix = nil)
43
+ @ident = ident
44
+ @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
45
+ @facility = facility
46
+ @level = Logger::INFO
47
+ @prefix = prefix
48
+ @suffix = suffix
49
+ end
50
+
51
+ %w{debug info warn error fatal unknown}.each do |logger_method|
52
+ define_method logger_method.to_sym do |message|
53
+ add(Logger.const_get(logger_method.upcase), message)
54
+ end
55
+
56
+ unless logger_method == 'unknown'
57
+ define_method "#{logger_method}?".to_sym do
58
+ @level <= Logger.const_get(logger_method.upcase)
59
+ end
60
+ end
61
+ end
62
+
63
+ # Logs a message at the Logger::INFO level.
64
+ def <<(msg)
65
+ add(Logger::INFO, msg)
66
+ end
67
+
68
+ # Low level method to add a message.
69
+ # +severity+:: the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN
70
+ # +message+:: the message string.
71
+ # If nil, the method will call the block and use the result as the message string.
72
+ # 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.
73
+ # +progname+:: optionally, overwrite the program name that appears in the log message.
74
+ def add(severity, message = nil, progname = nil, &block)
75
+ progname ||= @ident
76
+ Syslog.open(progname, @options, @facility) { |s|
77
+ s.mask = Syslog::LOG_UPTO(MAPPING[@level])
78
+ s.log(
79
+ MAPPING[severity],
80
+ clean("#{@prefix}#{message || (block && block.call) || progname}#{@suffix}")
81
+ )
82
+ }
83
+ end
84
+
85
+ # Sets the minimum level for messages to be written in the log.
86
+ # +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>
87
+ def level=(level)
88
+ @level = level
89
+ end
90
+
91
+ protected
92
+
93
+ # Borrowed from SyslogLogger.
94
+ def clean(message)
95
+ message = message.to_s.dup
96
+ message.strip!
97
+ message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
98
+ message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
99
+ message
100
+ end
101
+ 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,145 @@
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
+
61
+ it "should strip the :message" do
62
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
63
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
64
+ @logger.add(Logger::INFO, "\n\nmessage ")
65
+ end
66
+
67
+ it "should not raise exception if asked to log with a nil message and body" do
68
+ Syslog.should_receive(:open).
69
+ with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
70
+ and_yield(syslog=mock("syslog", :mask= => true))
71
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my_app")
72
+ lambda {
73
+ @logger.add(Logger::INFO, nil)
74
+ }.should_not raise_error
75
+ end
76
+
77
+ it "should use given progname as the message if the message and block are nil" do
78
+ Syslog.should_receive(:open).
79
+ with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
80
+ and_yield(syslog=mock("syslog", :mask= => true))
81
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my_app")
82
+ @logger.add(Logger::INFO, nil)
83
+ end
84
+ end # describe "add"
85
+
86
+ describe ":level? methods" do
87
+ before(:each) do
88
+ @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
89
+ end
90
+
91
+ %w{debug info warn error fatal}.each do |logger_method|
92
+ it "should respond to the #{logger_method}? method" do
93
+ @logger.should respond_to "#{logger_method}?".to_sym
94
+ end
95
+ end
96
+
97
+ it "should not have unknown? method" do
98
+ @logger.should_not respond_to :unknown?
99
+ end
100
+
101
+ it "should return true for all methods" do
102
+ @logger.level = Logger::DEBUG
103
+ %w{debug info warn error fatal}.each do |logger_method|
104
+ @logger.send("#{logger_method}?").should be_true
105
+ end
106
+ end
107
+
108
+ it "should return true for all except debug?" do
109
+ @logger.level = Logger::INFO
110
+ %w{info warn error fatal}.each do |logger_method|
111
+ @logger.send("#{logger_method}?").should be_true
112
+ end
113
+ @logger.debug?.should be_false
114
+ end
115
+
116
+ it "should return true for warn?, error? and fatal? when WARN" do
117
+ @logger.level = Logger::WARN
118
+ %w{warn error fatal}.each do |logger_method|
119
+ @logger.send("#{logger_method}?").should be_true
120
+ end
121
+ %w{debug info}.each do |logger_method|
122
+ @logger.send("#{logger_method}?").should be_false
123
+ end
124
+ end
125
+
126
+ it "should return true for error? and fatal? when ERROR" do
127
+ @logger.level = Logger::ERROR
128
+ %w{error fatal}.each do |logger_method|
129
+ @logger.send("#{logger_method}?").should be_true
130
+ end
131
+ %w{warn debug info}.each do |logger_method|
132
+ @logger.send("#{logger_method}?").should be_false
133
+ end
134
+ end
135
+
136
+ it "should return true only for fatal? when FATAL" do
137
+ @logger.level = Logger::FATAL
138
+ @logger.fatal?.should be_true
139
+ %w{error warn debug info}.each do |logger_method|
140
+ @logger.send("#{logger_method}?").should be_false
141
+ end
142
+ end
143
+ end # describe ":level? methods"
144
+
145
+ end # describe "Syslogger"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: le1t0-syslogger
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.2.4.001
6
+ platform: ruby
7
+ authors:
8
+ - Le1t0
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-29 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rake
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "0.8"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "1.3"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: Same as SyslogLogger, but without the ridiculous number of dependencies and with the possibility to specify the syslog facility
39
+ email:
40
+ - dev@ewout.to
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - lib/syslogger.rb
49
+ - spec/spec_helper.rb
50
+ - spec/syslogger_spec.rb
51
+ - Rakefile
52
+ - LICENSE
53
+ - README.rdoc
54
+ has_rdoc: true
55
+ homepage: http://github.com/le1t0/syslogger
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "1.8"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "1.3"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.5.2
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Dead simple Ruby Syslog logger
82
+ test_files:
83
+ - spec/spec_helper.rb
84
+ - spec/syslogger_spec.rb