Low commit activity in last 3 years
No release in over a year
LogDevice implementation that post logs on a Slack channel
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 9.0.0, < 12.0.0
>= 12.0.0, < 14.0.0
>= 3.5.0, < 3.11.0
>= 1.25.0, < 2.0.0
>= 0.6.0, < 1.0.0
>= 2.8.0, < 3.0.0

Runtime

>= 7.0.0, < 8.0.0
>= 0.14.0, < 0.21.0
 Project Readme

SlackLogDevice

LogDevice implementation that post logs on a Slack channel.

Setup

Just add this into your Gemfile:

gem 'slack_log_device'

Then, just run a bundle install.

Usage

require 'slack_log_device'

logger = Logger.new(SlackLogDevice.new(webhook_url: 'https://hooks.slack.com/services/...', username: 'MyApp'))
logger.level = Logger::INFO
logger.warn('BAM!')

Then, the logged message will be writen to webhook's configured channel.

Note that the messages written are buffered in order to avoid consecutive request.

Options

  • auto_flush: To flush messages directly when a message is written (disabled by default).
  • channel: The channel to post message on (webhook configured channel by default). It can be a channel (if starting with #) or a specific user (if starting with a @).
  • flush_delay: The delay in seconds to send buffered messages (1 by default).
  • max_buffer_size: The max messages count to flush them (10 messages by default).
  • timeout: The timeout in seconds to send message to slack (5 by default).
  • username: The username to post message as (nil by default).
  • webhook_url: The URL of the webhook (mandatory).

Formatter

Usage

slack_log_device provides a log formatter to have a pretty output for slack. It can be configured like this:

logger.formatter = SlackLogDevice.formatter

Converting message

SlackLogDevice.formatter method also accepts block to transform logged message, example:

logger.formatter = SlackLogDevice.formatter { |message| message.reverse }

Metadata

By default, formatter adds those metadata:

  • PID: The current process id.
  • User: The current user (ENV['USER']).
  • Machine: The machine name (hostname).

To disable default metadata (User, Machine and PID), set disable_default_metadata formatter option to true.

You can also add custom metadata to message sent to slack, here is how to do it:

logger.formatter = SlackLogDevice.formatter(extra_metadata: {
  'Exception class' => -> (options) { options[:exception].class.name },
  'System' => `uname -a`,
})

As you can see, blocks (invoked with options contains request and exception) are supported.

Backtrace

Exception backtrace is automatically stripped to 10 lines. You can change this behavior via :max_backtrace_lines:

logger.formatter = SlackLogDevice.formatter(max_backtrace_lines: 50)
  • A value of 0 will not print backtrace.
  • A value of -1 will not strip backtrace.

Rails metadata

If you use Ruby on Rails, you will get some metadata like current HTTP method, URL, remote address and User-Agent (see Rails configuration section).

If you need more, use :extra_metadata option. Note that blocks specified with :extra_metadata option are invoked with options that may contains :request option (if present).

Icon emojis

By default, there is a different icon emoji for each logging severity level. But, it can be configured:

logger.formatter = SlackLogDevice.formatter(max_backtrace_lines: 50, icon_emojis: { fatal: ':metal:', warn: ':skull:' })

Or for all severity levels:

logger.formatter = SlackLogDevice.formatter(max_backtrace_lines: 50, icon_emoji: ':metal:')

Rails configuration

For a rails application, it is recommanded to use following configuration into config/environments/production.rb file:

SlackLogDevice.enable_rails_logging!
config.logger = ActiveSupport::Logger.new(SlackLogDevice.new(webhook_url: 'https://hooks.slack.com/services/...', username: 'MyRailsApp'))
config.logger.formatter = SlackLogDevice.formatter
config.log_level = :warn

SlackLogDevice.enable_rails_logging! instruction put current request into thread in order to make it available by slack formatter.

Executing test suite

This project is fully tested with Rspec 3. Just run bundle exec rake (after a bundle install).