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.124 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb