Skip to content

Require syslog only when necessary #248

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

Open
wants to merge 1 commit 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
7 changes: 0 additions & 7 deletions lib/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
require 'little-plugger'
require 'multi_json'

begin
require 'syslog'
HAVE_SYSLOG = true
rescue LoadError
HAVE_SYSLOG = false
end

#
#
module Logging
Expand Down
14 changes: 13 additions & 1 deletion lib/logging/appenders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,21 @@ def reset
end
# :startdoc:

# Accessor / Factory for the Syslog appender.
#
def self.syslog( *args )
fail ArgumentError, '::Logging::Appenders::Syslog needs a name as first argument.' if args.empty?
::Logging::Appenders::Syslog.new(*args)
end

extend self
@appenders = Hash.new

# Load Syslog only when requested. Windows does not have syslog, and
# Ruby 3.4.0 will remove it from the standard library. Requiring
# syslog on Ruby 3.3.0 will result in an warning if the gem is not
# explicitly installed.
autoload :Syslog, Logging.libpath('logging/appenders/syslog')
end # Appenders

require libpath('logging/appenders/buffering')
Expand All @@ -57,6 +70,5 @@ def reset
require libpath('logging/appenders/file')
require libpath('logging/appenders/rolling_file')
require libpath('logging/appenders/string_io')
require libpath('logging/appenders/syslog')
end # Logging

14 changes: 1 addition & 13 deletions lib/logging/appenders/syslog.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@

# only load this class if we have the syslog library
# Windows does not have syslog
#
if HAVE_SYSLOG
require 'syslog'

module Logging::Appenders

# Accessor / Factory for the Syslog appender.
#
def self.syslog( *args )
fail ArgumentError, '::Logging::Appenders::Syslog needs a name as first argument.' if args.empty?
::Logging::Appenders::Syslog.new(*args)
end

# This class provides an Appender that can write to the UNIX syslog
# daemon.
#
Expand Down Expand Up @@ -211,4 +200,3 @@ def syslog_level_num( level )

end # Syslog
end # Logging::Appenders
end # HAVE_SYSLOG
7 changes: 7 additions & 0 deletions test/appenders/test_syslog.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

require File.expand_path('../setup', File.dirname(__FILE__))

begin
require 'syslog'
HAVE_SYSLOG = true
rescue LoadError
HAVE_SYSLOG = false
end

if HAVE_SYSLOG

module TestLogging
Expand Down