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 value of @levels.

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

The default value of @level.

:info

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



42
43
44
45
46
47
48
# File 'lib/lapis.rb', line 42

def initialize(out=$stdout, level=DEFAULT_LEVEL, levels=DEFAULT_LEVELS)
  @output_channel = out
  @levels = levels
  @level = level
  @formatter = -> (level, msg) { output_channel.print "#{level}: #{msg}\n" }
  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.



107
108
109
110
111
112
113
114
# File 'lib/lapis.rb', line 107

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)


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

def formatter
  @formatter
end

#levelSymbol

The minimum severity level required for printing

Returns:

  • (Symbol)


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

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>)


28
29
30
# File 'lib/lapis.rb', line 28

def levels
  @levels
end

#output_channelIO (readonly)

The IO channel to output to

Returns:

  • (IO)


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

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.



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

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:



55
56
57
# File 'lib/lapis.rb', line 55

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)


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

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



79
80
81
# File 'lib/lapis.rb', line 79

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



94
95
96
97
# File 'lib/lapis.rb', line 94

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