-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
251 additions
and
1 deletion.
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,2 @@ | ||
class TestModel < ActiveRecord::Base | ||
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
9 changes: 9 additions & 0 deletions
9
example/rails-42/db/migrate/20171009132509_create_test_models.rb
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,9 @@ | ||
class CreateTestModels < ActiveRecord::Migration | ||
def change | ||
create_table :test_models do |t| | ||
t.string :foo | ||
|
||
t.timestamps null: false | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative "logger" | ||
require "logging" | ||
|
||
module Bugsnag::Breadcrumbs | ||
class Appender < Logging::Appender | ||
def initialize(level = Logger::INFO) | ||
super "Bugsnag", { :level => level } | ||
end | ||
|
||
def <<(message) | ||
return if closed? | ||
Bugsnag::Breadcrumbs.log_breadcrumb(message) | ||
end | ||
|
||
def append(event) | ||
return if closed? || !allow(event) | ||
|
||
metadata = if !event.method.empty? && !event.file.empty? && !event.line.empty? | ||
{ | ||
:trace => { | ||
:method => event.method, | ||
:file => event.file, | ||
:line => event.line | ||
}, | ||
:data => event.data | ||
} | ||
else | ||
event.data | ||
end | ||
|
||
severity = Bugsnag::Breadcrumbs.get_severity_name(event.level) | ||
Bugsnag::Breadcrumbs.log_breadcrumb(event.logger, metadata, severity) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require "logger" | ||
require "bugsnag" | ||
|
||
module Bugsnag::Breadcrumbs | ||
class Logger < Logger | ||
SEVERITIES = [ | ||
"debug", | ||
"info", | ||
"warn", | ||
"error", | ||
"fatal", | ||
"unknown" | ||
] | ||
|
||
def self.get_severity_name(severity) | ||
if (0..5).cover? severity | ||
SEVERITIES[severity] | ||
else | ||
severity | ||
end | ||
end | ||
|
||
def self.log_breadcrumb(name, data = nil, severity = "unknown") | ||
metadata = { | ||
:severity => severity | ||
} | ||
if data.is_a? Hash | ||
metadata.merge!(data) | ||
elsif !data.nil? | ||
metadata[:data] = data | ||
end | ||
|
||
Bugsnag.leave_breadcrumb(name, Bugsnag::Breadcrumbs::LOG_TYPE, metadata) | ||
end | ||
|
||
def initialize(level = Logger::INFO) | ||
@open = true | ||
super nil, level | ||
end | ||
|
||
def add(severity, message = nil, progname = nil) | ||
return unless @open | ||
|
||
if block_given? | ||
message = yield message | ||
elsif message.nil? | ||
message = progname | ||
end | ||
|
||
Bugsnag::Breadcrumbs::Logger.log_breadcrumb(message, { :progname => progname }, Bugsnag::Breadcrumbs::Logger.get_severity_name(severity)) if severity >= level | ||
end | ||
alias log add | ||
|
||
def <<(message) | ||
return unless @open | ||
Bugsnag::Breadcrumbs::Logger.log_breadcrumb(message) | ||
end | ||
|
||
def close | ||
@open = false | ||
true | ||
end | ||
|
||
def reopen | ||
@open = true | ||
true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# encoding: utf-8 | ||
|
||
# Necessary to avoid monkey patching thread methods | ||
ENV["LOGGING_INHERIT_CONTEXT"] = "false" | ||
|
||
require 'spec_helper' | ||
require 'logger' | ||
require 'logging' | ||
require 'bugsnag/logging/appender' | ||
|
||
describe Bugsnag::Logging::Appender do | ||
|
||
before do | ||
@appender = Bugsnag::Logging::Appender.new | ||
end | ||
|
||
it "writes breadcrumbs" do | ||
expect(Bugsnag).to receive(:leave_breadcrumb).with( | ||
"message", | ||
"log", | ||
{ | ||
:severity => "unknown" | ||
} | ||
) | ||
@appender << "message" | ||
end | ||
|
||
it "write breadcrumbs from a logevent" do | ||
expect(Bugsnag).to receive(:leave_breadcrumb).with( | ||
"testLogger", | ||
"log", | ||
{ | ||
:data => ["message1", "message2"], | ||
:severity => "info" | ||
} | ||
) | ||
logevent = Logging::LogEvent.new("testLogger", Logger::INFO, ["message1", "message2"], false) | ||
@appender.append logevent | ||
end | ||
|
||
it "adds trace metadata if available" do | ||
expect(Bugsnag).to receive(:leave_breadcrumb).with( | ||
"testLogger", | ||
"log", | ||
{ | ||
:trace => hash_including(:method, :file, :line), | ||
:data => ["message1", "message2"], | ||
:severity => "info" | ||
} | ||
) | ||
logevent = Logging::LogEvent.new("testLogger", Logger::INFO, ["message1", "message2"], true) | ||
@appender.append logevent | ||
end | ||
|
||
it "doesn't write if closed" do | ||
expect(Bugsnag).to_not receive(:leave_breadcrumb) | ||
@appender.close | ||
@appender << "message" | ||
logevent = Logging::LogEvent.new("testLogger", Logger::INFO, ["message1", "message2"], false) | ||
@appender.append logevent | ||
end | ||
|
||
it "is an appender and a bugsnag appender" do | ||
expect(@appender.class.ancestors).to include(Bugsnag::Logging::Appender, Logging::Appender) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
require 'logger' | ||
require 'bugsnag/breadcrumbs/logger' | ||
|
||
describe Bugsnag::Breadcrumbs::Logger do | ||
|
||
before do | ||
@logger = Bugsnag::Breadcrumbs::Logger.new | ||
end | ||
|
||
it "writes by default" do | ||
expect(Bugsnag).to receive(:leave_breadcrumb).with( | ||
"message", | ||
"log", | ||
{ | ||
:severity => "unknown" | ||
} | ||
) | ||
@logger << "message" | ||
end | ||
|
||
it "doesn't write when closed" do | ||
expect(Bugsnag).to_not receive(:leave_breadcrumb) | ||
@logger.close | ||
@logger << "message" | ||
end | ||
|
||
it "writes after being re-opened" do | ||
expect(Bugsnag).to receive(:leave_breadcrumb).with( | ||
"message", | ||
"log", | ||
{ | ||
:severity => "unknown" | ||
} | ||
) | ||
@logger.close | ||
@logger.reopen | ||
@logger << "message" | ||
end | ||
|
||
it "allows a progname and severity" do | ||
expect(Bugsnag).to receive(:leave_breadcrumb).with( | ||
"message", | ||
"log", | ||
{ | ||
:progname => "logTests", | ||
:severity => "info" | ||
} | ||
) | ||
@logger.add Logger::INFO, "message", "logTests" | ||
end | ||
|
||
it "is a logger and a bugsnag logger" do | ||
expect(@logger.class.ancestors).to include(Bugsnag::Breadcrumbs::Logger, Logger) | ||
end | ||
end | ||
|