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 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
-
#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.
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
#formatter ⇒ Lambda
A lambda, used to format the output
41 42 43 |
# File 'lib/lapis.rb', line 41 def formatter @formatter end |
#level ⇒ Symbol
The minimum severity level required for printing
37 38 39 |
# File 'lib/lapis.rb', line 37 def level @level end |
#levels ⇒ Array<Symbol>
An array of all valid severity levels. Each levels’ offset in this array determines its relative importance.
33 34 35 |
# File 'lib/lapis.rb', line 33 def levels @levels end |
#output_channel ⇒ IO (readonly)
The IO channel to output to
45 46 47 |
# File 'lib/lapis.rb', line 45 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.
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.
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.
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).
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.
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 |