Sha256: c6de1eb5828cc17db895f687f578e337aab508db323b446a9c225a0bf687cca5

Contents?: true

Size: 1.12 KB

Versions: 26

Compression:

Stored size: 1.12 KB

Contents

require "delegate"
require "fileutils"
require "logger"
require "sshkit"

module Airbrussh
  # A Pretty formatter that sends its output to a specified log file path.
  # LogFileFormatter takes care of creating the file (and its parent
  # directory) if it does not already exist, opens it for appending, and writes
  # a delimiter message. The file is automatically rotated if it reaches 20 MB.
  #
  class LogFileFormatter < SimpleDelegator
    attr_reader :path

    def initialize(path, formatter_class=SSHKit::Formatter::Pretty)
      @path = path
      ensure_directory_exists if path.is_a?(String)
      super(formatter_class.new(log_file_io))
      write_delimiter
    end

    private

    def write_delimiter
      delimiter = []
      delimiter << "-" * 75
      delimiter << "START #{Time.now} cap #{ARGV.join(' ')}"
      delimiter << "-" * 75
      delimiter.each do |line|
        write(SSHKit::LogMessage.new(SSHKit::Logger::INFO, line))
      end
    end

    def ensure_directory_exists
      FileUtils.mkdir_p(File.dirname(path))
    end

    def log_file_io
      @io ||= ::Logger.new(path, 1, 20_971_520)
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
airbrussh-1.5.3 lib/airbrussh/log_file_formatter.rb
airbrussh-1.5.2 lib/airbrussh/log_file_formatter.rb
airbrussh-1.5.1 lib/airbrussh/log_file_formatter.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/airbrussh-1.5.0/lib/airbrussh/log_file_formatter.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/airbrussh-1.5.0/lib/airbrussh/log_file_formatter.rb
airbrussh-1.5.0 lib/airbrussh/log_file_formatter.rb
airbrussh-1.4.2 lib/airbrussh/log_file_formatter.rb
airbrussh-1.4.1 lib/airbrussh/log_file_formatter.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/airbrussh-1.3.1/lib/airbrussh/log_file_formatter.rb
airbrussh-1.4.0 lib/airbrussh/log_file_formatter.rb
airbrussh-1.3.4 lib/airbrussh/log_file_formatter.rb
airbrussh-1.3.3 lib/airbrussh/log_file_formatter.rb
airbrussh-1.3.2 lib/airbrussh/log_file_formatter.rb
airbrussh-1.3.1 lib/airbrussh/log_file_formatter.rb
airbrussh-1.3.0 lib/airbrussh/log_file_formatter.rb
airbrussh-1.2.0 lib/airbrussh/log_file_formatter.rb
airbrussh-1.1.2 lib/airbrussh/log_file_formatter.rb
airbrussh-1.1.1 lib/airbrussh/log_file_formatter.rb
airbrussh-1.1.0 lib/airbrussh/log_file_formatter.rb
airbrussh-1.0.2 lib/airbrussh/log_file_formatter.rb