Sha256: eef704b381522b982e439dc1e90a440acce291a5ee8817c9f55a26b1c331113e

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require "logger"
require "hourly_logger_rotator/version"

pattern = File.join(File.dirname(__FILE__), "hourly_logger_rotator", "patch", "*.rb")
Dir[pattern].each { |file| require_relative(file) }

module HourlyLoggerRotator
  class << self
    def add_hourly_rotation_period_support!
      if hourly_mixin
        Logger::LogDevice.prepend(hourly_mixin)
      else
        warn unsupported_warning
      end
    end

    def default_rotation_period=(period)
      raise unsupported_warning if period == "hourly" && !hourly_mixin

      mixin = Module.new do
        define_method(:initialize) do |logdev, shift_age = period, *args|
          super(logdev, shift_age, *args)
        end
      end

      Logger.prepend(mixin)
    end

    private

    def hourly_mixin
      return @mixin if defined?(@mixin)

      mixin_name = "HourlyLoggerRotator::Patch::Ruby_#{RUBY_VERSION.split('.').first(2).join('_')}"

      @mixin =
        begin
          Module.const_get(mixin_name)
        rescue NameError
          nil
        end
    end

    def unsupported_warning
      "Hourly log rotation period is not supported for Ruby #{RUBY_VERSION}"
    end
  end
end

HourlyLoggerRotator.add_hourly_rotation_period_support!

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hourly_logger_rotator-0.4.0.60 lib/hourly_logger_rotator.rb
hourly_logger_rotator-0.3.0.54 lib/hourly_logger_rotator.rb