Class: Lapis::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/lapis.rb

Overview

Lapis::Logger is a simple, lightweight logging utility. It is fully customizable; in particular, you can customize the following:

  • levels – An array of symbols, which determines the valid levels. See

its’ documentation for more info.

  • formatter – A lambda, used for formatting output. Upon a call to log,

this will be called with the severity of the message and the message itself.

  • output_channel – An IO object, representing the destination for all

output.

Constant Summary collapse

DELEGATE_LEVELS =

Special levels that will be sent to @levels as method calls.

[:first, :last]
DEFAULT_LEVELS =

The default severity domain.

[:debug, :info, :warn, :error, :fatal]
DEFAULT_LEVEL =

The default severity threshhold.

:info
DEFAULT_FORMATTER =

The default formatting function.

lambda do |level, msg|
  output_channel.print "#{level}: #{msg}\n"
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out = $stdout, level = DEFAULT_LEVEL, levels = DEFAULT_LEVELS) {|_self| ... } ⇒ Logger

Returns a new instance of Logger.

Yields:

  • (_self)

Yield Parameters:

  • _self (Lapis::Logger)

    the object that the method was called on



47
48
49
50
51
52
53
# File 'lib/lapis.rb', line 47

def initialize(out = $stdout, level = DEFAULT_LEVEL, levels = DEFAULT_LEVELS)
  @output_channel = out
  @levels = levels
  @level = level
  @formatter = DEFAULT_FORMATTER
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



112
113
114
115
116
117
118
119
# File 'lib/lapis.rb', line 112

def method_missing(name, *args)
  return super unless @levels.include?(name)
  if args.length == 1
    log(name, args.first)
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1)"
  end
end

Instance Attribute Details

#formatterLambda

A lambda, used to format the output

Returns:

  • (Lambda)


41
42
43
# File 'lib/lapis.rb', line 41

def formatter
  @formatter
end

#levelSymbol

The minimum severity level required for printing

Returns:

  • (Symbol)


37
38
39
# File 'lib/lapis.rb', line 37

def level
  @level
end

#levelsArray<Symbol>

An array of all valid severity levels. Each levels’ offset in this array determines its relative importance.

Returns:

  • (Array<Symbol>)


33
34
35
# File 'lib/lapis.rb', line 33

def levels
  @levels
end

#output_channelIO (readonly)

The IO channel to output to

Returns:

  • (IO)


45
46
47
# File 'lib/lapis.rb', line 45

def output_channel
  @output_channel
end

Class Method Details

.dummyObject

A factory method for getting a dummy instance. Returns a new instance of Logger with an ‘empty’ lambda as its formatting function.



66
67
68
# File 'lib/lapis.rb', line 66

def self.dummy
  new { |l| l.formatter = lambda { |level, msg| } }
end

.open(file, level = DEFAULT_LEVEL, levels = DEFAULT_LEVELS) ⇒ Lapis::Logger

A factory method for logging to files. Functions exactly as the regular new method, expect instead of expecting an IO object, it expects the name of a file.

Parameters:

  • file (String)

    the name of the file to log output to

Returns:



60
61
62
# File 'lib/lapis.rb', line 60

def self.open(file, level = DEFAULT_LEVEL, levels = DEFAULT_LEVELS)
  new(File.open(file, "w"), level, levels)
end

Instance Method Details

#file?Boolean

Determine if we’re logging to a file.

Returns:

  • (Boolean)


89
90
91
# File 'lib/lapis.rb', line 89

def file?
  @output_channel.is_a? File
end

#important?(severity) ⇒ Boolean

Determine if the specified severity level is important (i.e., will be printed when that level is called).

Parameters:

  • severity (Symbol)

    the severity level to check

Returns:

  • (Boolean)

    whether the specified severity is important



84
85
86
# File 'lib/lapis.rb', line 84

def important?(severity)
  @levels.index(severity) >= @levels.index(@level) if @levels.index(severity)
end

#log(severity, msg) ⇒ void

This method returns an undefined value.

Log a message. severity is expected to be the name of a severity level, and msg is a string denoting the message to be logged. If we’re logging to a file, the file’s buffer will be flushed immediately after logging.

Parameters:

  • severity (Symbol)

    how severe the message is

  • msg (String)

    the message to be logged



99
100
101
102
# File 'lib/lapis.rb', line 99

def log(severity, msg)
  instance_exec(severity, msg, &@formatter) if important?(severity)
  output_channel.flush if file?
end