Sha256: cecb65b9a9167136fe329b6daafd8cf1a19edbc0b327e39e655d01671b811913

Contents?: true

Size: 1.28 KB

Versions: 98

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require_relative 'appender'

class ReeLogger::FileAppender < ReeLogger::Appender
  include Ree::LinkDSL

  link 'ree_logger/formatters/default_formatter', -> { DefaultFormatter }
  link 'ree_logger/formatters/formatter', -> { Formatter }

  DEFAULTS = {
    auto_flush: false,
    log_file_count: 10,
    log_file_size: 1048576 # 1.megabyte
  }.freeze

  attr_reader :file, :auto_flush, :logger

  contract(
    Symbol,
    Nilor[Formatter],
    String,
    Ksplat[
      auto_flush?: Bool,
      log_file_count?: Integer,
      log_file_size?: Integer
    ] => Any
  )
  def initialize(level, formatter, file_path, **opts)
    super(
      level,
      formatter || DefaultFormatter.new
    )

    opts = DEFAULTS.merge(opts)

    unless File.exist?(file_path)
      FileUtils.mkdir_p(Pathname.new(file_path).parent.to_s)
      FileUtils.touch(file_path)
    end

    @auto_flush = opts[:auto_flush]
    @file = File.open(file_path, File::WRONLY | File::APPEND)

    @logger = Logger.new(
      @file, opts[:log_file_count], opts[:log_file_size]
    )
  end

  contract(ReeLogger::LogEvent, Nilor[String] => nil)
  def append(event, progname = nil)
    message = @formatter.format(event, progname)
    logger << (message + "\n")
    file.flush if auto_flush

    nil
  end
end

Version data entries

98 entries across 98 versions & 1 rubygems

Version Path
ree_lib-1.0.104 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.103 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.102 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.101 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.100 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.99 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.98 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.97 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.96 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.95 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.94 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.93 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.92 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.91 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.90 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.89 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.88 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.87 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.86 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.85 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb