Sha256: d3e7b698d3bef1b20044e0ec2904e2643d7b30d84fcb89c194e14672d55c0967

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require "logger"

module BmcDaemonLib
  module LoggerHelper

  protected

    def log_info message, details = nil
      build_messages Logger::INFO, message, details
    end

    def log_error message, details = nil
      build_messages Logger::ERROR, message, details
    end

    def log_debug message, details = nil
      build_messages Logger::DEBUG, message, details
    end

    alias info log_info
    alias error log_error
    alias debug log_debug

  private

    # Builds prefix if LOG_PREFIX_FORMAT defined and caller has log_prefix method to provide values
    def build_prefix
      # Skip if no values from user class
      return unless respond_to?(:log_prefix, true)
      values = log_prefix

      # Skip if no format defined
      return unless defined?('LOG_PREFIX_FORMAT')
      return unless LOG_PREFIX_FORMAT.is_a? String

      # Build prefix string
      LOG_PREFIX_FORMAT % values.map(&:to_s)
    end

    def build_messages severity, message, details = nil
      messages = []

      prefix = build_prefix

      # Add main message
      messages << sprintf(LOG_MESSAGE_TEXT, prefix, message) if message

      # Add details from array
      details.each do |line|
        messages << sprintf(LOG_MESSAGE_ARRAY, prefix, line)
      end if details.is_a? Array

      # Add details from hash
      details.each do |key, value|
        messages << sprintf(LOG_MESSAGE_HASH, prefix, key, value)
      end if details.is_a? Hash

      # Return all that stuff
      logger.add severity, messages
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bmc-daemon-lib-0.2.0 lib/bmc-daemon-lib/logger_helper.rb