Sha256: 0a409f372b8b6d914e1f99821ccb5046713fb26e532a46fcd9b708fcbe3ae5ad

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

require 'logger'
require_relative 'log_repository'
require_relative 'log_factory_log_formatter'

module RTALogger
  # show log items on console out put
  class LogRepositoryFile < LogRepository
    def initialize(file_path = 'log.txt', period = 'daily', shift_size = 1_048_576)
      super()
      @file_path = file_path
      @period = period
      @shift_size = shift_size
      @file_logger = create_ruby_logger(@file_path, @period, @shift_size)
    end

    def load_config(config_json)
      super

      file_path = config_json['file_path'].to_s
      period = config_json['roll_period'].to_s
      shift_size = config_json['roll_size'].nil? ? 1_048_576 : config_json['roll_size'].to_i
      @file_logger = create_ruby_logger(file_path, period, shift_size)
    end

    def to_builder
      json = super
      json.enable enable
      json.file_path @file_path
      json.period @period
      json.shift_size @shift_size

      json
    end

    # register :file

    def apply_run_time_config(config_json)
      super config_json

      @formatter.colorize = false
    end

    protected

    def create_ruby_logger(file_path, period, shift_size)
      ruby_logger = Logger.new(file_path, period, shift_size)
      ruby_logger.level = Logger::Severity::DEBUG
      ruby_logger.formatter = proc do |_severity, _datetime, _progname, msg|
        "#{msg}\n"
      end
      ruby_logger
    end

    def flush_and_clear
      @semaphore.synchronize do
        @log_records.each { |log_record| @file_logger.debug(@formatter.format(log_record)) }
      end
      super
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
RTALogger-2.4.0 lib/log_repository_file.rb
RTALogger-2.3.1 lib/log_repository_file.rb
RTALogger-2.3.0 lib/log_repository_file.rb
RTALogger-2.2.2 lib/log_repository_file.rb
RTALogger-2.2.0 lib/log_repository_file.rb