Sha256: 2a98fc02a6e42bdc1ea0cd5156a1d6fe2165f133e0d0e87c63c44ee35394c60e

Contents?: true

Size: 1.28 KB

Versions: 26

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.exists?(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

26 entries across 26 versions & 1 rubygems

Version Path
ree_lib-1.0.26 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.24 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.23 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.22 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.21 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.20 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.19 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.18 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.17 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.16 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.15 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.14 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.13 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.12 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.11 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.10 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.9 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.8 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.7 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb
ree_lib-1.0.6 lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb