syslogger 1.6.4 → 1.6.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c21391ce1c6dd9acb99edabd4dcd73a730ddb6c9
4
- data.tar.gz: 94d556ab7bd1569c0eae7162a2541d9f27aeb6a1
3
+ metadata.gz: 10037fda0f96cf2e4073a7b721083696f1b30404
4
+ data.tar.gz: e483814598921fe12e703c5b519abe904121b290
5
5
  SHA512:
6
- metadata.gz: 6abc0fcc0946fd0950a492b9a09b071d0636b1ebaaca8a5b9e73bf5cdd05589b25bd202b46f6842f222390d20a3e8a59d5b7ae170b6100ef279a43db798c18fc
7
- data.tar.gz: 716670a2a752274f51874823618bec6874bbf832c14b82a1f51ba13afe7e431948d183cfd3897afce404d97e0d4f5f360dc1a9439f4d02c1d5dba0bffe615658
6
+ metadata.gz: 4eff79de93b2c28c2ec407d6d4675f815698894f0b4f2ceb79e9a7f0d96e54655310f96ccfa69244f3c742daa55e242491a09499a9f41827b40db969672d5be3
7
+ data.tar.gz: 66deff30ebd629b8d0251981b0c8f4530c72764bb0c56bdfdc97ba5ca403993626e098f106717020dbdc1abe0287dd3b53d3507bd33c75a36483a69035748872
@@ -0,0 +1,26 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+ Gemfile.lock
21
+
22
+ ## PROJECT::SPECIFIC
23
+
24
+ ## RVM
25
+ .rvmrc
26
+
@@ -0,0 +1,9 @@
1
+ dist: trusty
2
+ language: ruby
3
+ sudo: false
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.2
7
+ - 2.3.5
8
+ - 2.2.8
9
+ - jruby-9.1.9.0
@@ -0,0 +1,9 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.6.5
4
+
5
+ * Merge [Fixnum type is deprecated](https://github.com/crohr/syslogger/pull/42) (thanks thesmart)
6
+ * Merge [Add #puts method to Syslogger](https://github.com/crohr/syslogger/pull/38) (thanks VuokkoVuorinnen)
7
+ * Fix [Severity is passed as an array in the call to the formatter](https://github.com/crohr/syslogger/issues/35)
8
+ * Save uniq tags in the thread
9
+ * Improve tests
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE CHANGED
@@ -1,20 +1,21 @@
1
+ The MIT License (MIT)
2
+
1
3
  Copyright (c) 2010 Cyril Rohr, INRIA Rennes-Bretagne Atlantique
2
4
 
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:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
10
11
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
13
14
 
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.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ ## Syslogger
2
+
3
+ [![GitHub license](https://img.shields.io/github/license/crohr/syslogger.svg)](https://github.com/crohr/syslogger/blob/master/LICENSE)
4
+ [![GitHub release](https://img.shields.io/github/release/crohr/syslogger.svg)](https://github.com/crohr/syslogger/releases/latest)
5
+ [![Gem](https://img.shields.io/gem/v/syslogger.svg)](https://rubygems.org/gems/syslogger)
6
+ [![Gem](https://img.shields.io/gem/dtv/syslogger.svg)](https://rubygems.org/gems/syslogger)
7
+ [![Build Status](https://travis-ci.org/crohr/syslogger.svg?branch=master)](https://travis-ci.org/crohr/syslogger)
8
+
9
+ A drop-in replacement for the standard Logger Ruby library, that logs to the syslog instead of a log file.
10
+ Contrary to the SyslogLogger library, you can specify the facility and the syslog options.
11
+
12
+ ## Installation
13
+
14
+ ```sh
15
+ $ gem install syslogger
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ require 'syslogger'
22
+
23
+ # Will send all messages to the local0 facility, adding the process id in the message
24
+ logger = Syslogger.new("app_name", Syslog::LOG_PID, Syslog::LOG_LOCAL0)
25
+
26
+ # Optionally split messages to the specified number of bytes
27
+ logger.max_octets = 480
28
+
29
+ # Send messages that are at least of the Logger::INFO level
30
+ logger.level = Logger::INFO # use Logger levels
31
+
32
+ logger.debug "will not appear"
33
+ logger.info "will appear"
34
+ logger.warn "will appear"
35
+ ```
36
+
37
+ ## Contributions
38
+
39
+ See <https://github.com/crohr/syslogger/contributors>.
40
+
41
+ ## Copyright
42
+
43
+ Copyright (c) 2010 Cyril Rohr, INRIA Rennes-Bretagne Atlantique. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,17 +1,8 @@
1
- require 'bundler'
2
- Bundler.require
3
-
4
1
  require 'rspec/core/rake_task'
5
2
  require 'rdoc/task'
6
3
 
7
- $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
8
-
9
- RSpec::Core::RakeTask.new(:spec) do |spec|
10
- end
11
-
12
- RSpec::Core::RakeTask.new(:rcov) do |spec|
13
- spec.rcov = true
14
- end
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
15
6
 
16
7
  Rake::RDocTask.new do |rdoc|
17
8
  require 'syslogger'
@@ -20,5 +11,3 @@ Rake::RDocTask.new do |rdoc|
20
11
  rdoc.rdoc_files.include('README*')
21
12
  rdoc.rdoc_files.include('lib/**/*.rb')
22
13
  end
23
-
24
- task :default => :spec
@@ -4,21 +4,19 @@ require 'thread'
4
4
 
5
5
  class Syslogger
6
6
 
7
- VERSION = "1.6.4"
8
-
9
7
  MUTEX = Mutex.new
10
8
 
11
9
  attr_reader :level, :ident, :options, :facility, :max_octets
12
10
  attr_accessor :formatter
13
11
 
14
12
  MAPPING = {
15
- Logger::DEBUG => Syslog::LOG_DEBUG,
16
- Logger::INFO => Syslog::LOG_INFO,
17
- Logger::WARN => Syslog::LOG_WARNING,
18
- Logger::ERROR => Syslog::LOG_ERR,
19
- Logger::FATAL => Syslog::LOG_CRIT,
13
+ Logger::DEBUG => Syslog::LOG_DEBUG,
14
+ Logger::INFO => Syslog::LOG_INFO,
15
+ Logger::WARN => Syslog::LOG_WARNING,
16
+ Logger::ERROR => Syslog::LOG_ERR,
17
+ Logger::FATAL => Syslog::LOG_CRIT,
20
18
  Logger::UNKNOWN => Syslog::LOG_ALERT
21
- }
19
+ }.freeze
22
20
 
23
21
  #
24
22
  # Initializes default options for the logger
@@ -45,11 +43,11 @@ class Syslogger
45
43
  # logger.info "my_subapp" { "Some lazily computed message" }
46
44
  #
47
45
  def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
48
- @ident = ident
49
- @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
50
- @facility = facility
51
- @level = Logger::INFO
52
- @formatter = proc do |severity, datetime, progname, msg|
46
+ @ident = ident
47
+ @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
48
+ @facility = facility
49
+ @level = Logger::INFO
50
+ @formatter = proc do |_, _, _, msg|
53
51
  msg
54
52
  end
55
53
  end
@@ -80,6 +78,10 @@ class Syslogger
80
78
  add(Logger::INFO, msg)
81
79
  end
82
80
 
81
+ def puts(msg)
82
+ add(Logger::INFO, msg)
83
+ end
84
+
83
85
  # Low level method to add a message.
84
86
  # +severity+:: the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN
85
87
  # +message+:: the message string.
@@ -93,27 +95,10 @@ class Syslogger
93
95
  progname ||= @ident
94
96
  mask = Syslog::LOG_UPTO(MAPPING[@level])
95
97
  communication = message || block && block.call
96
- formatted_communication = clean(formatter.call([severity], Time.now, progname, communication))
97
-
98
- MUTEX.synchronize do
99
- Syslog.open(progname, @options, @facility) do |s|
100
- s.mask = mask
101
- if self.max_octets
102
- buffer = "#{tags_text}"
103
- formatted_communication.bytes do |byte|
104
- buffer.concat(byte)
105
- # if the last byte we added is potentially part of an escape, we'll go ahead and add another byte
106
- if buffer.bytesize >= self.max_octets && !['%'.ord,'\\'.ord].include?(byte)
107
- s.log(MAPPING[severity],buffer)
108
- buffer = ""
109
- end
110
- end
111
- s.log(MAPPING[severity],buffer) unless buffer.empty?
112
- else
113
- s.log(MAPPING[severity],"#{tags_text}#{formatted_communication}")
114
- end
115
- end
116
- end
98
+ formatted_communication = clean(formatter.call(severity, Time.now, progname, communication))
99
+
100
+ # Call Syslog
101
+ syslog_add(progname, severity, mask, formatted_communication)
117
102
  end
118
103
 
119
104
  # Set the max octets of the messages written to the log
@@ -124,13 +109,7 @@ class Syslogger
124
109
  # Sets the minimum level for messages to be written in the log.
125
110
  # +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>
126
111
  def level=(level)
127
- level = Logger.const_get(level.to_s.upcase) if level.is_a?(Symbol)
128
-
129
- unless level.is_a?(Fixnum)
130
- raise ArgumentError.new("Invalid logger level `#{level.inspect}`")
131
- end
132
-
133
- @level = level
112
+ @level = sanitize_level(level)
134
113
  end
135
114
 
136
115
  # Sets the ident string passed along to Syslog
@@ -147,8 +126,8 @@ class Syslogger
147
126
  end
148
127
 
149
128
  def push_tags(*tags)
150
- tags.flatten.reject{ |i| i.respond_to?(:empty?) ? i.empty? : !i }.tap do |new_tags|
151
- current_tags.concat new_tags
129
+ tags.flatten.reject { |i| i.respond_to?(:empty?) ? i.empty? : !i }.tap do |new_tags|
130
+ current_tags.concat(new_tags).uniq!
152
131
  end
153
132
  end
154
133
 
@@ -160,8 +139,26 @@ class Syslogger
160
139
  current_tags.clear
161
140
  end
162
141
 
142
+ def current_tags
143
+ Thread.current[:syslogger_tagged_logging_tags] ||= []
144
+ end
145
+
163
146
  protected
164
147
 
148
+ def sanitize_level(new_level)
149
+ begin
150
+ new_level = Logger.const_get(new_level.to_s.upcase)
151
+ rescue => _
152
+ raise ArgumentError.new("Invalid logger level `#{new_level.inspect}`")
153
+ end if new_level.is_a?(Symbol)
154
+
155
+ unless new_level.is_a?(Integer)
156
+ raise ArgumentError.new("Invalid logger level `#{new_level.inspect}`")
157
+ end
158
+
159
+ new_level
160
+ end
161
+
165
162
  # Borrowed from SyslogLogger.
166
163
  def clean(message)
167
164
  message = message.to_s.dup
@@ -177,11 +174,29 @@ class Syslogger
177
174
  def tags_text
178
175
  tags = current_tags
179
176
  if tags.any?
180
- clean(tags.collect { |tag| "[#{tag}] " }.join) << " "
177
+ clean(tags.collect { |tag| "[#{tag}] " }.join) << ' '
181
178
  end
182
179
  end
183
180
 
184
- def current_tags
185
- Thread.current[:syslogger_tagged_logging_tags] ||= []
181
+ def syslog_add(progname, severity, mask, formatted_communication)
182
+ MUTEX.synchronize do
183
+ Syslog.open(progname, @options, @facility) do |s|
184
+ s.mask = mask
185
+ if self.max_octets
186
+ buffer = "#{tags_text}"
187
+ formatted_communication.bytes do |byte|
188
+ buffer.concat(byte)
189
+ # if the last byte we added is potentially part of an escape, we'll go ahead and add another byte
190
+ if buffer.bytesize >= self.max_octets && !['%'.ord,'\\'.ord].include?(byte)
191
+ s.log(MAPPING[severity], buffer)
192
+ buffer = ''
193
+ end
194
+ end
195
+ s.log(MAPPING[severity], buffer) unless buffer.empty?
196
+ else
197
+ s.log(MAPPING[severity], "#{tags_text}#{formatted_communication}")
198
+ end
199
+ end
200
+ end
186
201
  end
187
202
  end
@@ -0,0 +1,3 @@
1
+ class Syslogger
2
+ VERSION = '1.6.5'.freeze
3
+ end
@@ -1,5 +1,21 @@
1
+ require 'simplecov'
1
2
  require 'rspec'
2
3
 
3
- $LOAD_PATH.unshift(File.dirname(__FILE__))
4
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ # Start Simplecov
5
+ SimpleCov.start
6
+
7
+ # Configure RSpec
8
+ RSpec.configure do |config|
9
+ config.color = true
10
+ config.fail_fast = false
11
+
12
+ config.order = :random
13
+ Kernel.srand config.seed
14
+
15
+ config.expect_with :rspec do |c|
16
+ c.syntax = :expect
17
+ end
18
+ end
19
+
20
+ # Load lib
5
21
  require 'syslogger'
@@ -1,274 +1,196 @@
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=double("syslog", :mask= => true))
7
- syslog.should_receive(:log).with(Syslog::LOG_WARNING, "Some message")
8
- logger.warn "Some message"
9
- end
1
+ require 'spec_helper'
10
2
 
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=double("syslog", :mask= => true))
14
- syslog.should_receive(:log).with(Syslog::LOG_WARNING, "Some message")
15
- logger.warn "Some message"
16
- end
3
+ describe Syslogger do
17
4
 
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
5
+ let(:fake_syslog) { double('syslog', :mask= => true) }
22
6
 
23
- it "should log #{logger_method} without raising an exception if called with a block" do
7
+ describe '.new' do
8
+ it 'should log to the default syslog facility, with the default options' do
9
+ expect(Syslog).to receive(:open).with($0, Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(fake_syslog)
10
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_WARNING, 'Some message')
24
11
  logger = Syslogger.new
25
- logger.level = Logger.const_get(logger_method.upcase)
26
- Syslog.stub(:open).and_yield(syslog=double("syslog", :mask= => true))
27
- severity = Syslogger::MAPPING[Logger.const_get(logger_method.upcase)]
28
- syslog.should_receive(:log).with(severity, "Some message that doesn't need to be in a block")
29
- lambda {
30
- logger.send(logger_method.to_sym) { "Some message that doesn't need to be in a block" }
31
- }.should_not raise_error
12
+ logger.warn 'Some message'
32
13
  end
33
14
 
34
- it "should log #{logger_method} using message as progname with the block's result" do
35
- logger = Syslogger.new
36
- logger.level = Logger.const_get(logger_method.upcase)
37
- Syslog.should_receive(:open).with("Woah", anything, nil)
38
- .and_yield(syslog=double("syslog", :mask= => true))
39
- severity = Syslogger::MAPPING[Logger.const_get(logger_method.upcase)]
40
- syslog.should_receive(:log).with(severity, "Some message that really needs a block")
41
- lambda {
42
- logger.send(logger_method.to_sym, "Woah") { "Some message that really needs a block" }
43
- }.should_not raise_error
15
+ it 'should log to the user facility, with specific options' do
16
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
17
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_WARNING, 'Some message')
18
+ logger = Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER)
19
+ logger.warn 'Some message'
44
20
  end
21
+ end
45
22
 
46
- it "should log #{logger_method} without raising an exception if called with a nil message" do
47
- logger = Syslogger.new
48
- lambda {
49
- logger.send(logger_method.to_sym, nil)
50
- }.should_not raise_error
23
+ describe '#add' do
24
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
25
+
26
+ it 'should respond to add' do
27
+ expect(logger).to respond_to(:add)
51
28
  end
52
29
 
53
- it "should log #{logger_method} without raising an exception if called with a no message" do
54
- logger = Syslogger.new
55
- lambda {
56
- logger.send(logger_method.to_sym)
57
- }.should_not raise_error
30
+ it 'should correctly log' do
31
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
32
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
33
+ logger.add(Logger::INFO, 'message')
58
34
  end
59
35
 
60
- it "should log #{logger_method} without raising an exception if message splits on an escape" do
61
- logger = Syslogger.new
62
- logger.max_octets=100
63
- msg="A"*99
64
- msg+="%BBB"
65
- lambda {
66
- logger.send(logger_method.to_sym,msg)
67
- }.should_not raise_error
36
+ it 'should take the message from the block if :message is nil' do
37
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
38
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
39
+ logger.add(Logger::INFO) { 'message' }
68
40
  end
69
- end
70
41
 
71
- %w{debug info warn error}.each do |logger_method|
72
- it "should not log #{logger_method} when level is higher" do
73
- logger = Syslogger.new
74
- logger.level = Logger::FATAL
75
- Syslog.should_not_receive(:open).with($0, Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(syslog=double("syslog", :mask= => true))
76
- syslog.should_not_receive(:log).with(Syslog::LOG_NOTICE, "Some message")
77
- logger.send(logger_method.to_sym, "Some message")
42
+ it 'should use the given progname' do
43
+ expect(Syslog).to receive(:open).with('progname', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
44
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
45
+ logger.add(Logger::INFO, 'message', 'progname')
78
46
  end
79
47
 
80
- it "should not evaluate a block or log #{logger_method} when level is higher" do
81
- logger = Syslogger.new
82
- logger.level = Logger::FATAL
83
- Syslog.should_not_receive(:open).with($0, Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(syslog=double("syslog", :mask= => true))
84
- syslog.should_not_receive(:log).with(Syslog::LOG_NOTICE, "Some message")
85
- logger.send(logger_method.to_sym) { violated "This block should not have been called" }
48
+ it 'should use the default progname when message is passed in progname' do
49
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
50
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
51
+ logger.add(Logger::INFO, nil, 'message')
86
52
  end
87
- end
88
53
 
89
- it "should respond to <<" do
90
- logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
91
- logger.should respond_to(:<<)
92
- Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=double("syslog", :mask= => true))
93
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "yop")
94
- logger << "yop"
95
- end
54
+ it 'should use the given progname if message is passed in block' do
55
+ expect(Syslog).to receive(:open).with('progname', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
56
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
57
+ logger.add(Logger::INFO, nil, 'progname') { 'message' }
58
+ end
96
59
 
97
- it "should respond to write" do
98
- logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
99
- logger.should respond_to(:write)
100
- Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=double("syslog", :mask= => true))
101
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "yop")
102
- logger.write "yop"
103
- end
60
+ it "should substitute '%' for '%%' before adding the :message" do
61
+ allow(Syslog).to receive(:open).and_yield(fake_syslog)
62
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, "%%me%%ssage%%")
63
+ logger.add(Logger::INFO, "%me%ssage%")
64
+ end
104
65
 
105
- it "should allow multiple instances to log at the same time" do
106
- logger1 = Syslogger.new("my_app1", Syslog::LOG_PID, Syslog::LOG_USER)
107
- logger2 = Syslogger.new("my_app2", Syslog::LOG_PID, Syslog::LOG_USER)
66
+ it 'should clean formatted message' do
67
+ allow(Syslog).to receive(:open).and_yield(fake_syslog)
68
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, "m%%e%%s%%s%%a%%g%%e")
108
69
 
109
- thread1 = Thread.new do
110
- 5000.times do |i|
111
- logger1.write "logger1"
112
- end
113
- end
70
+ original_formatter = logger.formatter
114
71
 
115
- thread2 = Thread.new do
116
- 5000.times do |i|
117
- logger2.write "logger1"
72
+ begin
73
+ logger.formatter = proc do |severity, datetime, progname, msg|
74
+ msg.split(//).join('%')
75
+ end
76
+ logger.add(Logger::INFO, 'message')
77
+ ensure
78
+ logger.formatter = original_formatter
118
79
  end
119
80
  end
120
81
 
121
- thread1.join
122
- thread2.join
123
- end
124
-
125
- it "should not fail under chaos" do
126
- threads = []
127
- (1..10).each do
128
- threads << Thread.new do
129
- (1..100).each do |index|
130
- logger = Syslogger.new(Thread.current.inspect, Syslog::LOG_PID, Syslog::LOG_USER)
131
- logger.write index
82
+ it 'should clean tagged message' do
83
+ allow(Syslog).to receive(:open).and_yield(fake_syslog)
84
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, "[t%%a%%g%%g%%e%%d] [it] message")
85
+
86
+ logger.tagged("t%a%g%g%e%d") do
87
+ logger.tagged('it') do
88
+ logger.add(Logger::INFO, 'message')
132
89
  end
133
90
  end
134
91
  end
135
-
136
- threads.each{|thread| thread.join }
137
- end
138
92
 
139
- describe "add" do
140
- before do
141
- @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
142
- end
143
- it "should respond to add" do
144
- @logger.should respond_to(:add)
145
- end
146
- it "should correctly log" do
147
- Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=double("syslog", :mask= => true))
148
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
149
- @logger.add(Logger::INFO, "message")
150
- end
151
- it "should take the message from the block if :message is nil" do
152
- Syslog.should_receive(:open).with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=double("syslog", :mask= => true))
153
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "my message")
154
- @logger.add(Logger::INFO) { "my message" }
93
+ it 'should strip the :message' do
94
+ allow(Syslog).to receive(:open).and_yield(fake_syslog)
95
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
96
+ logger.add(Logger::INFO, "\n\nmessage ")
155
97
  end
156
- it "should use the given progname" do
157
- Syslog.should_receive(:open).with("progname", Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog=double("syslog", :mask= => true))
158
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
159
- @logger.add(Logger::INFO, "message", "progname") { "my message" }
160
- end
161
-
162
- it "should use the default progname when message is passed in progname" do
163
- Syslog.should_receive(:open).
164
- with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
165
- and_yield(syslog = double("syslog", :mask= => true))
166
98
 
167
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
168
- @logger.add(Logger::INFO, nil, "message")
99
+ it 'should not raise exception if asked to log with a nil message and body' do
100
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
101
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, '')
102
+ expect {
103
+ logger.add(Logger::INFO, nil)
104
+ }.to_not raise_error
169
105
  end
170
106
 
171
- it "should use the given progname if message is passed in block" do
172
- Syslog.should_receive(:open).
173
- with("progname", Syslog::LOG_PID, Syslog::LOG_USER).
174
- and_yield(syslog = double("syslog", :mask= => true))
175
-
176
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
177
- @logger.add(Logger::INFO, nil, "progname") { "message" }
107
+ it 'should send an empty string if the message and block are nil' do
108
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
109
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, '')
110
+ logger.add(Logger::INFO, nil)
178
111
  end
179
112
 
180
- it "should substitute '%' for '%%' before adding the :message" do
181
- Syslog.stub(:open).and_yield(syslog=double("syslog", :mask= => true))
182
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "%%me%%ssage%%")
183
- @logger.add(Logger::INFO, "%me%ssage%")
184
- end
185
-
186
- it "should clean formatted message" do
187
- Syslog.stub(:open).and_yield(syslog=double("syslog", :mask= => true))
188
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "m%%e%%s%%s%%a%%g%%e")
189
-
190
- original_formatter = @logger.formatter
191
- begin
192
- @logger.formatter = proc do |severity, datetime, progname, msg|
193
- msg.split(//).join('%')
194
- end
195
-
196
- @logger.add(Logger::INFO, "message")
197
- ensure
198
- @logger.formatter = original_formatter
199
- end
113
+ it 'should split string over the max octet size' do
114
+ logger.max_octets = 480
115
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
116
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'a' * 480).twice
117
+ logger.add(Logger::INFO, 'a' * 960)
200
118
  end
201
-
202
- it "should clean tagged message" do
203
- Syslog.stub(:open).and_yield(syslog=double("syslog", :mask= => true))
204
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "[t%%a%%g%%g%%e%%d] [it] message")
205
-
206
- @logger.tagged("t%a%g%g%e%d") do
207
- @logger.tagged("it") do
208
- @logger.add(Logger::INFO, "message")
209
- end
119
+
120
+ it 'should apply the log formatter to the message' do
121
+ allow(Syslog).to receive(:open).and_yield(fake_syslog)
122
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'test message!')
123
+ logger.formatter = proc do |severity, datetime, progname, msg|
124
+ "test #{msg}!"
210
125
  end
126
+ logger.add(Logger::INFO, 'message')
211
127
  end
128
+ end
212
129
 
213
- it "should strip the :message" do
214
- Syslog.stub(:open).and_yield(syslog=double("syslog", :mask= => true))
215
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
216
- @logger.add(Logger::INFO, "\n\nmessage ")
217
- end
130
+ describe '#<<' do
131
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
218
132
 
219
- it "should not raise exception if asked to log with a nil message and body" do
220
- Syslog.should_receive(:open).
221
- with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
222
- and_yield(syslog=double("syslog", :mask= => true))
223
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "")
224
- lambda {
225
- @logger.add(Logger::INFO, nil)
226
- }.should_not raise_error
133
+ it 'should respond to <<' do
134
+ expect(logger).to respond_to(:<<)
227
135
  end
228
136
 
229
- it "should send an empty string if the message and block are nil" do
230
- Syslog.should_receive(:open).
231
- with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
232
- and_yield(syslog=double("syslog", :mask= => true))
233
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "")
234
- @logger.add(Logger::INFO, nil)
137
+ it 'should correctly log' do
138
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
139
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
140
+ logger << 'message'
235
141
  end
142
+ end
236
143
 
237
- it "should split string over the max octet size" do
238
- @logger.max_octets = 480
239
- Syslog.should_receive(:open).
240
- with("my_app", Syslog::LOG_PID, Syslog::LOG_USER).
241
- and_yield(syslog=double("syslog", :mask= => true))
242
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "a"*480).twice
243
- @logger.add(Logger::INFO, "a"*960)
144
+ describe '#puts' do
145
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
146
+
147
+ it 'should respond to puts' do
148
+ expect(logger).to respond_to(:puts)
244
149
  end
245
150
 
246
- it "should apply the log formatter to the message" do
247
- Syslog.stub(:open).and_yield(syslog=double("syslog", :mask= => true))
248
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "test message!")
249
- @logger.formatter = proc do |severity, datetime, progname, msg|
250
- "test #{msg}!"
251
- end
252
- @logger.add(Logger::INFO, "message")
151
+ it 'should correctly log' do
152
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
153
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
154
+ logger.puts 'message'
253
155
  end
254
- end # describe "add"
156
+ end
255
157
 
256
- describe "max_octets=" do
257
- before(:each) do
258
- @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
158
+ describe '#write' do
159
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
160
+
161
+ it 'should respond to write' do
162
+ expect(logger).to respond_to(:write)
259
163
  end
260
164
 
261
- it "should set the max_octets for the logger" do
262
- lambda { @logger.max_octets = 1 }.should change(@logger, :max_octets)
263
- @logger.max_octets.should == 1
165
+ it 'should correctly log' do
166
+ expect(Syslog).to receive(:open).with('my_app', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(fake_syslog)
167
+ expect(fake_syslog).to receive(:log).with(Syslog::LOG_INFO, 'message')
168
+ logger.write 'message'
264
169
  end
170
+ end
265
171
 
172
+ describe '#max_octets=' do
173
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
174
+
175
+ it 'should set the max_octets for the logger' do
176
+ expect { logger.max_octets = 1 }.to change(logger, :max_octets)
177
+ expect(logger.max_octets).to eq 1
178
+ end
266
179
  end
267
180
 
268
- describe "level=" do
269
- before(:each) do
270
- @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
181
+ describe '#ident=' do
182
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID | Syslog::LOG_CONS, nil) }
183
+
184
+ it 'should permanently change the ident string' do
185
+ logger.ident = 'new_ident'
186
+ expect(Syslog).to receive(:open).with('new_ident', Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(fake_syslog)
187
+ expect(fake_syslog).to receive(:log)
188
+ logger.warn('should get the new ident string')
271
189
  end
190
+ end
191
+
192
+ describe '#level=' do
193
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
272
194
 
273
195
  { :debug => Logger::DEBUG,
274
196
  :info => Logger::INFO,
@@ -277,105 +199,257 @@ describe "Syslogger" do
277
199
  :fatal => Logger::FATAL
278
200
  }.each_pair do |level_symbol, level_value|
279
201
  it "should allow using :#{level_symbol}" do
280
- @logger.level = level_symbol
281
- @logger.level.should equal level_value
202
+ logger.level = level_symbol
203
+ expect(logger.level).to eq level_value
282
204
  end
283
205
 
284
- it "should allow using Fixnum #{level_value}" do
285
- @logger.level = level_value
286
- @logger.level.should equal level_value
206
+ it "should allow using Integer #{level_value}" do
207
+ logger.level = level_value
208
+ expect(logger.level).to eq level_value
287
209
  end
288
210
  end
289
211
 
290
- it "should not allow using random symbols" do
291
- lambda {
292
- @logger.level = :foo
293
- }.should raise_error
212
+ it 'should not allow using random symbols' do
213
+ expect {
214
+ logger.level = :foo
215
+ }.to raise_error(ArgumentError)
294
216
  end
295
217
 
296
- it "should not allow using symbols mapping back to non-level constants" do
297
- lambda {
298
- @logger.level = :version
299
- }.should raise_error
218
+ it 'should not allow using symbols mapping back to non-level constants' do
219
+ expect {
220
+ logger.level = :version
221
+ }.to raise_error(ArgumentError)
300
222
  end
301
223
 
302
- it "should not allow using strings" do
303
- lambda {
304
- @logger.level = "warn"
305
- }.should raise_error
224
+ it 'should not allow using strings' do
225
+ expect {
226
+ logger.level = 'warn'
227
+ }.to raise_error(ArgumentError)
306
228
  end
307
- end # describe "level="
229
+ end
308
230
 
309
- describe "ident=" do
310
- before(:each) do
311
- @logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, nil)
231
+ describe '#:level? methods' do
232
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
233
+
234
+ %w{debug info warn error fatal}.each do |logger_method|
235
+ it "should respond to the #{logger_method}? method" do
236
+ expect(logger).to respond_to "#{logger_method}?".to_sym
237
+ end
312
238
  end
313
239
 
314
- it "should permanently change the ident string" do
315
- @logger.ident = "new_ident"
316
- Syslog.should_receive(:open).with("new_ident", Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(syslog=double("syslog", :mask= => true))
317
- syslog.should_receive(:log)
318
- @logger.warn("should get the new ident string")
240
+ it 'should not have unknown? method' do
241
+ expect(logger).to_not respond_to :unknown?
319
242
  end
320
- end
321
243
 
322
- describe ":level? methods" do
323
- before(:each) do
324
- @logger = Syslogger.new("my_app", Syslog::LOG_PID, Syslog::LOG_USER)
244
+ context 'when loglevel is Logger::DEBUG' do
245
+ it 'should return true for all methods' do
246
+ logger.level = Logger::DEBUG
247
+ %w{debug info warn error fatal}.each do |logger_method|
248
+ expect(logger.send("#{logger_method}?")).to be true
249
+ end
250
+ end
325
251
  end
326
252
 
327
- %w{debug info warn error fatal}.each do |logger_method|
328
- it "should respond to the #{logger_method}? method" do
329
- @logger.should respond_to "#{logger_method}?".to_sym
253
+ context 'when loglevel is Logger::INFO' do
254
+ it 'should return true for all except debug?' do
255
+ logger.level = Logger::INFO
256
+ %w{info warn error fatal}.each do |logger_method|
257
+ expect(logger.send("#{logger_method}?")).to be true
258
+ end
259
+ expect(logger.debug?).to be false
330
260
  end
331
261
  end
332
262
 
333
- it "should not have unknown? method" do
334
- @logger.should_not respond_to :unknown?
263
+ context 'when loglevel is Logger::WARN' do
264
+ it 'should return true for warn?, error? and fatal? when WARN' do
265
+ logger.level = Logger::WARN
266
+ %w{warn error fatal}.each do |logger_method|
267
+ expect(logger.send("#{logger_method}?")).to be true
268
+ end
269
+ %w{debug info}.each do |logger_method|
270
+ expect(logger.send("#{logger_method}?")).to be false
271
+ end
272
+ end
335
273
  end
336
274
 
337
- it "should return true for all methods" do
338
- @logger.level = Logger::DEBUG
339
- %w{debug info warn error fatal}.each do |logger_method|
340
- @logger.send("#{logger_method}?").should be_true
275
+ context 'when loglevel is Logger::ERROR' do
276
+ it 'should return true for error? and fatal? when ERROR' do
277
+ logger.level = Logger::ERROR
278
+ %w{error fatal}.each do |logger_method|
279
+ expect(logger.send("#{logger_method}?")).to be true
280
+ end
281
+ %w{warn debug info}.each do |logger_method|
282
+ expect(logger.send("#{logger_method}?")).to be false
283
+ end
341
284
  end
342
285
  end
343
286
 
344
- it "should return true for all except debug?" do
345
- @logger.level = Logger::INFO
346
- %w{info warn error fatal}.each do |logger_method|
347
- @logger.send("#{logger_method}?").should be_true
287
+ context 'when loglevel is Logger::FATAL' do
288
+ it 'should return true only for fatal? when FATAL' do
289
+ logger.level = Logger::FATAL
290
+ expect(logger.fatal?).to be true
291
+ %w{error warn debug info}.each do |logger_method|
292
+ expect(logger.send("#{logger_method}?")).to be false
293
+ end
348
294
  end
349
- @logger.debug?.should be_false
350
295
  end
296
+ end
297
+
298
+ describe '#push_tags' do
299
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
300
+ after(:each) { logger.clear_tags! }
301
+
302
+ it 'saves tags' do
303
+ logger.push_tags('tag1')
304
+ logger.push_tags('tag2')
305
+ expect(logger.current_tags).to eq ['tag1', 'tag2']
306
+ end
307
+
308
+ it 'saves uniq tags' do
309
+ logger.push_tags('tag1')
310
+ logger.push_tags('tag2')
311
+ logger.push_tags('foo')
312
+ logger.push_tags('foo')
313
+ expect(logger.current_tags).to eq ['tag1', 'tag2', 'foo']
314
+ end
315
+ end
316
+
317
+ describe '#clear_tags!' do
318
+ let(:logger) { Syslogger.new('my_app', Syslog::LOG_PID, Syslog::LOG_USER) }
319
+ after(:each) { logger.clear_tags! }
351
320
 
352
- it "should return true for warn?, error? and fatal? when WARN" do
353
- @logger.level = Logger::WARN
354
- %w{warn error fatal}.each do |logger_method|
355
- @logger.send("#{logger_method}?").should be_true
321
+ it 'clears tags' do
322
+ expect(logger.current_tags).to eq []
323
+ logger.push_tags('tag1')
324
+ logger.push_tags('tag2')
325
+ expect(logger.current_tags).to eq ['tag1', 'tag2']
326
+ logger.clear_tags!
327
+ expect(logger.current_tags).to eq []
328
+ end
329
+ end
330
+
331
+ describe 'logger methods (debug info warn error fatal unknown)' do
332
+ %w{debug info warn error fatal unknown}.each do |logger_method|
333
+
334
+ it "should respond to the #{logger_method.inspect} method" do
335
+ expect(Syslogger.new).to respond_to logger_method.to_sym
356
336
  end
357
- %w{debug info}.each do |logger_method|
358
- @logger.send("#{logger_method}?").should be_false
337
+
338
+ it "should log #{logger_method} without raising an exception if called with a block" do
339
+ severity = Syslogger::MAPPING[Logger.const_get(logger_method.upcase)]
340
+
341
+ logger = Syslogger.new
342
+ logger.level = Logger.const_get(logger_method.upcase)
343
+
344
+ expect(Syslog).to receive(:open).and_yield(fake_syslog)
345
+ expect(fake_syslog).to receive(:log).with(severity, 'Some message that dont need to be in a block')
346
+ expect {
347
+ logger.send(logger_method.to_sym) { 'Some message that dont need to be in a block' }
348
+ }.to_not raise_error
359
349
  end
360
- end
361
350
 
362
- it "should return true for error? and fatal? when ERROR" do
363
- @logger.level = Logger::ERROR
364
- %w{error fatal}.each do |logger_method|
365
- @logger.send("#{logger_method}?").should be_true
351
+ it "should log #{logger_method} using message as progname with the block's result" do
352
+ severity = Syslogger::MAPPING[Logger.const_get(logger_method.upcase)]
353
+
354
+ logger = Syslogger.new
355
+ logger.level = Logger.const_get(logger_method.upcase)
356
+
357
+ expect(Syslog).to receive(:open).with('Woah', anything, nil).and_yield(fake_syslog)
358
+ expect(fake_syslog).to receive(:log).with(severity, 'Some message that really needs a block')
359
+ expect {
360
+ logger.send(logger_method.to_sym, 'Woah') { 'Some message that really needs a block' }
361
+ }.to_not raise_error
362
+ end
363
+
364
+ it "should log #{logger_method} without raising an exception if called with a nil message" do
365
+ logger = Syslogger.new
366
+ expect {
367
+ logger.send(logger_method.to_sym, nil)
368
+ }.to_not raise_error
369
+ end
370
+
371
+ it "should log #{logger_method} without raising an exception if called with a no message" do
372
+ logger = Syslogger.new
373
+ expect {
374
+ logger.send(logger_method.to_sym)
375
+ }.to_not raise_error
366
376
  end
367
- %w{warn debug info}.each do |logger_method|
368
- @logger.send("#{logger_method}?").should be_false
377
+
378
+ it "should log #{logger_method} without raising an exception if message splits on an escape" do
379
+ logger = Syslogger.new
380
+ logger.max_octets = 100
381
+ msg = 'A' * 99
382
+ msg += "%BBB"
383
+ expect {
384
+ logger.send(logger_method.to_sym, msg)
385
+ }.to_not raise_error
369
386
  end
370
387
  end
388
+ end
389
+
390
+ describe 'it should be thread safe' do
391
+ it 'should not fail under chaos' do
392
+ threads = []
393
+ (1..10).each do
394
+ threads << Thread.new do
395
+ (1..100).each do |index|
396
+ logger = Syslogger.new(Thread.current.inspect, Syslog::LOG_PID, Syslog::LOG_USER)
397
+ logger.write index
398
+ end
399
+ end
400
+ end
401
+
402
+ threads.map(&:join)
403
+ end
404
+
405
+ it 'should allow multiple instances to log at the same time' do
406
+ logger1 = Syslogger.new('my_app1', Syslog::LOG_PID, Syslog::LOG_USER)
407
+ logger2 = Syslogger.new('my_app2', Syslog::LOG_PID, Syslog::LOG_USER)
408
+
409
+ syslog1 = double('syslog1', :mask= => true)
410
+ syslog2 = double('syslog2', :mask= => true)
411
+
412
+ expect(Syslog).to receive(:open).exactly(5000).times.with('my_app1', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog1)
413
+ expect(Syslog).to receive(:open).exactly(5000).times.with('my_app2', Syslog::LOG_PID, Syslog::LOG_USER).and_yield(syslog2)
371
414
 
372
- it "should return true only for fatal? when FATAL" do
373
- @logger.level = Logger::FATAL
374
- @logger.fatal?.should be_true
375
- %w{error warn debug info}.each do |logger_method|
376
- @logger.send("#{logger_method}?").should be_false
415
+ expect(syslog1).to receive(:log).exactly(5000).times.with(Syslog::LOG_INFO, 'logger1')
416
+ expect(syslog2).to receive(:log).exactly(5000).times.with(Syslog::LOG_INFO, 'logger2')
417
+
418
+ threads = []
419
+
420
+ threads << Thread.new do
421
+ 5000.times do |i|
422
+ logger1.info 'logger1'
423
+ end
424
+ end
425
+
426
+ threads << Thread.new do
427
+ 5000.times do |i|
428
+ logger2.info 'logger2'
429
+ end
377
430
  end
431
+
432
+ threads.map(&:join)
378
433
  end
379
- end # describe ":level? methods"
434
+ end
435
+
436
+ describe 'it should respect loglevel precedence when logging' do
437
+ %w{debug info warn error}.each do |logger_method|
438
+ it "should not log #{logger_method} when level is higher" do
439
+ logger = Syslogger.new
440
+ logger.level = Logger::FATAL
441
+ expect(Syslog).to_not receive(:open).with($0, Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(fake_syslog)
442
+ expect(fake_syslog).to_not receive(:log).with(Syslog::LOG_NOTICE, 'Some message')
443
+ logger.send(logger_method.to_sym, 'Some message')
444
+ end
380
445
 
381
- end # describe "Syslogger"
446
+ it "should not evaluate a block or log #{logger_method} when level is higher" do
447
+ logger = Syslogger.new
448
+ logger.level = Logger::FATAL
449
+ expect(Syslog).to_not receive(:open).with($0, Syslog::LOG_PID | Syslog::LOG_CONS, nil).and_yield(fake_syslog)
450
+ expect(fake_syslog).to_not receive(:log).with(Syslog::LOG_NOTICE, 'Some message')
451
+ logger.send(logger_method.to_sym) { violated 'This block should not have been called' }
452
+ end
453
+ end
454
+ end
455
+ end