Lapis
Lapis is a simple, extensible logging utility. It provides the bare minimum needed to function, and lets you do the rest. You can provide your own formatting function, and even customize the severity levels.
Installation
Add this line to your application's Gemfile:
gem 'lapis'
And then execute:
$ bundle
Or install it yourself as:
$ gem install lapis
Usage
The simplest use case is to make an instance of Lapis::Logger
and start
using it, which will provide a default formatting function and output to
$stdout
:
logger = Lapis::Logger.new
logger.info('Hello, world!') # => info: Hello, world!
If you want more customization, everything about the logger is customizable.
For instance, you can provide your own formatting function by setting the value
of formatter
:
logger = Lapis::Logger.new
logger.formatter = lambda do |level, msg|
output_channel.puts "[#{level.to_s.upcase}] #{msg}"
end
logger.info('Hello, world!') # => [INFO] Hello, world!
You can also customize the severity levels by setting the value of levels
to
an array. Levels should be ordered by their relative severity. You can do this
straight from the constructor, although you will have to pass in the output
channel explicitly. It is also recommended to set level
to something sensible
instead of the default, :info
:
# Doing everything in the constructor
Lapis::Logger.new($stdout, :bar, [:foo, :bar, :baz, :quux])
# Setting things explicitly
logger = Lapis::Logger.new
logger.levels = [:foo, :bar, :baz, :quux]
logger.level = :bar
In the example above, :foo
is the least important (equivalent to :debug
),
and :quux
is the most important (equivalent to :fatal
). Additionally, you
can automagically call each value:
logger = Lapis::Logger.new($stdout, :bar, [:foo, :bar, :baz, :quux])
logger.('Hello, world!') # => bar: Hello, world!
logger.foo('Hello, world!') # => No output
The Logger
class defines two factory methods, open
and dummy
.
The open
factory method is provided to easily log output to files:
logger = Lapis::Logger.open('foo.log')
logger.info('Hello, world!')
This is just shorthand for the following:
logger = Lapis::Logger.new(File.open('foo.log'))
logger.info('Hello, world!')
The dummy
factory method is provided to create a 'dummy' object which discards
all output:
logger = Lapis::Logger.dummy
logger.info('Hello, world!') # => does nothing
Internally, the dummy
method simply makes a new instance of Logger
and sets
its formatting function to an 'empty' lambda (i.e., a lambda that does nothing).
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request