This repository has been archived by the owner on Mar 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
logger_spec.rb
148 lines (125 loc) · 4.6 KB
/
logger_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require "spec_helper"
module Rails
def self.logger
@logger
end
def self.logger=(logger)
@logger = logger
end
end
module HoptoadNotifier
def self.notify(e)
end
end
module Airbrake
def self.notify(e)
end
end
describe Rapns::Daemon::Logger do
let(:log) { stub(:sync= => true) }
let(:config) { stub(:airbrake_notify => true) }
before do
Rails.stub(:root).and_return("/rails_root")
@buffered_logger = mock("BufferedLogger", :info => nil, :error => nil, :level => 0, :auto_flushing => 1, :auto_flushing= => nil)
Rails.logger = @buffered_logger
ActiveSupport::BufferedLogger.stub(:new).and_return(@buffered_logger)
Rapns::Daemon.stub(:config => config)
File.stub(:open => log)
end
it "should open the a log file in the Rails log directory" do
File.should_receive(:open).with('/rails_root/log/rapns.log', 'w')
Rapns::Daemon::Logger.new(:foreground => true)
end
it 'sets sync mode on the log descriptor' do
log.should_receive(:sync=).with(true)
Rapns::Daemon::Logger.new(:foreground => true)
end
it 'instantiates the BufferedLogger' do
ActiveSupport::BufferedLogger.should_receive(:new).with(log, Rails.logger.level)
Rapns::Daemon::Logger.new(:foreground => true)
end
it "should print out the msg if running in the foreground" do
logger = Rapns::Daemon::Logger.new(:foreground => true)
logger.should_receive(:puts).with(/hi mom/)
logger.info("hi mom")
end
it "should not print out the msg if not running in the foreground" do
logger = Rapns::Daemon::Logger.new(:foreground => false)
logger.should_not_receive(:puts).with(/hi mom/)
logger.info("hi mom")
end
it "should prefix log lines with the current time" do
now = Time.now
Time.stub(:now).and_return(now)
logger = Rapns::Daemon::Logger.new(:foreground => false)
@buffered_logger.should_receive(:info).with(/#{Regexp.escape("[#{now.to_s(:db)}]")}/)
logger.info("blah")
end
it "should prefix error logs with the ERROR label" do
logger = Rapns::Daemon::Logger.new(:foreground => false)
@buffered_logger.should_receive(:error).with(/#{Regexp.escape("[ERROR]")}/)
logger.error("eeek")
end
it "should prefix warn logs with the WARNING label" do
logger = Rapns::Daemon::Logger.new(:foreground => false)
@buffered_logger.should_receive(:warn).with(/#{Regexp.escape("[WARNING]")}/)
logger.warn("eeek")
end
it "should handle an Exception instance" do
e = RuntimeError.new("hi mom")
e.stub(:backtrace => [])
logger = Rapns::Daemon::Logger.new(:foreground => false)
@buffered_logger.should_receive(:error).with(/RuntimeError, hi mom/)
logger.error(e)
end
it "should notify Airbrake of the exception" do
e = RuntimeError.new("hi mom")
e.stub(:backtrace => [])
logger = Rapns::Daemon::Logger.new(:foreground => false, :airbrake_notify => true)
Airbrake.should_receive(:notify_or_ignore).with(e)
logger.error(e)
end
context "without Airbrake defined" do
before do
Object.send(:remove_const, :Airbrake)
end
after do
module Airbrake
def self.notify(e)
end
end
end
it "should notify using HoptoadNotifier" do
e = RuntimeError.new("hi mom")
e.stub(:backtrace => [])
logger = Rapns::Daemon::Logger.new(:foreground => false, :airbrake_notify => true)
HoptoadNotifier.should_receive(:notify_or_ignore).with(e)
logger.error(e)
end
end
it "should not notify Airbrake of the exception if the airbrake_notify option is false" do
e = RuntimeError.new("hi mom")
e.stub(:backtrace => [])
logger = Rapns::Daemon::Logger.new(:foreground => false, :airbrake_notify => false)
Airbrake.should_not_receive(:notify_or_ignore).with(e)
logger.error(e)
end
it "should not notify Airbrake if explicitly disabled in the call to error" do
e = RuntimeError.new("hi mom")
e.stub(:backtrace => [])
logger = Rapns::Daemon::Logger.new(:foreground => false, :airbrake_notify => true)
Airbrake.should_not_receive(:notify_or_ignore).with(e)
logger.error(e, :airbrake_notify => false)
end
it "should not attempt to notify Airbrake of the error is not an Exception" do
logger = Rapns::Daemon::Logger.new(:foreground => false)
Airbrake.should_not_receive(:notify_or_ignore)
logger.error("string error message")
end
it 'defaults auto_flushing to true if the Rails logger does not respond to auto_flushing' do
rails_logger = mock(:info => nil, :error => nil, :level => 0)
Rails.logger = rails_logger
logger = Rapns::Daemon::Logger.new({})
@buffered_logger.auto_flushing.should be_true
end
end