Sha256: 402847014bb691b8f13f31b77e85a5193019424324ce52bb04d5337bf8357be1

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 KB

Contents

require "logger"

class Logger

  def info_with_id message, context = {}
    # Ensure context is a hash of options and init
    context = {} unless context.is_a? Hash
    context[:level] ||= Logger::DEBUG

    # Build prefixes depending on this context
    prefix1 = build_prefix(context)
    prefix2 = build_prefix() + "   | "

    lines = context[:lines]

    if lines.is_a? Hash
      output = build_from_hash prefix2, lines

    elsif lines.is_a? Array
      output = build_from_array prefix2, lines

    else
      output = []

    end

    # Prepend plain message to output
    output.unshift prefix1 + message

    # Send all this to logger
    add context[:level], output
  end

  def build_prefix context = {}
    LOG_FORMAT_MESSAGE % [
      context[:wid].to_s,
      context[:jid].to_s,
      context[:id].to_s,
      context[:level].to_i+1
    ]
  end

  protected

    def build_from_array prefix, lines
      lines.map do |value|
        #text = value.to_s.strip[0..LOG_TRIM_LINE]
        text = value.to_s[0..LOG_TRIM_LINE]
        "#{prefix}#{text}"
      end
    end

    def build_from_hash prefix, lines
      lines.map do |name, value|
        text = value.to_s.strip[0..LOG_TRIM_LINE]
        "#{prefix}#{name}: #{text}"
      end
    end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rest-ftp-daemon-0.242.1 lib/rest-ftp-daemon/logger.rb
rest-ftp-daemon-0.242.0 lib/rest-ftp-daemon/logger.rb
rest-ftp-daemon-0.241 lib/rest-ftp-daemon/logger.rb
rest-ftp-daemon-0.240.2 lib/rest-ftp-daemon/logger.rb
rest-ftp-daemon-0.240.1 lib/rest-ftp-daemon/logger.rb
rest-ftp-daemon-0.240.0 lib/rest-ftp-daemon/logger.rb
rest-ftp-daemon-0.231.1 lib/rest-ftp-daemon/logger.rb
rest-ftp-daemon-0.231.0 lib/rest-ftp-daemon/logger.rb