From 29e2eaed26a2631a3eeabbf71230d9ffaddf3360 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Sun, 14 Jul 2024 15:54:27 +0900 Subject: [PATCH] Require syslog only when necessary Requiring syslog on Ruby 3.3.0 results in an warning if the gem is not explicitly installed. Suppress it by requiring syslog only when the user tries to use Logging::Appender::Syslog. --- lib/logging.rb | 7 ------- lib/logging/appenders.rb | 14 +++++++++++++- lib/logging/appenders/syslog.rb | 14 +------------- test/appenders/test_syslog.rb | 7 +++++++ 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/logging.rb b/lib/logging.rb index eb4668ad..8620d6cc 100644 --- a/lib/logging.rb +++ b/lib/logging.rb @@ -6,13 +6,6 @@ require 'little-plugger' require 'multi_json' -begin - require 'syslog' - HAVE_SYSLOG = true -rescue LoadError - HAVE_SYSLOG = false -end - # # module Logging diff --git a/lib/logging/appenders.rb b/lib/logging/appenders.rb index f221f8f1..119059dd 100644 --- a/lib/logging/appenders.rb +++ b/lib/logging/appenders.rb @@ -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') @@ -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 diff --git a/lib/logging/appenders/syslog.rb b/lib/logging/appenders/syslog.rb index 818e0b44..387d4a82 100644 --- a/lib/logging/appenders/syslog.rb +++ b/lib/logging/appenders/syslog.rb @@ -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. # @@ -211,4 +200,3 @@ def syslog_level_num( level ) end # Syslog end # Logging::Appenders -end # HAVE_SYSLOG diff --git a/test/appenders/test_syslog.rb b/test/appenders/test_syslog.rb index 7411f458..14962be5 100644 --- a/test/appenders/test_syslog.rb +++ b/test/appenders/test_syslog.rb @@ -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