timestamped-logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,31 @@
1
+ TimestampedLogger gives you a simple way to create a well-formatted log, including timetsamps, and formatted logging of error messages.
2
+
3
+ # create the log
4
+ IMPORT_LOGGER = TimestampedLogger.new(“#{RAILS_ROOT}/log/imports.log”)
5
+
6
+ # log normal information
7
+ IMPORT_LOGGER.info("Started importing")
8
+ # produces: 2010-01-02 03:04:05 | Started importing
9
+
10
+ # log without timestamp
11
+ IMPORT_LOGGER.info("I don't care when this happened", false)
12
+ # produces: "I don't care when this happened"
13
+
14
+ # log some information, and then indent related info
15
+ IMPORT_LOGGER.info("started importing")
16
+ IMPORT_LOGGER.sub_info("started this sub-step")
17
+ IMPORT_LOGGER.sub_info("started this other sub-step")
18
+ IMPORT_LOGGER.info("ended importing")
19
+ # produces:
20
+ # 2010-01-02 03:04:05 | started importing
21
+ # 2010-01-02 03:04:06 | started this sub-step
22
+ # 2010-01-02 03:07:00 | started this other sub-step
23
+ # 2010-01-02 03:09:36 | ended importing
24
+
25
+ # log an error and it's backtrace
26
+ IMPORT_LOGGER.backtrace(exception, "Oh no, error during import")
27
+ # produces:
28
+ # 2010-01-02 03:04:05 | Oh no, error during import
29
+ # 2010-01-02 03:04:05 | RuntimeException Foo
30
+ # 2010-01-02 03:04:06 | ---> first line of the backtrace
31
+ # 2010-01-02 03:04:07 | ---> second line of the backtrace
@@ -0,0 +1,27 @@
1
+ class TimestampedLogger < Logger
2
+
3
+ def info(message, include_timestamp = true)
4
+ message = "#{Time.now.to_s(:db)} | #{message}" if include_timestamp
5
+ super(message)
6
+ end
7
+
8
+ def error(message)
9
+ super("#{Time.now.to_s(:db)} | #{message}")
10
+ end
11
+
12
+ def sub_info(message, include_timestamp = true)
13
+ info(" #{message}", include_timestamp)
14
+ end
15
+
16
+ def backtrace(exception, message = "")
17
+ if exception.respond_to?(:backtrace)
18
+ info("================================================", false)
19
+ error(message) unless message.empty?
20
+ error(exception.message)
21
+ exception.backtrace.to_a.each do |bt|
22
+ info(" ---> #{bt}", false)
23
+ end
24
+ info("================================================", false)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,75 @@
1
+ require File.join(File.dirname(__FILE__), "/spec_helper")
2
+
3
+ describe TimestampedLogger, "Adds an info message to the log" do
4
+ it "should call the logger info method with the timestamp on the front" do
5
+ File.delete(File.join(File.dirname(__FILE__), "/spec.log"))
6
+ log = TimestampedLogger.new(File.join(File.dirname(__FILE__), "/spec.log"))
7
+ t = Time.new
8
+ t.stubs(:to_s => "date")
9
+ Time.stubs(:now).returns(t)
10
+ log.info("message")
11
+
12
+ File.open(File.join(File.dirname(__FILE__), "/spec.log"), 'r') { |f|
13
+ f.read.should include('date | message')
14
+ }
15
+ end
16
+ end
17
+
18
+ describe TimestampedLogger, "Adds an error message to the log" do
19
+ it "should call the logger error method with the timestamp on the front" do
20
+ File.delete(File.join(File.dirname(__FILE__), "/spec.log"))
21
+ log = TimestampedLogger.new(File.join(File.dirname(__FILE__), "/spec.log"))
22
+ t = Time.new
23
+ t.stubs(:to_s => "date")
24
+ Time.stubs(:now).returns(t)
25
+ log.error("message")
26
+
27
+ File.open(File.join(File.dirname(__FILE__), "/spec.log"), 'r') { |f|
28
+ f.read.should include('date | message')
29
+ }
30
+ end
31
+ end
32
+
33
+
34
+ describe TimestampedLogger, "Adds a sub message to the log" do
35
+ it "should call the logger info method with the timestamp and spaces on the front and the default timestamp value" do
36
+ log = TimestampedLogger.new(File.join(File.dirname(__FILE__), "/spec.log"))
37
+ log.expects(:info).with(" message", true)
38
+ log.sub_info("message")
39
+ end
40
+
41
+ it "should call the logger info method with the value of the timestamp parameter passed" do
42
+ log = TimestampedLogger.new(File.join(File.dirname(__FILE__), "/spec.log"))
43
+ log.expects(:info).with(" message", false)
44
+ log.sub_info("message", false)
45
+ end
46
+ end
47
+
48
+ describe TimestampedLogger, "Add a back trace error" do
49
+ it "should log nothing if what is passed doesn't responsd to the backtrace method" do
50
+ log = TimestampedLogger.new(File.join(File.dirname(__FILE__), "/spec.log"))
51
+ log.expects(:info).never
52
+ log.backtrace("string")
53
+ end
54
+
55
+ it "should log the backtrace of the error passed" do
56
+ log = TimestampedLogger.new(File.join(File.dirname(__FILE__), "/spec.log"))
57
+ excep = mock('Exception', :backtrace => ['line 1', 'line 2'], :message => 'message')
58
+ log.expects(:info).with("================================================", false).twice
59
+ log.expects(:error).with('message')
60
+ log.expects(:info).with(" ---> line 1", false)
61
+ log.expects(:info).with(" ---> line 2", false)
62
+ log.backtrace(excep)
63
+ end
64
+
65
+ it "should log the backtrace of the error passed and the message passed" do
66
+ log = TimestampedLogger.new(File.join(File.dirname(__FILE__), "/spec.log"))
67
+ excep = mock('Exception', :backtrace => ['line 1', 'line 2'], :message => 'message')
68
+ log.expects(:info).with("================================================", false).twice
69
+ log.expects(:error).with('message')
70
+ log.expects(:error).with('passing a message')
71
+ log.expects(:info).with(" ---> line 1", false)
72
+ log.expects(:info).with(" ---> line 2", false)
73
+ log.backtrace(excep, 'passing a message')
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timestamped-logger
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - William Spencer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description:
33
+ email:
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - README
40
+ files:
41
+ - lib/timestamped_logger.rb
42
+ - README
43
+ has_rdoc: true
44
+ homepage:
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.6
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Adds timestamps to logs. Adds formating for adding backtraces to logs. Allows sub messages related to heading messages.
73
+ test_files:
74
+ - spec/timestamped_logger_spec.rb