Class: Lapis::Logger
- Inherits:
-
Object
- Object
- Lapis::Logger
- 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
-
#formatter ⇒ Lambda
A lambda, used to format the output.
-
#level ⇒ Symbol
The minimum severity level required for printing.
-
#levels ⇒ Array<Symbol>
An array of all valid severity levels.
-
#output_channel ⇒ IO
readonly
The IO channel to output to.
Class Method Summary collapse
-
.dummy ⇒ Object
A factory method for getting a dummy instance.
-
.open(file, level = DEFAULT_LEVEL, levels = DEFAULT_LEVELS) ⇒ Lapis::Logger
A factory method for logging to files.
Instance Method Summary collapse
-
#file? ⇒ Boolean
Determine if we’re logging to a file.
-
#important?(severity) ⇒ Boolean
Determine if the specified severity level is important (i.e., will be printed when that level is called).
-
#initialize(out = $stdout, level = DEFAULT_LEVEL, levels = DEFAULT_LEVELS) {|_self| ... } ⇒ Logger
constructor
A new instance of Logger.
-
#log(severity, msg) ⇒ void
Log a message.
Constructor Details
#initialize(out = $stdout, level = DEFAULT_LEVEL, levels = DEFAULT_LEVELS) {|_self| ... } ⇒ Logger
Returns a new instance of Logger.
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
#formatter ⇒ Lambda
A lambda, used to format the output
36 37 38 |
# File 'lib/lapis.rb', line 36 def formatter @formatter end |
#level ⇒ Symbol
The minimum severity level required for printing
32 33 34 |
# File 'lib/lapis.rb', line 32 def level @level end |
#levels ⇒ Array<Symbol>
An array of all valid severity levels. Each levels’ offset in this array determines its relative importance.
28 29 30 |
# File 'lib/lapis.rb', line 28 def levels @levels end |
#output_channel ⇒ IO (readonly)
The IO channel to output to
40 41 42 |
# File 'lib/lapis.rb', line 40 def output_channel @output_channel end |
Class Method Details
.dummy ⇒ Object
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.
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.
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).
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.
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 |