resque-logger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/README.md +64 -0
- data/Rakefile +1 -0
- data/UNLICENSE +25 -0
- data/lib/resque-logger.rb +4 -0
- data/lib/resque/plugins/logger.rb +15 -0
- data/lib/resque/plugins/logger/factory.rb +49 -0
- data/lib/resque/setup.rb +12 -0
- data/lib/resque_logger.rb +45 -0
- data/lib/resque_logger/version.rb +3 -0
- data/resque-logger.gemspec +24 -0
- data/spec/lib/resque/plugins/logger/factory_spec.rb +69 -0
- data/spec/lib/resque/plugins/logger_spec.rb +17 -0
- data/spec/lib/resque_logger_spec.rb +30 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/support/rspec_macros.rb +16 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
Resque Per-worker Logger Plugin
|
2
|
+
===============================
|
3
|
+
|
4
|
+
Tired of create mechanisms to isolate log files to each [Resque][] worker in your application?
|
5
|
+
The gem resque-logger gives you a simple plugin to create a log file based on queue name.
|
6
|
+
|
7
|
+
Configuration
|
8
|
+
=============
|
9
|
+
|
10
|
+
### Using a initializer
|
11
|
+
# config/initializers/resque.rb
|
12
|
+
|
13
|
+
log_path = File.join Rails.root, 'log'
|
14
|
+
|
15
|
+
config = {
|
16
|
+
folder: log_path, # destination folder
|
17
|
+
class_name: Logger, # logger class name
|
18
|
+
class_args: [ 'daily', 1.kilobyte ], # logger additional parameters
|
19
|
+
level: Logger::INFO, # optional
|
20
|
+
formatter: Logger::Formatter.new, # optional
|
21
|
+
}
|
22
|
+
|
23
|
+
Resque.logger = config
|
24
|
+
|
25
|
+
Usage
|
26
|
+
=====
|
27
|
+
|
28
|
+
### Adding to a resque worker
|
29
|
+
# app/workers/my_killer_worker.rb
|
30
|
+
|
31
|
+
class MyKillerWorker
|
32
|
+
extend Resque::Plugins::Logger
|
33
|
+
|
34
|
+
@queue = :my_killer_worker_job
|
35
|
+
|
36
|
+
def self.perform(args = {})
|
37
|
+
(...)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Dependencies
|
42
|
+
============
|
43
|
+
|
44
|
+
* resque
|
45
|
+
|
46
|
+
Installation
|
47
|
+
============
|
48
|
+
|
49
|
+
### With rubygems:
|
50
|
+
$ [sudo] gem install resque-logger
|
51
|
+
|
52
|
+
Authors
|
53
|
+
=======
|
54
|
+
|
55
|
+
* Marcelo Correia Pinheiro - <http://salizzar.net/>
|
56
|
+
|
57
|
+
License
|
58
|
+
=======
|
59
|
+
|
60
|
+
ResqueLogger is free and unencumbered public domain software. For more
|
61
|
+
information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
|
62
|
+
|
63
|
+
[Resque]: https://github.com/defunkt/resque
|
64
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/UNLICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
25
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Resque
|
4
|
+
module Plugins
|
5
|
+
module Logger
|
6
|
+
class Factory
|
7
|
+
@queue_map = {}
|
8
|
+
|
9
|
+
#
|
10
|
+
# Returns a new instance of configured logger given a queue name.
|
11
|
+
# If queue logger is already created, returns previous instance.
|
12
|
+
#
|
13
|
+
def self.get(queue)
|
14
|
+
queue_name = queue.to_sym
|
15
|
+
logger = @queue_map[queue_name]
|
16
|
+
|
17
|
+
if logger.nil?
|
18
|
+
logger = create_logger queue_name
|
19
|
+
@queue_map[queue_name] = logger
|
20
|
+
end
|
21
|
+
|
22
|
+
logger
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
#
|
28
|
+
# Creates a new instance of logger given a queue name, retrieving
|
29
|
+
# configuration from Resque.logger.
|
30
|
+
#
|
31
|
+
def self.create_logger(queue)
|
32
|
+
config = Resque.logger
|
33
|
+
log_path = config[:folder]
|
34
|
+
class_name = config[:class_name]
|
35
|
+
class_args = config[:class_args]
|
36
|
+
|
37
|
+
file_path = File.join log_path, "#{queue}.log"
|
38
|
+
|
39
|
+
logger = class_name.new file_path, *class_args
|
40
|
+
logger.level = config[:level] if config[:level]
|
41
|
+
logger.formatter = config[:formatter] if config[:formatter]
|
42
|
+
|
43
|
+
logger
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
data/lib/resque/setup.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require "resque_logger/version"
|
4
|
+
require "resque/setup"
|
5
|
+
|
6
|
+
module ResqueLogger
|
7
|
+
module ClassMethods
|
8
|
+
def logger
|
9
|
+
@logger
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# Configures ResqueLogger with the following hash:
|
14
|
+
# {
|
15
|
+
# folder: <folder location for logs>,
|
16
|
+
# class_name: <class name to be created>,
|
17
|
+
# class_args: <an array of class arguments to be passed in constructor>,
|
18
|
+
# level: <logger level>,
|
19
|
+
# formatter: <a new instance of formatter class to add in logger>
|
20
|
+
# }
|
21
|
+
#
|
22
|
+
# Hash keys will be symbols to work.
|
23
|
+
#
|
24
|
+
def logger=(options)
|
25
|
+
check_logger_args! options
|
26
|
+
|
27
|
+
@logger = options
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
#
|
33
|
+
# Check for necessary keys and raises if not found.
|
34
|
+
#
|
35
|
+
def check_logger_args!(options)
|
36
|
+
keys = options.keys
|
37
|
+
|
38
|
+
raise ArgumentError.new 'Folder must be supplied' unless keys.include?(:folder)
|
39
|
+
raise ArgumentError.new 'Logger must be supplied' unless keys.include?(:class_name)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
Resque.extend ResqueLogger::ClassMethods
|
45
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "resque_logger/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "resque-logger"
|
7
|
+
s.version = ResqueLogger::VERSION
|
8
|
+
s.authors = ["Marcelo Correia Pinheiro"]
|
9
|
+
s.email = ["salizzar@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A Resque plugin to provides logger for each worker}
|
12
|
+
s.description = %q{A simple mechanism to create custom log files based on queue names of Resque workers.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "resque-logger"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency 'rake'
|
22
|
+
s.add_development_dependency 'rspec'
|
23
|
+
s.add_runtime_dependency 'resque'
|
24
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Resque::Plugins::Logger::Factory do
|
6
|
+
include RSpecMacros
|
7
|
+
|
8
|
+
let(:subject) { Resque::Plugins::Logger::Factory }
|
9
|
+
|
10
|
+
let(:logger) { double Logger }
|
11
|
+
let(:queue) { :an_queue }
|
12
|
+
let(:file_path) { File.join config[:folder], "#{queue}.log" }
|
13
|
+
|
14
|
+
before :each do
|
15
|
+
Resque.stub(:logger).and_return(config)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'creating a logger given a queue' do
|
19
|
+
before :each do
|
20
|
+
config.delete :level
|
21
|
+
config.delete :formatter
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns a new instance of configured logger if not exists' do
|
25
|
+
Logger.should_receive(:new).with(file_path, config[:class_args].first, config[:class_args].last).and_return(logger)
|
26
|
+
|
27
|
+
subject.get(queue).should == logger
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns previous created logger if exists' do
|
31
|
+
Logger.should_not_receive(:new)
|
32
|
+
|
33
|
+
subject.get queue
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'setting configurations' do
|
38
|
+
before :each do
|
39
|
+
Logger.should_receive(:new).and_return(logger)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'configures level if exists' do
|
43
|
+
config.delete :formatter
|
44
|
+
|
45
|
+
logger.should_receive(:level=).with(config[:level])
|
46
|
+
|
47
|
+
subject.get :a_queue_with_level
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'configures formatter if exists' do
|
51
|
+
config.delete :level
|
52
|
+
|
53
|
+
logger.should_receive(:formatter=).with(config[:formatter])
|
54
|
+
|
55
|
+
subject.get :a_queue_with_formatter
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'not assigns level and formatter if not exists' do
|
59
|
+
config.delete :level
|
60
|
+
config.delete :formatter
|
61
|
+
|
62
|
+
logger.should_not_receive(:level=)
|
63
|
+
logger.should_not_receive(:formatter=)
|
64
|
+
|
65
|
+
subject.get :a_basic_queue_config
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Resque::Plugins::Logger do
|
6
|
+
include Resque::Plugins::Logger
|
7
|
+
|
8
|
+
@queue = :an_queue
|
9
|
+
|
10
|
+
let(:a_logger) { double 'an mock' }
|
11
|
+
|
12
|
+
it 'returns a instance created by a logger factory' do
|
13
|
+
Resque::Plugins::Logger::Factory.should_receive(:get).with(@queue).and_return(a_logger)
|
14
|
+
logger.should == a_logger
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe ResqueLogger do
|
6
|
+
include RSpecMacros
|
7
|
+
|
8
|
+
describe 'checking configured values' do
|
9
|
+
it 'raises an error if folder is not found' do
|
10
|
+
config.delete :folder
|
11
|
+
|
12
|
+
error = ArgumentError.new 'Folder must be supplied'
|
13
|
+
|
14
|
+
expect { Resque.logger = config }.to raise_error(error.class, error.message)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'raises an error if class name is not found' do
|
18
|
+
config.delete :class_name
|
19
|
+
|
20
|
+
error = ArgumentError.new 'Logger must be supplied'
|
21
|
+
|
22
|
+
expect { Resque.logger = config }.to raise_error(error.class, error.message)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'not raises otherwise' do
|
26
|
+
expect { Resque.logger = config }.to_not raise_error
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
require 'rspec'
|
5
|
+
require 'resque'
|
6
|
+
require 'resque-logger'
|
7
|
+
require 'logger'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
# spec/support files
|
11
|
+
Dir[File.join File.dirname(__FILE__), 'support', '**', '*.rb'].each { |f| require f }
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module RSpecMacros
|
4
|
+
def self.included(spec)
|
5
|
+
spec.let(:config) do
|
6
|
+
{
|
7
|
+
folder: '/path/to/your/log/folder',
|
8
|
+
class_name: Logger,
|
9
|
+
class_args: [ 'daily', 1024 ],
|
10
|
+
level: Logger::INFO,
|
11
|
+
formatter: Logger::Formatter.new,
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marcelo Correia Pinheiro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70257554434120 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70257554434120
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70257554433360 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70257554433360
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: resque
|
38
|
+
requirement: &70257554432580 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70257554432580
|
47
|
+
description: A simple mechanism to create custom log files based on queue names of
|
48
|
+
Resque workers.
|
49
|
+
email:
|
50
|
+
- salizzar@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- Gemfile
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- UNLICENSE
|
61
|
+
- lib/resque-logger.rb
|
62
|
+
- lib/resque/plugins/logger.rb
|
63
|
+
- lib/resque/plugins/logger/factory.rb
|
64
|
+
- lib/resque/setup.rb
|
65
|
+
- lib/resque_logger.rb
|
66
|
+
- lib/resque_logger/version.rb
|
67
|
+
- resque-logger.gemspec
|
68
|
+
- spec/lib/resque/plugins/logger/factory_spec.rb
|
69
|
+
- spec/lib/resque/plugins/logger_spec.rb
|
70
|
+
- spec/lib/resque_logger_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- spec/support/rspec_macros.rb
|
73
|
+
homepage: ''
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: -2417837300736213772
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
hash: -2417837300736213772
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project: resque-logger
|
99
|
+
rubygems_version: 1.8.10
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: A Resque plugin to provides logger for each worker
|
103
|
+
test_files:
|
104
|
+
- spec/lib/resque/plugins/logger/factory_spec.rb
|
105
|
+
- spec/lib/resque/plugins/logger_spec.rb
|
106
|
+
- spec/lib/resque_logger_spec.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
- spec/support/rspec_macros.rb
|