class Logger

Constants

LEVEL_TO_COLOR

Public Class Methods

Root(progname, base_level, &block) click to toggle source

Defines a logger on a module, allowing to use that module as a root in a hierarchy (i.e. having submodules use the Logger::Hierarchy support)

@param [String] progname is used as the logger's program name @param [Integer/Symbol] base_level is the level at which the logger is

initialized, this can be either a symbol from [:DEBUG, :INFO, :WARN,
:ERROR, :FATAL] or the integer constants from Logger::DEBUG,
Logger::INFO, etc.  This value is overriden if the BASE_LOG_LEVEL
environment variable is set.

If a block is given, it will be provided the message severity, time, program name and text and should return the formatted message.

This method creates a logger attribute in which the module can be accessed. Moreover, it includes Logger::Forward, which allows to access the logger's output methods on the module directly

@example

module MyModule
    extend Logger.Root('MyModule', Logger::WARN)
end

MyModule.info "text"
MyModule.warn "warntext"
Calls superclass method
# File lib/utilrb/logger/root.rb, line 42
def self.Root(progname, base_level, &block)
    begin  
        if ENV['BASE_LOG_LEVEL']
            env_level = ENV['BASE_LOG_LEVEL'].upcase.to_sym 
            # there is currently no disabled level on the ruby side
            # but fatal is the closest
            env_level = :FATAL if env_level == :DISABLE
            
            base_level = ::Logger.const_get( env_level ) 
        end
    rescue Exception
        raise ArgumentError, "Log level #{base_level} is not available in the ruby Logger"
    end

    console = @console
    formatter =
        if block then lambda(&block)
        elsif !LEVEL_TO_COLOR.empty?
            lambda do |severity, time, name, msg|
                LEVEL_TO_COLOR[severity].call("#{name}[#{severity}]: #{msg}\n")
            end
        else lambda { |severity, time, name, msg| "#{name}[#{severity}]: #{msg}\n" }
        end

    Module.new do
        include ::Logger::Forward
        include ::Logger::HierarchyElement

        def has_own_logger?; true end

        define_method :logger do
            if logger = super()
                return logger
            end

            logger = ::Logger.new(STDOUT)
            logger.level = base_level
            logger.progname = progname
            logger.formatter = formatter
            @__utilrb_hierarchy__default_logger = logger
        end
    end
end
pp_to_array(object) click to toggle source
# File lib/utilrb/logger/log_pp.rb, line 8
def self.pp_to_array(object)
    message =
        begin
            PP.pp(object, "")
        rescue Exception => formatting_error
            begin
              "error formatting object using pretty-printing\n" +
                  object.to_s +
              "\nplease report the formatting error: \n" + 
              formatting_error.full_message
            rescue Exception => formatting_error
              "\nerror formatting object using pretty-printing\n" +
                  formatting_error.full_message
            end
        end

    message.split("\n")
end

Public Instance Methods

color(*args) click to toggle source
# File lib/utilrb/logger/log_pp.rb, line 28
def color(*args)
    @color_generator ||= HighLine.new
    @color_generator.color(*args)
end
format_message(severity, datetime, progname, msg) click to toggle source

Overloaded from the base logger implementation to add the current indentation

# File lib/utilrb/logger/indent.rb, line 67
def format_message(severity, datetime, progname, msg)
    if !@nest_string
        @nest_string = " " * self.nest_size
    end
    msg = "#{@nest_string}#{msg}"
    (@formatter || @default_formatter).call(severity, datetime, progname, msg)
end
has_own_logger?() click to toggle source
# File lib/utilrb/logger/root.rb, line 70
def has_own_logger?; true end
io(level) click to toggle source
# File lib/utilrb/logger/io.rb, line 21
def io(level)
    LoggerIO.new(self, level)
end
log_pp(level, object, *first_line_format) click to toggle source
# File lib/utilrb/logger/log_pp.rb, line 34
def log_pp(level, object, *first_line_format)
    send(level) do
        first_line = !first_line_format.empty? && defined?(HighLine)
        self.class.pp_to_array(object).each do |line|
            if first_line
                line = color(line, *first_line_format)
                first_line = false
            end
            send(level, line)
        end
        break
    end
end
nest(size, log_level = nil) { || ... } click to toggle source

Adds a certain number number of spaces to the current indentation level

@overload nest(size)

Permanently adds a number of spaces to the current indentation
@param [Integer] size the number of spaces that should be added to
  {nest_size}

@overload nest(size) { }

Adds a number of spaces to the current indentation for the duration of
the block, and restores the original indentation afterwards.

@param [Integer] size the number of spaces that should be added to
  {nest_size}

@overload nest(size, log_level) { }

Shortcut for
@example

  logger.send(log_level) do
    logger.nest(size) do
      ...
    end
  end
# File lib/utilrb/logger/indent.rb, line 42
def nest(size, log_level = nil)
    if log_level
        send(log_level) do
            nest(size) do
                yield
            end
            return
        end
    end

    if block_given?
        begin
            current = self.nest_size
            self.nest_size += size
            yield
        ensure
            self.nest_size = current
        end
    else
        self.nest_size += size
    end
end
nest_size=(new_value) click to toggle source

Sets the absolute number of spaces that should be prepended to every message

You usually want to increment / decrement this with {nest}

# File lib/utilrb/logger/indent.rb, line 10
def nest_size=(new_value)
    @nest_string = nil
    if new_value < 0
        raise ArgumentError, "negative value for nest_size. You probably have unbalanced nest calls"
    end
    @nest_size = new_value
end
silent() { || ... } click to toggle source

Silences this logger for the duration of the block

# File lib/utilrb/logger/silent.rb, line 3
def silent
    current_level, self.level = self.level, Logger::FATAL + 1
    yield
ensure
    self.level = current_level
end