class Logify::Logger

Constants

ANONYMOUS
DEBUG
DEFAULT
ERROR
FATAL
FILTERED
INFO
LEVEL_MAP
MAX_LENGTH
MONITOR
NEWLINE
NONE
PREFIX_DEBUG
PREFIX_ERROR
PREFIX_FATAL
PREFIX_INFO
PREFIX_LONG_DEBUG
PREFIX_LONG_ERROR
PREFIX_LONG_FATAL
PREFIX_LONG_INFO
PREFIX_LONG_WARN
PREFIX_WARN
SEPARATOR
WARN

Public Class Methods

level(name) click to toggle source

@private

@macro level

@method $1(message = nil, &block)
  Write a new $1 message to the current IO object.

  @example Write a +:$1+ message
    log.$1 'This is a message'

  @example Write a lazy evaluated +:$1+ message
    log.$1 { perform_complex_operation }

  @param [String] message
    the message to log
  @param [Proc] block
    the block to call that returns a string to write

  @return [String]
    the compiled log message
# File lib/logify/logger.rb, line 27
      def level(name)
        constant = name.to_s.upcase

        class_eval <<-EOH, __FILE__, __LINE__ + 1
          def #{name}(message = nil, &block)
            if Logify.level <= #{constant}
              buffer = ''

              if Logify.level == #{DEBUG}
                buffer << formatted_id
                buffer << SEPARATOR
                buffer << PREFIX_LONG_#{constant}
              else
                buffer << PREFIX_#{constant}
              end

              buffer << filter(message) if message
              buffer << filter(yield)   if block_given?
              buffer << "#{NEWLINE}"

              MONITOR.synchronize { Logify.io.write(buffer) }

              buffer
            end
          end
        EOH
      end
new(id) click to toggle source

Create a new logger object.

@param [String, nil] id

the ID of the logger object to create
# File lib/logify/logger.rb, line 105
def initialize(id)
  @id = id
end

Private Instance Methods

filter(string) click to toggle source

@private

Filter the given string of any filtered parameters.

@see Logify.filter

@param [String]

the string to filter

@return [String]

the filtered string
# File lib/logify/logger.rb, line 157
def filter(string)
  string.dup.tap do |copy|
    Logify.filters.each do |param, _|
      copy.gsub!(param, FILTERED)
    end
  end
end
formatted_id() click to toggle source

@private

The truncated id (for debug only).

@return [String]

the formatted id
# File lib/logify/logger.rb, line 119
def formatted_id
  return @formatted_id if @formatted_id

  # Account for anonymous classes
  id = @id ? @id.to_s : ANONYMOUS

  if id.length == MAX_LENGTH
    @formatted_id = id
  elsif id.length < MAX_LENGTH
    @formatted_id = id.rjust(MAX_LENGTH)
  else
    temp = id

    until temp.length <= MAX_LENGTH
      if temp.include?('::')
        temp = temp.split('::')[1..-1].join('::')
      else
        temp = id[-MAX_LENGTH..-1]
      end
    end

    @formatted_id = temp.rjust(MAX_LENGTH)
  end
end