catcher 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Kacper Cieśla
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ Catcher
2
+ ====
3
+
4
+ Everytime you create a thread in ruby, you need to add some exception handling to know if something goes wrong (and what exactly went wrong). Similary when you do background processing unless you already use some bigger framework for this. And when you have multiple threads, each one doing something that may or may not succeed, that's where you really appreciate logging done well.
5
+
6
+ This gem encapsulates pattern I use. First, somewhere in initialization you decide where to log:
7
+
8
+ Catcher.setup_logger "shit_happens.log"
9
+
10
+ Arguments are the same as for Logger.new. Then in your classes:
11
+
12
+ class Foo
13
+ include Catcher::Logger
14
+
15
+ def self.bar
16
+ log.info "gangnam style"
17
+ end
18
+ end
19
+
20
+ It could be also instance method. It will produce something like this:
21
+
22
+ I, [2012-10-30 10:56:45 #20295] INFO -- : Foo : gangnam style
23
+
24
+ So by default you have timestamps (how could you live without them?!), and class name because you don't like to repeat yourself. Now the catching stuff:
25
+
26
+ Catcher.block "I'm gonnna do science here" do
27
+ raise "oops"
28
+ end
29
+
30
+ Exception gets catched and logged like this:
31
+
32
+ E, [2012-10-30 11:00:32 #23305] ERROR -- : Exception raised by 'I'm gonnna do science here'
33
+ [RuntimeError] oops
34
+ (irb):3:in `block in irb_binding'
35
+ /comboy/projects/os/catcher/lib/catcher.rb:9:in `block'
36
+ (irb):2:in `irb_binding'
37
+ (.. full backtrace here ..)
38
+
39
+ And finally forget about Thread.new, just use:
40
+
41
+ Catcher.thread "doing science" do
42
+ # your stuff
43
+ end
44
+
45
+ Which is equal to Thread.new with Catcher.block inside.
46
+
47
+ Description strings in Catcher.block and Catcher.thread are optional, but very much recommended. Comments and suggestions are very much welome.
48
+
49
+ Happy threading.
50
+
51
+ Usage
52
+ ------
53
+
54
+ In your Gemfile:
55
+
56
+ gem 'catcher'
57
+
58
+ Author
59
+ -------
60
+
61
+ Kacper Cieśla
62
+
63
+ License
64
+ -------
65
+
66
+ Catcher is released under the MIT license.
67
+
68
+
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Catcher'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,49 @@
1
+ require 'logger'
2
+ require 'catcher/prefixed_logger'
3
+ require 'catcher/logger'
4
+
5
+ module Catcher
6
+
7
+ def self.block(progname = nil)
8
+ begin
9
+ yield
10
+ rescue Exception => e
11
+ log_exception e, progname
12
+ end
13
+ end
14
+
15
+ def self.thread(progname = nil)
16
+ Thread.new do
17
+ block progname do
18
+ yield
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.log_exception(e, progname)
24
+ text = progname ? "Exception raised by '#{progname}'\n\t" : ""
25
+ if e.kind_of? Exception
26
+ text << "[#{e.class}] #{e.message}\n\t#{e.backtrace.join("\n\t")}"
27
+ else
28
+ # This software is UFO ready
29
+ text << "Some not very exceptional object raised o_O #{e.inspect} [#{e.class}]"
30
+ end
31
+ Catcher.logger.error text
32
+ end
33
+
34
+ def self.logger
35
+ @logger || setup_logger(STDOUT)
36
+ end
37
+
38
+ def self.logger=(logger)
39
+ @logger = logger
40
+ end
41
+
42
+ def self.setup_logger(*args)
43
+ @logger = ::Logger.new(*args)
44
+ @logger.formatter = ::Logger::Formatter.new
45
+ @logger.datetime_format = "%Y-%m-%d %H:%M:%S "
46
+ @logger
47
+ end
48
+ end
49
+
@@ -0,0 +1,18 @@
1
+ module Catcher
2
+ module Logger
3
+ module LogMethods
4
+ def log
5
+ return @logger if @logger
6
+ class_prefix = self.to_s
7
+ # Q: Hey dude, why don't you just use Catcher.logger.progname?
8
+ # A: Because thread safety
9
+ @logger = PrefixedLogger.new Catcher.logger, class_prefix
10
+ end
11
+ end
12
+
13
+ def self.included(klass)
14
+ klass.send :include, LogMethods
15
+ klass.send :extend, LogMethods
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ module Catcher
2
+
3
+ class PrefixedLogger
4
+ def initialize(logger, prefix)
5
+ @logger = logger
6
+ @prefix = prefix + ' : '
7
+ end
8
+
9
+ # OK, I feel bad about this repetition below.
10
+ # But define_method won't define method with block argument,
11
+ # and I don't feel like using eval. Feel free to improve
12
+
13
+ def debug(progname = nil, &block)
14
+ @logger.add(::Logger::DEBUG, nil, @prefix + progname.to_s, &block)
15
+ end
16
+
17
+ def info(progname = nil, &block)
18
+ @logger.add(::Logger::INFO, nil, @prefix + progname.to_s, &block)
19
+ end
20
+
21
+ def warn(progname = nil, &block)
22
+ @logger.add(::Logger::WARN, nil, @prefix + progname.to_s, &block)
23
+ end
24
+
25
+ def error(progname = nil, &block)
26
+ @logger.add(::Logger::ERROR, nil, @prefix + progname.to_s, &block)
27
+ end
28
+
29
+ def fatal(progname = nil, &block)
30
+ @logger.add(::Logger::FATAL, nil, @prefix + progname.to_s, &block)
31
+ end
32
+
33
+ def unknown(progname = nil, &block)
34
+ @logger.add(::Logger::UNKNOWN, nil, @prefix + progname.to_s, &block)
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Catcher
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+ require 'catcher'
3
+
4
+ class TestClass
5
+ include Catcher::Logger
6
+
7
+ def foo
8
+ log.info "instance method"
9
+ end
10
+
11
+ def self.foo
12
+ log.info "class method"
13
+ end
14
+ end
15
+
16
+ describe Catcher::Logger do
17
+ before do
18
+ @output = ""
19
+ io = StringIO.new @output
20
+ Catcher.setup_logger io
21
+ end
22
+
23
+ it "works with instance methods" do
24
+ TestClass.new.foo
25
+ @output.strip.must_match /I, \[(.*)\] *INFO -- : #<TestClass:0x[a-z0-9]+> : instance method$/
26
+ end
27
+
28
+ it "works with class methods" do
29
+ TestClass.foo
30
+ @output.strip.must_match /I, \[(.*)\] *INFO -- : TestClass : class method$/
31
+ end
32
+
33
+ # TODO test coverage other log methods (debug, fatel etc.)
34
+ end
@@ -0,0 +1,72 @@
1
+ require 'test_helper'
2
+
3
+ describe Catcher do
4
+
5
+ it { Catcher.logger.wont_be_nil }
6
+ it { Catcher.logger.must_be_kind_of Logger }
7
+ let(:output) { "" }
8
+ let(:io) { StringIO.new output }
9
+
10
+ describe "logger=" do
11
+ before do
12
+ @logger = Logger.new(nil)
13
+ Catcher.logger = @logger
14
+ end
15
+
16
+ it { assert_equal Catcher.logger, @logger }
17
+ end
18
+
19
+ it "formats logs properly" do
20
+ Catcher.setup_logger io
21
+ Catcher.logger.info "foo"
22
+ output.strip.must_match /I, \[(.*)\] *INFO -- : foo$/
23
+ end
24
+
25
+ describe ".thread" do
26
+ it "returns thread" do
27
+ t = Catcher.thread { sleep 0.1 }
28
+ t.must_be_kind_of Thread
29
+ end
30
+
31
+ it "executes" do
32
+ a = 1
33
+ Catcher.thread("my thread") { a = 2 }.join
34
+ a.must_equal 2
35
+ end
36
+
37
+ it "executes without description" do
38
+ a = 1
39
+ Catcher.thread { a = 2 }.join
40
+ a.must_equal 2
41
+ end
42
+ end
43
+
44
+ describe ".block" do
45
+ before { Catcher.setup_logger io }
46
+
47
+ it "does not raise and logs exception" do
48
+ Catcher.block("something") { raise "boom" }
49
+ output.must_match /E, \[(.*)\] *ERROR -- : Exception raised by 'something'/m
50
+ end
51
+
52
+ it "workns without description" do
53
+ a = 1
54
+ Catcher.block { a = 2 }
55
+ assert_equal a, 2
56
+ end
57
+
58
+ end
59
+
60
+ it "logs exception" do
61
+ ex = nil
62
+ begin; raise "the hell"; rescue => e; ex = e; end
63
+ ex.must_be_kind_of RuntimeError
64
+
65
+ Catcher.setup_logger io
66
+ Catcher.log_exception(ex, "from here")
67
+ output.must_match /E, \[(.*)\] *ERROR -- : Exception raised by 'from here'/m
68
+ output.must_match /\[RuntimeError\] the hell/m
69
+ output.must_match /test\/catcher_test\.rb:\d+/m
70
+ end
71
+
72
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: catcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kacper Cieśla
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Keep readable logs for your multithreaded ruby scripts
15
+ email:
16
+ - kacper.ciesla@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/catcher.rb
22
+ - lib/catcher/prefixed_logger.rb
23
+ - lib/catcher/logger.rb
24
+ - lib/catcher/version.rb
25
+ - MIT-LICENSE
26
+ - Rakefile
27
+ - README.md
28
+ - test/test_helper.rb
29
+ - test/catcher_test.rb
30
+ - test/catcher/logger_test.rb
31
+ homepage: https://github.com/comboy/catcher
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.23
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Keep readable logs for your multithreaded ruby scripts
55
+ test_files:
56
+ - test/test_helper.rb
57
+ - test/catcher_test.rb
58
+ - test/catcher/logger_test.rb