Class: Logger

Inherits:
Object show all
Defined in:
lib/more/facets/logger.rb

Overview

Logger

Extended variation of Ruby’s standard Logger library. Mainly for compatibility purposes (with what?)

log = Logger.new

log.setup_format do |severity, timestamp, progname, msg|
  Logger::SIMPLE_FORMAT % [severity, msg]
end

Convention

When using debug level logger messages always append ‘if $DBG’ at the end. This hack is needed because Ruby does not support lazy evaluation (lisp macros).

TODO What’s all this about then?

Direct Known Subclasses

Console::Logger

Constant Summary collapse

SIMPLE_FORMAT =

Some available logging formats.

"%5s: %s\n"
DETAILED_FORMAT =
"%s %5s: %s\n"
TRACE_STYLES =

:nodoc:

{}

Instance Method Summary collapse

Instance Method Details

#format_procedureObject



139
140
141
# File 'lib/more/facets/logger.rb', line 139

def format_procedure
  @format_proc
end

#setup_format(&format_proc) ⇒ Object

Dictate the way in which this logger should format the messages it displays. This method requires a block. The block should return formatted strings given severity, timestamp, msg, progname.

Example

logger = Logger.new logger.setup_format do |severity, timestamp, msg, progname|

"#{progname}@#{timestamp} - #{severity}::#{msg}"

end



134
135
136
137
# File 'lib/more/facets/logger.rb', line 134

def setup_format(&format_proc)
  raise "Formating block needed" unless format_proc
  @format_proc = format_proc
end

#trace(expr, style = :p) ⇒ Object

Prints a trace message to DEBUGLOG (at debug level). Useful for emitting the value of variables, etc. Use like this:

x = y = 5
trace 'x'        # -> 'x = 5'
trace 'x ** y'   # -> 'x ** y = 3125'

If you have a more complicated value, like an array of hashes, then you’ll probably want to use an alternative output format. For instance:

trace 'value', :yaml

Valid output format values (the style parameter) are:

:p :inspect
:pp                     (pretty-print, using 'pp' library)
:s :to_s
:y :yaml :to_yaml       (using the 'yaml' library')

The default is :p.

CREDITS:

This code comes straight from the dev-utils Gem. Author: Gavin Sinclair <[email protected]>



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/more/facets/logger.rb', line 89

def trace(expr, style=:p)
  unless expr.respond_to? :to_str
    warn "trace: Can't evaluate the given value: #{caller.first}"
  else
    raise "FACETS: binding/or_caller is no longer possible"
    require "facets/core/binding/self/of_caller"

    Binding.of_caller do |b|
      value = b.eval(expr.to_str)
      formatter = TRACE_STYLES[style] || :inspect
      case formatter
      when :pp then require 'pp'
      when :y, :yaml, :to_yaml then require 'yaml'
      end
      value_s = value.send(formatter)
      message = "#{expr} = #{value_s}"
      lines = message.split(/\n/)
      indent = "   "
      debug(lines.shift)
      lines.each do |line|
        debug(indent + line)
      end
    end
  end
end