Sha256: 8fe75901dfcb0a83390d4e8384e3b3cce220981692faf9bfb32d283c6e713216

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 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.3.0 lib/hourly_logger_rotator.rb
hourly_logger_rotator-0.2.0 lib/hourly_logger_rotator.rb