Project

sonnet

0.0
Low commit activity in last 3 years
No release in over a year
Structured logging for Ruby applications
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Sonnet

JSON logging for Ruby applications.

Philosophy

  • Adhere to Ruby conventions as much as possible. Keep API as close as possible to Logger. If Sonnet were removed, your log lines should continue to work (though they might be ugly).
  • Configurable, to a point
  • For advanced use cases, see below alternatives.
  • Work as well for Ruby application as for Rails

Alternatives

Installation

Add this line to your application's Gemfile:

gem 'sonnet'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sonnet

Usage

require "sonnet"
logger = Sonnet::Logger.new(Logger.new(STDOUT))

Ruby on Rails

It shouldn't be as much of a pain as it is to configure Rails to log JSON. Even if you're able to do it, every gem introduces another logger that might have to be configured.

ActiveSupport::TaggedLogger

The easiest workaround is to just not use it. However, Rails uses it internally, and it modifies the logger passed to it! So you (currently) can't escape monkeypatching Rails.

Additionally, it is useful to be able to specify context info that gets logged with every line inside of a block. I have plans to possibly support #tagged, but I'm not sure what a sensible default is for converting string tags into a hash. Some options:

  • store in a tags array
  • attempt to guess at keys for each tag (possble if passing symbols to log_tags, but probably can't handle every use case).
  • Allow user to define keys for each tag

ActionDispatch::DebugExceptions

This middleware logs exceptions as strings. Since it is included by default, the easiest workaround is to monkeypatch it to log the exception rather than strings, which means we can handle it with our usual exception logging in Sonnet::Formatter.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/allspiritseve/sonnet.

License

The gem is available as open source under the terms of the MIT License.