sayso-syslogger 1.2.4.001

Sign up to get free protection for your applications and to get access to all the features.
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.
@@ -0,0 +1,57 @@
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
+ # Log all messages with given prefix. Might be combined with middleware to
16
+ # have request information in each log message.
17
+ # See https://gist.github.com/892489 for middleware example.
18
+ logger.prefix = '[PREFIX]'
19
+
20
+ # Send messages that are at least of the Logger::INFO level
21
+ logger.level = Logger::INFO # use Logger levels
22
+
23
+ # Logs all messages with given prefix, useful to have e.g. request ip
24
+ # See https://gist.github.com/892489 for middleware example
25
+ logger.prefix = Logger::INFO # use Logger levels
26
+
27
+ logger.debug "will not appear"
28
+ logger.info "will appear"
29
+ logger.warn "will appear"
30
+
31
+ Documentation available at <http://rdoc.info/github/crohr/syslogger/master/file/README.rdoc>.
32
+
33
+ == Development
34
+ * Install +bundler+:
35
+
36
+ $ gem install bundler
37
+
38
+ * Install development dependencies:
39
+
40
+ $ bundle install
41
+
42
+ * Run tests:
43
+
44
+ $ bundle exec rake
45
+
46
+ * Package (do not forget to increment <tt>Syslogger:VERSION</tt>):
47
+
48
+ $ gem build syslogger.gemspec
49
+
50
+ == Contributions
51
+ * crhym3
52
+ * theflow
53
+ * smulube
54
+
55
+ == Copyright
56
+
57
+ Copyright (c) 2010 Cyril, INRIA Rennes-Bretagne Atlantique. See LICENSE for details.
@@ -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
@@ -0,0 +1,104 @@
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
9
+
10
+ attr_accessor :prefix
11
+
12
+ MAPPING = {
13
+ Logger::DEBUG => Syslog::LOG_DEBUG,
14
+ Logger::INFO => Syslog::LOG_INFO,
15
+ Logger::WARN => Syslog::LOG_NOTICE,
16
+ Logger::ERROR => Syslog::LOG_WARNING,
17
+ Logger::FATAL => Syslog::LOG_ERR,
18
+ Logger::UNKNOWN => Syslog::LOG_ALERT
19
+ }
20
+
21
+ #
22
+ # Initializes default options for the logger
23
+ # <tt>ident</tt>:: the name of your program [default=$0].
24
+ # <tt>options</tt>:: syslog options [default=<tt>Syslog::LOG_PID | Syslog::LOG_CONS</tt>].
25
+ # Correct values are:
26
+ # LOG_CONS : writes the message on the console if an error occurs when sending the message;
27
+ # LOG_NDELAY : no delay before sending the message;
28
+ # LOG_PERROR : messages will also be written on STDERR;
29
+ # LOG_PID : adds the process number to the message (just after the program name)
30
+ # <tt>facility</tt>:: the syslog facility [default=nil] Correct values include:
31
+ # Syslog::LOG_DAEMON
32
+ # Syslog::LOG_USER
33
+ # Syslog::LOG_SYSLOG
34
+ # Syslog::LOG_LOCAL2
35
+ # Syslog::LOG_NEWS
36
+ # etc.
37
+ #
38
+ # Usage:
39
+ # logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
40
+ # logger.level = Logger::INFO # use Logger levels
41
+ # logger.warn "warning message"
42
+ # logger.debug "debug message"
43
+ #
44
+ def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
45
+ @ident = ident
46
+ @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
47
+ @facility = facility
48
+ @level = Logger::INFO
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(message || (block && block.call) || progname)
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
+ if self.prefix
100
+ message = "#{self.prefix}#{message}"
101
+ end
102
+ message
103
+ end
104
+ 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,163 @@
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 "prefix" do
33
+ before do
34
+ @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
35
+ @logger.prefix = "[my_app_prefix]"
36
+ end
37
+
38
+ it "should rensure that prefix was set" do
39
+ @logger.prefix.should == "[my_app_prefix]"
40
+ end
41
+
42
+ it "should add prefix to logged message" do
43
+ Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
44
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "#{@logger.prefix}message")
45
+ @logger.add(Logger::INFO, "message")
46
+ end
47
+
48
+ end
49
+
50
+ describe "add" do
51
+ before do
52
+ @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
53
+ end
54
+ it "should respond to add" do
55
+ @logger.should respond_to(:add)
56
+ end
57
+ it "should correctly log" do
58
+ Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
59
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
60
+ @logger.add(Logger::INFO, "message")
61
+ end
62
+ it "should take the message from the block if :message is nil" do
63
+ Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
64
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my message")
65
+ @logger.add(Logger::INFO) { "my message" }
66
+ end
67
+ it "should use the given progname" do
68
+ Syslog.should_receive(:open).with("progname", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=mock("syslog", :mask= => true))
69
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
70
+ @logger.add(Logger::INFO, "message", "progname") { "my message" }
71
+ end
72
+
73
+ it "should substitute '%' for '%%' before adding the :message" do
74
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
75
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "%%me%%ssage%%")
76
+ @logger.add(Logger::INFO, "%me%ssage%")
77
+ end
78
+
79
+ it "should strip the :message" do
80
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
81
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
82
+ @logger.add(Logger::INFO, "\n\nmessage ")
83
+ end
84
+
85
+ it "should not raise exception if asked to log with a nil message and body" do
86
+ Syslog.should_receive(:open).
87
+ with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
88
+ and_yield(syslog=mock("syslog", :mask= => true))
89
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my_app")
90
+ lambda {
91
+ @logger.add(Logger::INFO, nil)
92
+ }.should_not raise_error
93
+ end
94
+
95
+ it "should use given progname as the message if the message and block are nil" do
96
+ Syslog.should_receive(:open).
97
+ with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
98
+ and_yield(syslog=mock("syslog", :mask= => true))
99
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "my_app")
100
+ @logger.add(Logger::INFO, nil)
101
+ end
102
+ end # describe "add"
103
+
104
+ describe ":level? methods" do
105
+ before(:each) do
106
+ @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
107
+ end
108
+
109
+ %w{debug info warn error fatal}.each do |logger_method|
110
+ it "should respond to the #{logger_method}? method" do
111
+ @logger.should respond_to "#{logger_method}?".to_sym
112
+ end
113
+ end
114
+
115
+ it "should not have unknown? method" do
116
+ @logger.should_not respond_to :unknown?
117
+ end
118
+
119
+ it "should return true for all methods" do
120
+ @logger.level = Logger::DEBUG
121
+ %w{debug info warn error fatal}.each do |logger_method|
122
+ @logger.send("#{logger_method}?").should be_true
123
+ end
124
+ end
125
+
126
+ it "should return true for all except debug?" do
127
+ @logger.level = Logger::INFO
128
+ %w{info warn error fatal}.each do |logger_method|
129
+ @logger.send("#{logger_method}?").should be_true
130
+ end
131
+ @logger.debug?.should be_false
132
+ end
133
+
134
+ it "should return true for warn?, error? and fatal? when WARN" do
135
+ @logger.level = Logger::WARN
136
+ %w{warn error fatal}.each do |logger_method|
137
+ @logger.send("#{logger_method}?").should be_true
138
+ end
139
+ %w{debug info}.each do |logger_method|
140
+ @logger.send("#{logger_method}?").should be_false
141
+ end
142
+ end
143
+
144
+ it "should return true for error? and fatal? when ERROR" do
145
+ @logger.level = Logger::ERROR
146
+ %w{error fatal}.each do |logger_method|
147
+ @logger.send("#{logger_method}?").should be_true
148
+ end
149
+ %w{warn debug info}.each do |logger_method|
150
+ @logger.send("#{logger_method}?").should be_false
151
+ end
152
+ end
153
+
154
+ it "should return true only for fatal? when FATAL" do
155
+ @logger.level = Logger::FATAL
156
+ @logger.fatal?.should be_true
157
+ %w{error warn debug info}.each do |logger_method|
158
+ @logger.send("#{logger_method}?").should be_false
159
+ end
160
+ end
161
+ end # describe ":level? methods"
162
+
163
+ end # describe "Syslogger"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sayso-syslogger
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.2.4.001
6
+ platform: ruby
7
+ authors:
8
+ - SaySo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-23 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 - forked and gemified for sayso
39
+ email:
40
+ - sayso@truvolabs.com
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/sayso/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.6.2
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Dead simple Ruby Syslog logger - forked and gemified for sayso
82
+ test_files:
83
+ - spec/spec_helper.rb
84
+ - spec/syslogger_spec.rb