-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from rollbar/rollbar-logger
Rollbar logger
- Loading branch information
Showing
5 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require 'logger' | ||
require 'rollbar' | ||
|
||
module Rollbar | ||
# This class provides logger interface that can be used to replace | ||
# the application logger and send all the log messages to Rollbar | ||
# | ||
# Usage: | ||
# require 'rollbar/logger' | ||
# logger = Rollbar::Logger.new | ||
# logger.error('Error processing purchase') | ||
# | ||
# If using Rails, you can extend the Rails logger so messages are logged | ||
# normally and also to Rollbar: | ||
# | ||
# Rails.logger.extend(ActiveSupport::Logger.broadcast(Rollbar::Logger.new)) | ||
class Logger < ::Logger | ||
class Error < RuntimeError; end | ||
class DatetimeFormatNotSupported < Error; end | ||
class FormatterNotSupported < Error; end | ||
|
||
def initialize | ||
@level = ERROR | ||
end | ||
|
||
def add(severity, message = nil, progname = nil) | ||
return true if severity < @level | ||
|
||
message ||= block_given? ? yield : progname | ||
|
||
return true if message.blank? | ||
|
||
rollbar.log(rollbar_level(severity), message) | ||
end | ||
|
||
def <<(message) | ||
error(message) | ||
end | ||
|
||
def formatter=(_) | ||
raise(FormatterNotSupported) | ||
end | ||
|
||
def formatter | ||
raise(FormatterNotSupported) | ||
end | ||
|
||
def datetime_format=(_) | ||
raise(DatetimeFormatNotSupported) | ||
end | ||
|
||
def datetime_format | ||
raise(DatetimeFormatNotSupported) | ||
end | ||
|
||
# Returns a Rollbar::Notifier instance with the current global scope and | ||
# with a logger writing to /dev/null so we don't have a infinite loop | ||
# when Rollbar.configuration.logger is Rails.logger. | ||
def rollbar | ||
notifier = Rollbar.scope | ||
notifier.configuration.logger = ::Logger.new('/dev/null') | ||
|
||
notifier | ||
end | ||
|
||
private | ||
|
||
# Find correct Rollbar level to use using the indexes in Logger::Severity | ||
# DEBUG = 0 | ||
# INFO = 1 | ||
# WARN = 2 | ||
# ERROR = 3 | ||
# FATAL = 4 | ||
# UNKNOWN = 5 | ||
# | ||
# If not found we'll use 'error' as the used level | ||
def rollbar_level(severity) | ||
[:debug, :info, :warning, :error, :critical, :error][severity] || :error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
require 'spec_helper' | ||
require 'rollbar/logger' | ||
|
||
describe Rollbar::Logger do | ||
describe '#add' do | ||
context 'with severity under level' do | ||
it 'returns true' do | ||
result = subject.add(Logger::DEBUG, 'foo') | ||
|
||
expect(result).to be_truthy | ||
end | ||
end | ||
|
||
context 'with blank message' do | ||
it 'returns true' do | ||
result = subject.add(subject.level) | ||
|
||
expect(result).to be_truthy | ||
end | ||
end | ||
|
||
context 'with ERROR severity' do | ||
let(:message) { 'foo' } | ||
|
||
it 'calls Rollbar to send the message' do | ||
expect_any_instance_of(Rollbar::Notifier).to receive(:log).with(:error, message) | ||
|
||
subject.add(Logger::ERROR, message) | ||
end | ||
end | ||
|
||
context 'with FATAL severity' do | ||
let(:message) { 'foo' } | ||
|
||
it 'calls Rollbar to send the message with critical level' do | ||
expect_any_instance_of(Rollbar::Notifier).to receive(:log).with(:critical, message) | ||
|
||
subject.add(Logger::FATAL, message) | ||
end | ||
end | ||
|
||
context 'with UNKNOWN severity' do | ||
let(:message) { 'foo' } | ||
|
||
it 'calls Rollbar to send the message with error level' do | ||
expect_any_instance_of(Rollbar::Notifier).to receive(:log).with(:error, message) | ||
|
||
subject.add(Logger::UNKNOWN, message) | ||
end | ||
end | ||
|
||
context 'with out of range severity' do | ||
let(:message) { 'foo' } | ||
|
||
it 'calls Rollbar to send the message with error level' do | ||
expect_any_instance_of(Rollbar::Notifier).to receive(:log).with(:error, message) | ||
|
||
subject.add(10, message) | ||
end | ||
end | ||
end | ||
|
||
describe '#<<' do | ||
let(:message) { 'foo' } | ||
|
||
it 'calls #error' do | ||
expect(subject).to receive(:error).with(message) | ||
|
||
subject << message | ||
end | ||
end | ||
|
||
describe '#formatter=' do | ||
it 'fails with FormatterNotSupported' do | ||
expect do | ||
subject.formatter = double | ||
end.to raise_error(Rollbar::Logger::FormatterNotSupported) | ||
end | ||
end | ||
|
||
describe '#formatter' do | ||
it 'fails with FormatterNotSupported' do | ||
expect do | ||
subject.formatter | ||
end.to raise_error(Rollbar::Logger::FormatterNotSupported) | ||
end | ||
end | ||
|
||
describe '#datetime_format=' do | ||
it 'fails with DatetimeFormatNotSupported' do | ||
expect do | ||
subject.datetime_format = double | ||
end.to raise_error(Rollbar::Logger::DatetimeFormatNotSupported) | ||
end | ||
end | ||
|
||
describe '#datetime_format' do | ||
it 'fails with DatetimeFormatNotSupported' do | ||
expect do | ||
subject.datetime_format | ||
end.to raise_error(Rollbar::Logger::DatetimeFormatNotSupported) | ||
end | ||
end | ||
|
||
describe '#rollbar' do | ||
it 'returns a Rollbar notifier with a logger pointing to /dev/null' do | ||
notifier = subject.rollbar | ||
logger = notifier.configuration.logger | ||
logdev = logger.instance_eval { @logdev } | ||
|
||
expect(logdev.filename).to be_eql('/dev/null') | ||
end | ||
end | ||
end |