Sha256: fb575320666e272d3c49ebfc9e6956c9c355667686b4c8fc0e0754ae527e9785

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require "logger"
require "singleton"

# Logger interface class to access logger though symbolic names
module RestFtpDaemon
  class LoggerPool
    include Singleton

    def get pipe
      @loggers ||= {}
      @loggers[pipe] ||= create(pipe)
    end

    def create pipe
      # Compute logfile or STDERR, and declare what we're doing
      filename = logfile(pipe)

      # Create the logger and return it
      logger = Logger.new(filename, LOG_ROTATION)   #, 10, 1024000)
      logger.progname = pipe.to_s.downcase
      logger.formatter = Shared::LoggerFormatter

      # Finally return this logger
      logger

    rescue Errno::EACCES
      puts "LoggerPool [#{pipe}] failed: access error"
    end

  protected

    def logfile pipe
      # Disabled if no valid config
      return nil unless Conf[:logs].is_a?(Hash)

      # Compute logfile and check if we can write there
      logfile = File.expand_path(Conf[:logs][pipe].to_s, Conf[:logs][:path].to_s)

      # Check that we'll be able to create logfiles
      if File.exists?(logfile)
        # File is there, is it writable ?
        unless File.writable?(logfile)
          puts "LoggerPool [#{pipe}] disabled: file not writable [#{logfile}]"
          return nil
        end
      else
        # No file here, can we create it ?
        logdir = File.dirname(logfile)
        unless File.writable?(logdir)
          puts "LoggerPool [#{pipe}] disabled: directory not writable [#{logdir}]"
          return nil
        end
      end

      # OK, return a clean file path
      puts "LoggerPool [#{pipe}] logging to [#{logfile}]"
      return logfile
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rest-ftp-daemon-0.302.3 lib/rest-ftp-daemon/logger_pool.rb
rest-ftp-daemon-0.302.2 lib/rest-ftp-daemon/logger_pool.rb
rest-ftp-daemon-0.302.1 lib/rest-ftp-daemon/logger_pool.rb