lumberjack 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d18e80866556c7f4fb366d5b3877e18471c5a13b
4
+ data.tar.gz: eabc812fc6145223e566b85d4197a431e6127a18
5
+ SHA512:
6
+ metadata.gz: 35e888599a3613e410685282574d3f02be1ede309d51633095a8a73bd2a48a00b0d19d2c6c9f7bc14f4a90e603d0f011f64e7fd9c05760ad4498c25ca134a775
7
+ data.tar.gz: 5a0814c7a3b66427fa24e5a4f0d957f4bd259bc0cc252c84db8a5462929096bde2d4ff7e3267042754c7c51b44477f62244aac281584699e2a0ab4a46c66c0cb
data/README.rdoc CHANGED
@@ -98,3 +98,35 @@ The built in stream based logging devices use an internal buffer. The size of th
98
98
  The built in devices include two that can automatically roll log files based either on date or on file size. When a log file is rolled, it will be renamed with a suffix and a new file will be created to receive new log entries. This can keep your log files from growing to unusable sizes and removes the need to schedule an external process to roll the files.
99
99
 
100
100
  There is a similar feature in the standard library Logger class, but the implementation here is safe to use with multiple processes writing to the same log file.
101
+
102
+ == Examples
103
+
104
+ These example are for Rails applications, but there is no dependency on Rails for using this gem. Most of the examples are applicable to any Ruby application.
105
+
106
+ In a Rails application you can replace the default production logger by adding this to your config/environments/production.rb file:
107
+
108
+ # Add the unit of work id to each request
109
+ config.middleware.insert(0, Lumberjack::Rack::UnitOfWork)
110
+ # Change the logger to use Lumberjack
111
+ log_file_path = Rails.root + "log" + "#{Rails.env}.log"
112
+ config.logger = Lumberjack::Logger.new(log_file, :level => :warn)
113
+
114
+ To set up a logger to roll every day at midnight, you could use this code (you can also specify :weekly or :monthly):
115
+
116
+ config.logger = Lumberjack::Logger.new(log_file_path, :roll => :daily)
117
+
118
+ To set up a logger to roll log files when they get to 100Mb, you could use this:
119
+
120
+ config.logger = Lumberjack::Logger.new(log_file_path, :max_size => 100.megabytes)
121
+
122
+ To change the log message format, you could use this code:
123
+
124
+ config.logger = Lumberjack::Logger.new(log_file_path, :template => ":time - :message")
125
+
126
+ To change the log message format to output JSON, you could use this code (this example requires the multi-json gem):
127
+
128
+ config.logger = Lumberjack::Logger.new(log_file_path, :template => lambda{|e| MultiJson.dump(e)})
129
+
130
+ To send log messages to syslog instead of to a file, you could use this (require the lumberjack_syslog_device gem):
131
+
132
+ config.logger = Lumberjack::Logger.new(Lumberjack::SyslogDevice.new)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.0.5
@@ -2,12 +2,12 @@ module Lumberjack
2
2
  # This is an abstract class for logging devices. Subclasses must implement the +write+ method and
3
3
  # may implement the +close+ and +flush+ methods if applicable.
4
4
  class Device
5
- autoload :DateRollingLogFile, File.expand_path("../device/date_rolling_log_file.rb", __FILE__)
6
- autoload :LogFile, File.expand_path("../device/log_file.rb", __FILE__)
7
- autoload :Null, File.expand_path("../device/null.rb", __FILE__)
8
- autoload :RollingLogFile, File.expand_path("../device/rolling_log_file.rb", __FILE__)
9
- autoload :SizeRollingLogFile, File.expand_path("../device/size_rolling_log_file.rb", __FILE__)
10
- autoload :Writer, File.expand_path("../device/writer.rb", __FILE__)
5
+ load File.expand_path("../device/writer.rb", __FILE__)
6
+ load File.expand_path("../device/log_file.rb", __FILE__)
7
+ load File.expand_path("../device/rolling_log_file.rb", __FILE__)
8
+ load File.expand_path("../device/date_rolling_log_file.rb", __FILE__)
9
+ load File.expand_path("../device/size_rolling_log_file.rb", __FILE__)
10
+ load File.expand_path("../device/null.rb", __FILE__)
11
11
 
12
12
  # Subclasses must implement this method to write a LogEntry.
13
13
  def write(entry)
@@ -8,10 +8,10 @@ module Lumberjack
8
8
  # By default, all object will be converted to strings using their inspect method except for Strings
9
9
  # and Exceptions. Strings are not converted and Exceptions are converted using the ExceptionFormatter.
10
10
  class Formatter
11
- autoload :ExceptionFormatter, File.expand_path("../formatter/exception_formatter.rb", __FILE__)
12
- autoload :InspectFormatter, File.expand_path("../formatter/inspect_formatter.rb", __FILE__)
13
- autoload :PrettyPrintFormatter, File.expand_path("../formatter/pretty_print_formatter.rb", __FILE__)
14
- autoload :StringFormatter, File.expand_path("../formatter/string_formatter.rb", __FILE__)
11
+ load File.expand_path("../formatter/exception_formatter.rb", __FILE__)
12
+ load File.expand_path("../formatter/inspect_formatter.rb", __FILE__)
13
+ load File.expand_path("../formatter/pretty_print_formatter.rb", __FILE__)
14
+ load File.expand_path("../formatter/string_formatter.rb", __FILE__)
15
15
 
16
16
  def initialize
17
17
  @class_formatters = {}
@@ -24,22 +24,22 @@ module Lumberjack
24
24
  # using a Formatter associated with the logger.
25
25
  class Logger
26
26
  include Severity
27
-
27
+
28
28
  # The Formatter object used to convert messages into strings.
29
29
  attr_reader :formatter
30
-
30
+
31
31
  # The time that the device was last flushed.
32
32
  attr_reader :last_flushed_at
33
-
33
+
34
34
  # The name of the program associated with log messages.
35
35
  attr_writer :progname
36
-
36
+
37
37
  # The device being written to.
38
38
  attr_reader :device
39
-
39
+
40
40
  # Set +silencer+ to false to disable silencing the log.
41
41
  attr_accessor :silencer
42
-
42
+
43
43
  # Create a new logger to log to a Device.
44
44
  #
45
45
  # The +device+ argument can be in any one of several formats.
@@ -60,27 +60,27 @@ module Lumberjack
60
60
  # All other options are passed to the device constuctor.
61
61
  def initialize(device = STDOUT, options = {})
62
62
  @thread_settings = {}
63
-
63
+
64
64
  options = options.dup
65
65
  self.level = options.delete(:level) || INFO
66
66
  self.progname = options.delete(:progname)
67
67
  max_flush_seconds = options.delete(:flush_seconds).to_f
68
-
68
+
69
69
  @device = open_device(device, options)
70
70
  @formatter = Formatter.new
71
71
  @lock = Mutex.new
72
72
  @last_flushed_at = Time.now
73
73
  @silencer = true
74
-
74
+
75
75
  create_flusher_thread(max_flush_seconds) if max_flush_seconds > 0
76
76
  end
77
-
77
+
78
78
  # Get the level of severity of entries that are logged. Entries with a lower
79
79
  # severity level will be ignored.
80
80
  def level
81
81
  thread_local_value(:lumberjack_logger_level) || @level
82
82
  end
83
-
83
+
84
84
  # Add a message to the log with a given severity. The message can be either
85
85
  # passed in the +message+ argument or supplied with a block. This method
86
86
  # is not normally called. Instead call one of the helper functions
@@ -97,94 +97,105 @@ module Lumberjack
97
97
  # logger.add(Lumberjack::Severity::DEBUG){"Start processing with options #{options.inspect}"}
98
98
  def add(severity, message = nil, progname = nil)
99
99
  severity = Severity.label_to_level(severity) if severity.is_a?(String) || severity.is_a?(Symbol)
100
- if severity && severity >= level
101
- time = Time.now
102
- message = yield if message.nil? && block_given?
103
- message = @formatter.format(message)
104
- entry = LogEntry.new(time, severity, message, progname || self.progname, $$, Lumberjack.unit_of_work_id)
105
- begin
106
- device.write(entry)
107
- rescue => e
108
- $stderr.puts("#{e.class.name}: #{e.message}#{' at ' + e.backtrace.first if e.backtrace}")
109
- $stderr.puts(entry.to_s)
100
+
101
+ return unless severity && severity >= level
102
+
103
+ time = Time.now
104
+ if message.nil?
105
+ if block_given?
106
+ message = yield
107
+ else
108
+ message = progname
109
+ progname = nil
110
110
  end
111
111
  end
112
+
113
+ message = @formatter.format(message)
114
+ progname ||= self.progname
115
+ entry = LogEntry.new(time, severity, message, progname, $$, Lumberjack.unit_of_work_id)
116
+ begin
117
+ device.write(entry)
118
+ rescue => e
119
+ $stderr.puts("#{e.class.name}: #{e.message}#{' at ' + e.backtrace.first if e.backtrace}")
120
+ $stderr.puts(entry.to_s)
121
+ end
122
+
112
123
  nil
113
124
  end
114
-
125
+
115
126
  alias_method :log, :add
116
-
127
+
117
128
  # Flush the logging device. Messages are not guaranteed to be written until this method is called.
118
129
  def flush
119
130
  device.flush
120
131
  @last_flushed_at = Time.now
121
132
  nil
122
133
  end
123
-
134
+
124
135
  # Close the logging device.
125
136
  def close
126
137
  flush
127
138
  @device.close if @device.respond_to?(:close)
128
139
  end
129
-
140
+
130
141
  # Log a +FATAL+ message. The message can be passed in either the +message+ argument or in a block.
131
142
  def fatal(message = nil, progname = nil, &block)
132
143
  add(FATAL, message, progname, &block)
133
144
  end
134
-
145
+
135
146
  # Return +true+ if +FATAL+ messages are being logged.
136
147
  def fatal?
137
148
  level <= FATAL
138
149
  end
139
-
150
+
140
151
  # Log an +ERROR+ message. The message can be passed in either the +message+ argument or in a block.
141
152
  def error(message = nil, progname = nil, &block)
142
153
  add(ERROR, message, progname, &block)
143
154
  end
144
-
155
+
145
156
  # Return +true+ if +ERROR+ messages are being logged.
146
157
  def error?
147
158
  level <= ERROR
148
159
  end
149
-
160
+
150
161
  # Log a +WARN+ message. The message can be passed in either the +message+ argument or in a block.
151
162
  def warn(message = nil, progname = nil, &block)
152
163
  add(WARN, message, progname, &block)
153
164
  end
154
-
165
+
155
166
  # Return +true+ if +WARN+ messages are being logged.
156
167
  def warn?
157
168
  level <= WARN
158
169
  end
159
-
170
+
160
171
  # Log an +INFO+ message. The message can be passed in either the +message+ argument or in a block.
161
172
  def info(message = nil, progname = nil, &block)
162
173
  add(INFO, message, progname, &block)
163
174
  end
164
-
175
+
165
176
  # Return +true+ if +INFO+ messages are being logged.
166
177
  def info?
167
178
  level <= INFO
168
179
  end
169
-
180
+
170
181
  # Log a +DEBUG+ message. The message can be passed in either the +message+ argument or in a block.
171
182
  def debug(message = nil, progname = nil, &block)
172
183
  add(DEBUG, message, progname, &block)
173
184
  end
174
-
185
+
175
186
  # Return +true+ if +DEBUG+ messages are being logged.
176
187
  def debug?
177
188
  level <= DEBUG
178
189
  end
179
-
190
+
180
191
  # Log a message when the severity is not known. Unknown messages will always appear in the log.
181
192
  # The message can be passed in either the +message+ argument or in a block.
182
193
  def unknown(message = nil, progname = nil, &block)
183
194
  add(UNKNOWN, message, progname, &block)
184
195
  end
185
-
196
+
186
197
  alias_method :<<, :unknown
187
-
198
+
188
199
  # Set the minimum level of severity of messages to log.
189
200
  def level=(severity)
190
201
  if severity.is_a?(Fixnum)
@@ -193,7 +204,7 @@ module Lumberjack
193
204
  @level = Severity.label_to_level(severity)
194
205
  end
195
206
  end
196
-
207
+
197
208
  # Silence the logger by setting a new log level inside a block. By default, only +ERROR+ or +FATAL+
198
209
  # messages will be logged.
199
210
  #
@@ -210,7 +221,7 @@ module Lumberjack
210
221
  yield
211
222
  end
212
223
  end
213
-
224
+
214
225
  # Set the program name that is associated with log messages. If a block
215
226
  # is given, the program name will be valid only within the block.
216
227
  def set_progname(value, &block)
@@ -220,14 +231,14 @@ module Lumberjack
220
231
  self.progname = value
221
232
  end
222
233
  end
223
-
234
+
224
235
  # Get the program name associated with log messages.
225
236
  def progname
226
237
  thread_local_value(:lumberjack_logger_progname) || @progname
227
238
  end
228
-
239
+
229
240
  private
230
-
241
+
231
242
  # Set a local value for a thread tied to this object.
232
243
  def set_thread_local_value(name, value) #:nodoc:
233
244
  values = Thread.current[name]
@@ -242,13 +253,13 @@ module Lumberjack
242
253
  values[self] = value
243
254
  end
244
255
  end
245
-
256
+
246
257
  # Get a local value for a thread tied to this object.
247
258
  def thread_local_value(name) #:nodoc:
248
259
  values = Thread.current[name]
249
260
  values[self] if values
250
261
  end
251
-
262
+
252
263
  # Set a local value for a thread tied to this object within a block.
253
264
  def push_thread_local_value(name, value) #:nodoc:
254
265
  save_val = thread_local_value(name)
@@ -259,12 +270,12 @@ module Lumberjack
259
270
  set_thread_local_value(name, save_val)
260
271
  end
261
272
  end
262
-
273
+
263
274
  # Open a logging device.
264
275
  def open_device(device, options) #:nodoc:
265
276
  if device.is_a?(Device)
266
277
  device
267
- elsif device.respond_to?(:write)
278
+ elsif device.respond_to?(:write) && device.respond_to?(:flush)
268
279
  Device::Writer.new(device, options)
269
280
  elsif device == :null
270
281
  Device::Null.new
@@ -279,7 +290,7 @@ module Lumberjack
279
290
  end
280
291
  end
281
292
  end
282
-
293
+
283
294
  # Create a thread that will periodically call flush.
284
295
  def create_flusher_thread(flush_seconds) #:nodoc:
285
296
  if flush_seconds > 0
data/lib/lumberjack.rb CHANGED
@@ -3,16 +3,17 @@ require 'time'
3
3
  require 'thread'
4
4
 
5
5
  module Lumberjack
6
- autoload :Device, File.expand_path("../lumberjack/device.rb", __FILE__)
7
- autoload :Formatter, File.expand_path("../lumberjack/formatter.rb", __FILE__)
8
- autoload :LogEntry, File.expand_path("../lumberjack/log_entry.rb", __FILE__)
9
- autoload :Logger, File.expand_path("../lumberjack/logger.rb", __FILE__)
10
- autoload :Rack, File.expand_path("../lumberjack/rack.rb", __FILE__)
11
- autoload :Severity, File.expand_path("../lumberjack/severity.rb", __FILE__)
12
- autoload :Template, File.expand_path("../lumberjack/template.rb", __FILE__)
13
-
14
6
  LINE_SEPARATOR = (RbConfig::CONFIG['host_os'].match(/mswin/i) ? "\r\n" : "\n")
15
7
 
8
+ load File.expand_path("../lumberjack/severity.rb", __FILE__)
9
+ load File.expand_path("../lumberjack/log_entry.rb", __FILE__)
10
+ load File.expand_path("../lumberjack/formatter.rb", __FILE__)
11
+ load File.expand_path("../lumberjack/device.rb", __FILE__)
12
+ load File.expand_path("../lumberjack/logger.rb", __FILE__)
13
+ load File.expand_path("../lumberjack/template.rb", __FILE__)
14
+
15
+ autoload :Rack, File.expand_path("../lumberjack/rack.rb", __FILE__)
16
+
16
17
  class << self
17
18
  # Define a unit of work within a block. Within the block supplied to this
18
19
  # method, calling +unit_of_work_id+ will return the same value that can
@@ -20,8 +20,8 @@ describe Lumberjack::Device::DateRollingLogFile do
20
20
  logger = Lumberjack::Logger.new(device, :buffer_size => 2)
21
21
  logger.error("test day one")
22
22
  logger.flush
23
- Time.stub!(:now).and_return(now + one_day)
24
- Date.stub!(:today).and_return(today + 1)
23
+ Time.stub(:now => now + one_day)
24
+ Date.stub(:today => today + 1)
25
25
  logger.error("test day two")
26
26
  logger.close
27
27
 
@@ -37,8 +37,8 @@ describe Lumberjack::Device::DateRollingLogFile do
37
37
  logger = Lumberjack::Logger.new(device, :buffer_size => 2)
38
38
  logger.error("test week one")
39
39
  logger.flush
40
- Time.stub!(:now).and_return(now + (7 * one_day))
41
- Date.stub!(:today).and_return(today + 7)
40
+ Time.stub(:now => now + (7 * one_day))
41
+ Date.stub(:today => today + 7)
42
42
  logger.error("test week two")
43
43
  logger.close
44
44
 
@@ -54,8 +54,8 @@ describe Lumberjack::Device::DateRollingLogFile do
54
54
  logger = Lumberjack::Logger.new(device, :buffer_size => 2)
55
55
  logger.error("test month one")
56
56
  logger.flush
57
- Time.stub!(:now).and_return(now + (31 * one_day))
58
- Date.stub!(:today).and_return(today + 31)
57
+ Time.stub(:now => now + (31 * one_day))
58
+ Date.stub(:today => today + 31)
59
59
  logger.error("test month two")
60
60
  logger.close
61
61
 
@@ -26,7 +26,7 @@ describe Lumberjack::Device::RollingLogFile do
26
26
  device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :buffer_size => 32767)
27
27
  device.should_receive(:roll_file?).and_return(false, true)
28
28
  device.should_receive(:after_roll)
29
- device.stub!(:archive_file_suffix).and_return("rolled")
29
+ device.stub(:archive_file_suffix => "rolled")
30
30
  device.write(entry)
31
31
  device.flush
32
32
  device.write(Lumberjack::LogEntry.new(Time.now, 1, "Another log entry", nil, $$, nil))
@@ -38,7 +38,7 @@ describe Lumberjack::Device::RollingLogFile do
38
38
  it "should reopen the file if the stream inode doesn't match the file path inode" do
39
39
  log_file = File.join(tmp_dir, "test_3.log")
40
40
  device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message")
41
- device.stub!(:roll_file?).and_return(false)
41
+ device.stub(:roll_file? => false)
42
42
  device.write(entry)
43
43
  device.flush
44
44
  File.rename(log_file, "#{log_file}.rolled")
@@ -109,12 +109,11 @@ describe Lumberjack::Device::RollingLogFile do
109
109
  log_file = File.join(tmp_dir, "test_5.log")
110
110
  device = Lumberjack::Device::RollingLogFile.new(log_file, :template => ":message", :keep => 2, :buffer_size => 32767)
111
111
  device.should_receive(:roll_file?).and_return(false, true, true, true)
112
- device.stub!(:archive_file_suffix).and_return("delete", "another", "keep")
112
+ device.should_receive(:archive_file_suffix).and_return("delete", "another", "keep")
113
113
  t = Time.now
114
- File.should_receive(:ctime).with(log_file).any_number_of_times.and_return(t)
115
- File.should_receive(:ctime).with("#{log_file}.delete").any_number_of_times.and_return(t + 1)
116
- File.should_receive(:ctime).with("#{log_file}.another").any_number_of_times.and_return(t + 2)
117
- File.should_receive(:ctime).with("#{log_file}.keep").any_number_of_times.and_return(t + 3)
114
+ File.should_receive(:ctime).with("#{log_file}.delete").at_least(1).times.and_return(t + 1)
115
+ File.should_receive(:ctime).with("#{log_file}.another").at_least(1).times.and_return(t + 2)
116
+ File.should_receive(:ctime).with("#{log_file}.keep").at_least(1).times.and_return(t + 3)
118
117
  device.write(entry)
119
118
  device.flush
120
119
  device.write(entry)
data/spec/logger_spec.rb CHANGED
@@ -17,56 +17,56 @@ describe Lumberjack::Logger do
17
17
  logger = Lumberjack::Logger.new(output)
18
18
  logger.device.class.should == Lumberjack::Device::Writer
19
19
  end
20
-
20
+
21
21
  it "should open a file path in a device" do
22
22
  logger = Lumberjack::Logger.new(File.join(tmp_dir, "log_file_1.log"))
23
23
  logger.device.class.should == Lumberjack::Device::LogFile
24
24
  end
25
-
25
+
26
26
  it "should use the null device if the stream is :null" do
27
27
  logger = Lumberjack::Logger.new(:null)
28
28
  logger.device.class.should == Lumberjack::Device::Null
29
29
  end
30
-
30
+
31
31
  it "should set the level with a numeric" do
32
32
  logger = Lumberjack::Logger.new(:null, :level => Lumberjack::Severity::WARN)
33
33
  logger.level.should == Lumberjack::Severity::WARN
34
34
  end
35
-
35
+
36
36
  it "should set the level with a level" do
37
37
  logger = Lumberjack::Logger.new(:null, :level => :warn)
38
38
  logger.level.should == Lumberjack::Severity::WARN
39
39
  end
40
-
40
+
41
41
  it "should default the level to INFO" do
42
42
  logger = Lumberjack::Logger.new(:null)
43
43
  logger.level.should == Lumberjack::Severity::INFO
44
44
  end
45
-
45
+
46
46
  it "should set the progname"do
47
47
  logger = Lumberjack::Logger.new(:null, :progname => "app")
48
48
  logger.progname.should == "app"
49
49
  end
50
-
50
+
51
51
  it "should create a thread to flush the device" do
52
52
  Thread.should_receive(:new)
53
53
  logger = Lumberjack::Logger.new(:null, :flush_seconds => 10)
54
54
  end
55
55
  end
56
-
56
+
57
57
  context "attributes" do
58
58
  it "should have a level" do
59
59
  logger = Lumberjack::Logger.new
60
60
  logger.level = Lumberjack::Severity::DEBUG
61
61
  logger.level.should == Lumberjack::Severity::DEBUG
62
62
  end
63
-
63
+
64
64
  it "should have a progname" do
65
65
  logger = Lumberjack::Logger.new
66
66
  logger.progname = "app"
67
67
  logger.progname.should == "app"
68
68
  end
69
-
69
+
70
70
  it "should be able to silence the log in a block" do
71
71
  output = StringIO.new
72
72
  logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
@@ -79,7 +79,7 @@ describe Lumberjack::Logger do
79
79
  logger.info("four")
80
80
  output.string.split.should == ["one", "three", "four"]
81
81
  end
82
-
82
+
83
83
  it "should be able to customize the level of silence in a block" do
84
84
  output = StringIO.new
85
85
  logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
@@ -93,7 +93,7 @@ describe Lumberjack::Logger do
93
93
  logger.info("four")
94
94
  output.string.split.should == ["one", "woof", "four"]
95
95
  end
96
-
96
+
97
97
  it "should not be able to silence the logger if silencing is disabled" do
98
98
  output = StringIO.new
99
99
  logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
@@ -107,7 +107,7 @@ describe Lumberjack::Logger do
107
107
  logger.info("four")
108
108
  output.string.split.should == ["one", "two", "three", "four"]
109
109
  end
110
-
110
+
111
111
  it "should be able to set the progname in a block" do
112
112
  logger = Lumberjack::Logger.new
113
113
  logger.set_progname("app")
@@ -120,7 +120,7 @@ describe Lumberjack::Logger do
120
120
  block_executed.should == true
121
121
  logger.progname.should == "app"
122
122
  end
123
-
123
+
124
124
  it "should only affect the current thread when silencing the logger" do
125
125
  output = StringIO.new
126
126
  logger = Lumberjack::Logger.new(output, :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":message")
@@ -144,7 +144,7 @@ describe Lumberjack::Logger do
144
144
  status = 2
145
145
  end
146
146
  end
147
-
147
+
148
148
  it "should only affect the current thread when changing the progname in a block" do
149
149
  output = StringIO.new
150
150
  logger = Lumberjack::Logger.new(output, :progname => "thread1", :buffer_size => 0, :level => Lumberjack::Severity::INFO, :template => ":progname :message")
@@ -169,7 +169,7 @@ describe Lumberjack::Logger do
169
169
  end
170
170
  end
171
171
  end
172
-
172
+
173
173
  context "flushing" do
174
174
  it "should autoflush the buffer if it hasn't been flushed in a specified number of seconds" do
175
175
  output = StringIO.new
@@ -184,7 +184,7 @@ describe Lumberjack::Logger do
184
184
  sleep(0.15)
185
185
  output.string.split(Lumberjack::LINE_SEPARATOR).should == ["message 1", "message 2", "message 3"]
186
186
  end
187
-
187
+
188
188
  it "should write the log entries to the device on flush and update the last flushed time" do
189
189
  output = StringIO.new
190
190
  logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
@@ -195,7 +195,7 @@ describe Lumberjack::Logger do
195
195
  output.string.split(Lumberjack::LINE_SEPARATOR).should == ["message 1"]
196
196
  logger.last_flushed_at.should >= last_flushed_at
197
197
  end
198
-
198
+
199
199
  it "should flush the buffer and close the devices" do
200
200
  output = StringIO.new
201
201
  logger = Lumberjack::Logger.new(output, :level => Lumberjack::Severity::INFO, :template => ":message", :buffer_size => 32767)
@@ -206,7 +206,7 @@ describe Lumberjack::Logger do
206
206
  output.should be_closed
207
207
  end
208
208
  end
209
-
209
+
210
210
  context "logging" do
211
211
  let(:output){ StringIO.new }
212
212
  let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0) }
@@ -215,61 +215,70 @@ describe Lumberjack::Logger do
215
215
 
216
216
  it "should add entries with a numeric severity and a message" do
217
217
  time = Time.parse("2011-01-30T12:31:56.123")
218
- Time.stub!(:now).and_return(time)
218
+ Time.stub(:now => time)
219
219
  logger.add(Lumberjack::Severity::INFO, "test")
220
220
  output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
221
221
  end
222
222
 
223
223
  it "should add entries with a severity label" do
224
224
  time = Time.parse("2011-01-30T12:31:56.123")
225
- Time.stub!(:now).and_return(time)
225
+ Time.stub(:now => time)
226
226
  logger.add(:info, "test")
227
227
  output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
228
228
  end
229
229
 
230
230
  it "should add entries with a custom progname and message" do
231
231
  time = Time.parse("2011-01-30T12:31:56.123")
232
- Time.stub!(:now).and_return(time)
232
+ Time.stub(:now => time)
233
233
  logger.add(Lumberjack::Severity::INFO, "test", "app")
234
234
  output.string.should == "[2011-01-30T12:31:56.123 INFO app(#{$$}) #] test#{n}"
235
235
  end
236
236
 
237
237
  it "should add entries with a local progname and message" do
238
238
  time = Time.parse("2011-01-30T12:31:56.123")
239
- Time.stub!(:now).and_return(time)
239
+ Time.stub(:now => time)
240
240
  logger.set_progname("block") do
241
241
  logger.add(Lumberjack::Severity::INFO, "test")
242
242
  end
243
243
  output.string.should == "[2011-01-30T12:31:56.123 INFO block(#{$$}) #] test#{n}"
244
244
  end
245
-
245
+
246
+ it "should add entries with a progname but no message or block" do
247
+ time = Time.parse("2011-01-30T12:31:56.123")
248
+ Time.stub(:now => time)
249
+ logger.set_progname("default") do
250
+ logger.add(Lumberjack::Severity::INFO, nil, "message")
251
+ end
252
+ output.string.should == "[2011-01-30T12:31:56.123 INFO default(#{$$}) #] message#{n}"
253
+ end
254
+
246
255
  it "should add entries with a block" do
247
256
  time = Time.parse("2011-01-30T12:31:56.123")
248
- Time.stub!(:now).and_return(time)
257
+ Time.stub(:now => time)
249
258
  logger.add(Lumberjack::Severity::INFO){"test"}
250
259
  output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
251
260
  end
252
-
261
+
253
262
  it "should log entries (::Logger compatibility)" do
254
263
  time = Time.parse("2011-01-30T12:31:56.123")
255
- Time.stub!(:now).and_return(time)
264
+ Time.stub(:now => time)
256
265
  logger.log(Lumberjack::Severity::INFO, "test")
257
266
  output.string.should == "[2011-01-30T12:31:56.123 INFO test(#{$$}) #] test#{n}"
258
267
  end
259
-
268
+
260
269
  it "should append messages with unknown severity to the log" do
261
270
  time = Time.parse("2011-01-30T12:31:56.123")
262
- Time.stub!(:now).and_return(time)
271
+ Time.stub(:now => time)
263
272
  logger << "test"
264
273
  output.string.should == "[2011-01-30T12:31:56.123 UNKNOWN test(#{$$}) #] test#{n}"
265
274
  end
266
-
275
+
267
276
  it "should ouput entries to STDERR if they can't be written the the device" do
268
277
  stderr = $stderr
269
278
  $stderr = StringIO.new
270
279
  begin
271
280
  time = Time.parse("2011-01-30T12:31:56.123")
272
- Time.stub!(:now).and_return(time)
281
+ Time.stub(:now => time)
273
282
  device.should_receive(:write).and_raise(StandardError.new("Cannot write to device"))
274
283
  logger.add(Lumberjack::Severity::INFO, "test")
275
284
  $stderr.string.should include("[2011-01-30T12:31:56.123 INFO test(#{$$})] test")
@@ -278,17 +287,17 @@ describe Lumberjack::Logger do
278
287
  $stderr = stderr
279
288
  end
280
289
  end
281
-
290
+
282
291
  context "log helper methods" do
283
292
  let(:device){ Lumberjack::Device::Writer.new(output, :buffer_size => 0, :template => ":message") }
284
-
293
+
285
294
  it "should only add messages whose severity is greater or equal to the logger level" do
286
295
  logger.add(Lumberjack::Severity::DEBUG, "debug")
287
296
  logger.add(Lumberjack::Severity::INFO, "info")
288
297
  logger.add(Lumberjack::Severity::ERROR, "error")
289
298
  output.string.should == "info#{n}error#{n}"
290
299
  end
291
-
300
+
292
301
  it "should only log fatal messages when the level is set to fatal" do
293
302
  logger.level = Lumberjack::Severity::FATAL
294
303
  logger.fatal("fatal")
@@ -304,7 +313,7 @@ describe Lumberjack::Logger do
304
313
  logger.unknown("unknown")
305
314
  output.string.should == "fatal#{n}unknown#{n}"
306
315
  end
307
-
316
+
308
317
  it "should only log error messages and higher when the level is set to error" do
309
318
  logger.level = Lumberjack::Severity::ERROR
310
319
  logger.fatal("fatal")
@@ -320,7 +329,7 @@ describe Lumberjack::Logger do
320
329
  logger.unknown("unknown")
321
330
  output.string.should == "fatal#{n}error#{n}unknown#{n}"
322
331
  end
323
-
332
+
324
333
  it "should only log warn messages and higher when the level is set to warn" do
325
334
  logger.level = Lumberjack::Severity::WARN
326
335
  logger.fatal("fatal")
@@ -336,7 +345,7 @@ describe Lumberjack::Logger do
336
345
  logger.unknown("unknown")
337
346
  output.string.should == "fatal#{n}error#{n}warn#{n}unknown#{n}"
338
347
  end
339
-
348
+
340
349
  it "should only log info messages and higher when the level is set to info" do
341
350
  logger.level = Lumberjack::Severity::INFO
342
351
  logger.fatal("fatal")
@@ -352,7 +361,7 @@ describe Lumberjack::Logger do
352
361
  logger.unknown("unknown")
353
362
  output.string.should == "fatal#{n}error#{n}warn#{n}info#{n}unknown#{n}"
354
363
  end
355
-
364
+
356
365
  it "should log all messages when the level is set to debug" do
357
366
  logger.level = Lumberjack::Severity::DEBUG
358
367
  logger.fatal("fatal")
@@ -368,7 +377,7 @@ describe Lumberjack::Logger do
368
377
  logger.unknown("unknown")
369
378
  output.string.should == "fatal#{n}error#{n}warn#{n}info#{n}debug#{n}unknown#{n}"
370
379
  end
371
-
380
+
372
381
  it "should only log unkown messages when the level is set above fatal" do
373
382
  logger.level = Lumberjack::Severity::FATAL + 1
374
383
  logger.fatal("fatal")
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lumberjack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
5
- prerelease:
4
+ version: 1.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Brian Durand
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-26 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: A simple, powerful, and very fast logging utility that can be a drop
15
14
  in replacement for Logger or ActiveSupport::BufferedLogger. Provides support for
@@ -22,29 +21,29 @@ extensions: []
22
21
  extra_rdoc_files:
23
22
  - README.rdoc
24
23
  files:
24
+ - MIT_LICENSE
25
25
  - README.rdoc
26
- - VERSION
27
26
  - Rakefile
28
- - MIT_LICENSE
27
+ - VERSION
28
+ - lib/lumberjack.rb
29
+ - lib/lumberjack/device.rb
29
30
  - lib/lumberjack/device/date_rolling_log_file.rb
30
31
  - lib/lumberjack/device/log_file.rb
31
32
  - lib/lumberjack/device/null.rb
32
33
  - lib/lumberjack/device/rolling_log_file.rb
33
34
  - lib/lumberjack/device/size_rolling_log_file.rb
34
35
  - lib/lumberjack/device/writer.rb
35
- - lib/lumberjack/device.rb
36
+ - lib/lumberjack/formatter.rb
36
37
  - lib/lumberjack/formatter/exception_formatter.rb
37
38
  - lib/lumberjack/formatter/inspect_formatter.rb
38
39
  - lib/lumberjack/formatter/pretty_print_formatter.rb
39
40
  - lib/lumberjack/formatter/string_formatter.rb
40
- - lib/lumberjack/formatter.rb
41
41
  - lib/lumberjack/log_entry.rb
42
42
  - lib/lumberjack/logger.rb
43
- - lib/lumberjack/rack/unit_of_work.rb
44
43
  - lib/lumberjack/rack.rb
44
+ - lib/lumberjack/rack/unit_of_work.rb
45
45
  - lib/lumberjack/severity.rb
46
46
  - lib/lumberjack/template.rb
47
- - lib/lumberjack.rb
48
47
  - spec/device/date_rolling_log_file_spec.rb
49
48
  - spec/device/log_file_spec.rb
50
49
  - spec/device/null_spec.rb
@@ -64,31 +63,31 @@ files:
64
63
  - spec/spec_helper.rb
65
64
  - spec/template_spec.rb
66
65
  homepage: http://github.com/bdurand/lumberjack
67
- licenses: []
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
68
69
  post_install_message:
69
70
  rdoc_options:
70
- - --charset=UTF-8
71
- - --main
71
+ - "--charset=UTF-8"
72
+ - "--main"
72
73
  - README.rdoc
73
74
  require_paths:
74
75
  - lib
75
76
  required_ruby_version: !ruby/object:Gem::Requirement
76
- none: false
77
77
  requirements:
78
- - - ! '>='
78
+ - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
82
  requirements:
84
- - - ! '>='
83
+ - - ">="
85
84
  - !ruby/object:Gem::Version
86
85
  version: '0'
87
86
  requirements: []
88
87
  rubyforge_project:
89
- rubygems_version: 1.8.25
88
+ rubygems_version: 2.2.2
90
89
  signing_key:
91
- specification_version: 3
90
+ specification_version: 4
92
91
  summary: A simple, powerful, and very fast logging utility that can be a drop in replacement
93
92
  for Logger or ActiveSupport::BufferedLogger.
94
93
  test_files: []