# SlackLogDevice LogDevice implementation that post logs on a Slack channel. ## Setup Just add this into your `Gemfile`: ```ruby gem 'slack_log_device' ``` Then, just run a `bundle install`. ## Usage ```ruby 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 `slack_log_device` provides a log formatter to have a pretty output for slack. It can be configured like this: ```ruby logger.formatter = SlackLogDevice.formatter ``` `SlackLogDevice.formatter` method also accepts block to transform logged message, example: ```ruby logger.formatter = SlackLogDevice.formatter { |message| message.reverse } ``` You can also add custom metadata to message sent to slack, here is how to do it: ```ruby logger.formatter = SlackLogDevice.formatter(extra_metadata: { 'User' => -> (options) { ENV['USER'] }, 'Machine' => `uname -a`, }) ``` If you use [Ruby on Rails](http://rubyonrails.org/), 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). ## Rails configuration For a rails application, it is recommanded to use following configuration into `config/environments/production.rb` file: ```ruby 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](http://github.com/rspec/rspec). Just run `bundle exec rake` (after a `bundle install`).