Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syslogging must be exclusive to process #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/log4r/outputter/syslogoutputter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Log4r

class SyslogOutputter < Outputter
include Syslog::Constants

SEMAPHOR = Mutex.new()

# maps default log4r levels to syslog priorities (logevents never see ALL and OFF)
# SYSLOG Levels are:
Expand Down Expand Up @@ -118,8 +120,16 @@ def canonical_log(logevent)
msg = o.inspect
end

Syslog.open(@ident, @logopt, @facility) do |s|
s.log(pri, '%s', msg)
#Block globally, since the Syslog gets opened for the process
SEMAPHOR.synchronize do
Syslog.open(@ident, @logopt, @facility) do |s|
s.mask = begin
Syslog::LOG_UPTO(SYSLOG_LEVELS_MAP[@levels_map[Log4r::LNAMES[@level]]])
rescue
Syslog::LOG_UPTO(LOG_INFO)
end
s.log(pri, '%s', msg)
end
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions tests/testsyslogoutputter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'test_helper'
require 'log4r/outputter/syslogoutputter'

class TestSyslogOutputter < TestCase
def test_concurrency
threads = []
(1..20).each do
threads << Thread.new do
#Create log and add syslog outputter
id = Thread.current.__id__
id = id.to_s()
log = Log4r::Logger.new(id)
log.add(Log4r::SyslogOutputter.new(id))

(1..200).each do
log.debug "debugging"
log.info "a piece of info"
log.warn "Danger, Will Robinson, danger!"
log.error "I dropped my Wookie! :("
log.fatal "kaboom!"
Thread.pass()
end
end
end

threads.each do |thread|
assert_nothing_raised(Exception) do
thread.join()
end
end
end
end