em_logger 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +47 -0
- data/LICENSE.md +20 -0
- data/README.md +36 -0
- data/Rakefile +15 -0
- data/em_logger.gemspec +24 -0
- data/lib/em_logger.rb +1 -0
- data/lib/em_logger/logger.rb +111 -0
- data/lib/em_logger/version.rb +5 -0
- data/spec/logger_spec.rb +111 -0
- data/spec/spec_helper.rb +6 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 032c8a3cd33d96de73fb75b23c08cad8a60ee8e3
|
4
|
+
data.tar.gz: e241e3b0bedbd54d6bc87e044cc820d837a7c0f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ed8f34da9959074d17935d17f81c66a39f3b5d76d31acf5e9ed785476cc51e407067365ecdd3d009d7cb72e3a9baa5ca76a9d58e3c3b079f95a0676de56e4721
|
7
|
+
data.tar.gz: 77f2ec37dd4f176edcc8dbecaa3e69cb56a99fffb06f534e6758aa0ca42428306a45184c4f6c8b26364bbf92e1162178b2c7260a64c274d27b1d0371233a2e64
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
em_logger (1.0)
|
5
|
+
eventmachine (>= 0.12.10)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
docile (1.1.5)
|
12
|
+
eventmachine (1.0.9.1)
|
13
|
+
json (1.8.3)
|
14
|
+
rake (10.5.0)
|
15
|
+
rspec (3.4.0)
|
16
|
+
rspec-core (~> 3.4.0)
|
17
|
+
rspec-expectations (~> 3.4.0)
|
18
|
+
rspec-mocks (~> 3.4.0)
|
19
|
+
rspec-core (3.4.1)
|
20
|
+
rspec-support (~> 3.4.0)
|
21
|
+
rspec-expectations (3.4.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.4.0)
|
24
|
+
rspec-mocks (3.4.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.4.0)
|
27
|
+
rspec-support (3.4.1)
|
28
|
+
simplecov (0.11.1)
|
29
|
+
docile (~> 1.1.0)
|
30
|
+
json (~> 1.8)
|
31
|
+
simplecov-html (~> 0.10.0)
|
32
|
+
simplecov-html (0.10.0)
|
33
|
+
yard (0.8.7.6)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
ruby
|
37
|
+
|
38
|
+
DEPENDENCIES
|
39
|
+
bundler (~> 1.0)
|
40
|
+
em_logger!
|
41
|
+
rake
|
42
|
+
rspec
|
43
|
+
simplecov
|
44
|
+
yard
|
45
|
+
|
46
|
+
BUNDLED WITH
|
47
|
+
1.10.6
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) Valery Mayatsky
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# EventMachine Logger
|
2
|
+
|
3
|
+
EM::Logger is a simple async wrapper around the ruby logger class. It responds to all the log levels you are familiar with
|
4
|
+
from existing loggers (info, debug, warn, etc.). The only difference is that it's instantiated by passing an existing logger in when initializing.
|
5
|
+
|
6
|
+
Getting started
|
7
|
+
---------------
|
8
|
+
1. Add SimpleCov to your `Gemfile` and `bundle install`:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'em_logger'
|
12
|
+
```
|
13
|
+
|
14
|
+
2. Require it in code and use:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'eventmachine'
|
18
|
+
require 'logger'
|
19
|
+
require 'em_logger'
|
20
|
+
|
21
|
+
logger = EM::Logger.new(Logger.new(STDOUT))
|
22
|
+
|
23
|
+
EM.run do
|
24
|
+
logger.debug('Wow!')
|
25
|
+
|
26
|
+
EM.stop
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
3. How does it work?
|
31
|
+
|
32
|
+
It pushes all your log requests into queue and the separate thread pops them and delegates to the standard Ruby logger.
|
33
|
+
|
34
|
+
## Copyright
|
35
|
+
|
36
|
+
Copyright (c) Valery Mayatsky. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
task :test => :spec
|
8
|
+
|
9
|
+
require 'yard'
|
10
|
+
namespace :doc do
|
11
|
+
YARD::Rake::YardocTask.new do |task|
|
12
|
+
task.files = ['LICENSE.md', 'lib/**/*.rb']
|
13
|
+
task.options = ['--markup', 'markdown']
|
14
|
+
end
|
15
|
+
end
|
data/em_logger.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'em_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'em_logger'
|
8
|
+
gem.version = EventMachine::Logger::VERSION
|
9
|
+
gem.homepage = 'https://github.com/Arugin/em_logger'
|
10
|
+
|
11
|
+
gem.author = 'Valery Mayatsky'
|
12
|
+
gem.email = 'valerymayatsky@gmail.com'
|
13
|
+
gem.description = 'An async wrapper for ruby logger for EventMachine applications.'
|
14
|
+
gem.summary = 'An async wrapper for ruby logger for EventMachine applications.'
|
15
|
+
|
16
|
+
gem.add_dependency 'eventmachine', '>= 0.12.10'
|
17
|
+
gem.add_development_dependency 'bundler', '~> 1.0'
|
18
|
+
|
19
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
|
20
|
+
gem.files = `git ls-files`.split("\n")
|
21
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
|
23
|
+
gem.require_paths = ['lib']
|
24
|
+
end
|
data/lib/em_logger.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'em_logger/logger'
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'eventmachine'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module EventMachine
|
5
|
+
class LogMessage
|
6
|
+
attr_accessor :severity, :message, :progname
|
7
|
+
|
8
|
+
def initialize(severity, message = nil, progname = nil)
|
9
|
+
@severity = severity
|
10
|
+
@message = message
|
11
|
+
@progname = progname
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class Logger
|
17
|
+
|
18
|
+
attr_reader :logger
|
19
|
+
attr_reader :logger_queue
|
20
|
+
|
21
|
+
def self.logger(logger = nil)
|
22
|
+
@logger ||= new(logger || ::Logger.new(STDOUT))
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(logger)
|
26
|
+
@logger = logger
|
27
|
+
@logger_queue = ::Queue.new
|
28
|
+
|
29
|
+
start_worker
|
30
|
+
|
31
|
+
EM.add_shutdown_hook { drain } if EM.reactor_running?
|
32
|
+
end
|
33
|
+
|
34
|
+
def add(severity, message = nil, progname = nil)
|
35
|
+
return true if severity < @logger.level
|
36
|
+
if message.nil?
|
37
|
+
if block_given?
|
38
|
+
message = yield
|
39
|
+
else
|
40
|
+
message = progname
|
41
|
+
progname = @logger.progname
|
42
|
+
end
|
43
|
+
end
|
44
|
+
@logger_queue.push(LogMessage.new(severity, message, progname))
|
45
|
+
@worker.wakeup
|
46
|
+
end
|
47
|
+
|
48
|
+
alias log add
|
49
|
+
|
50
|
+
def debug(progname = nil, &block)
|
51
|
+
add(::Logger::DEBUG, nil, progname, &block)
|
52
|
+
end
|
53
|
+
|
54
|
+
def info(progname = nil, &block)
|
55
|
+
add(::Logger::INFO, nil, progname, &block)
|
56
|
+
end
|
57
|
+
|
58
|
+
def warn(progname = nil, &block)
|
59
|
+
add(::Logger::WARN, nil, progname, &block)
|
60
|
+
end
|
61
|
+
|
62
|
+
def error(progname = nil, &block)
|
63
|
+
add(::Logger::ERROR, nil, progname, &block)
|
64
|
+
end
|
65
|
+
|
66
|
+
def fatal(progname = nil, &block)
|
67
|
+
add(::Logger::FATAL, nil, progname, &block)
|
68
|
+
end
|
69
|
+
|
70
|
+
def unknown(progname = nil, &block)
|
71
|
+
add(::Logger::UNKNOWN, nil, progname, &block)
|
72
|
+
end
|
73
|
+
|
74
|
+
def <<(data)
|
75
|
+
@logger_queue.push(LogMessage.new(nil, data))
|
76
|
+
end
|
77
|
+
|
78
|
+
def method_missing(method, *args, &block)
|
79
|
+
return super unless @logger.respond_to?(method)
|
80
|
+
@logger.send(method, *args, &block)
|
81
|
+
end
|
82
|
+
|
83
|
+
def respond_to?(method, include_private = false)
|
84
|
+
@logger.respond_to?(method, include_private) || super(method, include_private)
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def drain
|
90
|
+
until @worker.stop?
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def start_worker
|
96
|
+
@worker = Thread.new do
|
97
|
+
|
98
|
+
loop do
|
99
|
+
until @logger_queue.empty?
|
100
|
+
log_message = @logger_queue.pop(true)
|
101
|
+
@logger.add(log_message.severity, log_message.message, log_message.progname)
|
102
|
+
end
|
103
|
+
Thread.stop
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
@worker.abort_on_exception = true
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
data/spec/logger_spec.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EventMachine::Logger do
|
4
|
+
let(:logger) { ::Logger.new(STDOUT) }
|
5
|
+
|
6
|
+
describe 'creating' do
|
7
|
+
subject { EventMachine::Logger.new(logger) }
|
8
|
+
|
9
|
+
it 'instantiates with a logger' do
|
10
|
+
EM.run_block do
|
11
|
+
expect(subject.logger).to eq(logger)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'adds a shutdown hook when the reactor is running' do
|
16
|
+
EM.run_block do
|
17
|
+
expect(EM).to receive(:add_shutdown_hook).and_yield
|
18
|
+
subject
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'starts the queue worker' do
|
23
|
+
expect(logger).to receive('add').once
|
24
|
+
|
25
|
+
EM.run_block do
|
26
|
+
subject.debug('this is a test')
|
27
|
+
EM.stop
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'log statements' do
|
33
|
+
let(:em_logger) { EventMachine::Logger.new(logger) }
|
34
|
+
|
35
|
+
%w(debug info warn error fatal).each do |l|
|
36
|
+
describe "##{l}" do
|
37
|
+
it 'pushes the log message onto the logger_queue' do
|
38
|
+
expect(em_logger.logger_queue).to receive('push').once
|
39
|
+
em_logger.send("#{l}", 'this is a test')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#unknown' do
|
45
|
+
it 'pushes the log message onto the logger_queue' do
|
46
|
+
expect(em_logger.logger_queue).to receive('push').once
|
47
|
+
em_logger.unknown('this is a test')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#<<' do
|
52
|
+
it 'pushes the log message onto the logger_queue' do
|
53
|
+
expect(em_logger.logger_queue).to receive('push').once
|
54
|
+
em_logger << 'this is a test'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'queuing' do
|
60
|
+
describe '#add' do
|
61
|
+
context 'when logging below the defined level' do
|
62
|
+
it 'pushes the log message onto the logger_queue' do
|
63
|
+
logger.level = ::Logger::WARN
|
64
|
+
em_logger = EventMachine::Logger.new(logger)
|
65
|
+
expect(em_logger.logger_queue).to_not receive('push')
|
66
|
+
expect(em_logger.add(::Logger::INFO, 'this is a test')).to be_truthy
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when logging above the defined level' do
|
71
|
+
it 'pushes the log message onto the logger_queue' do
|
72
|
+
em_logger = EventMachine::Logger.new(logger)
|
73
|
+
expect(em_logger.logger_queue).to receive('push').once
|
74
|
+
em_logger.add(::Logger::INFO, 'this is a test')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when using a block' do
|
79
|
+
it 'evaluates the block' do
|
80
|
+
em_logger = EventMachine::Logger.new(logger)
|
81
|
+
expect(em_logger.logger_queue).to receive('push').once
|
82
|
+
em_logger.add(::Logger::INFO) { 'ohai' }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'delegating to logger' do
|
89
|
+
describe 'method_missing' do
|
90
|
+
it 'passes through to the underlying logger' do
|
91
|
+
em_logger = EventMachine::Logger.new(logger)
|
92
|
+
expect(logger).to receive('level').once
|
93
|
+
em_logger.level
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'returns the underlying loggers value' do
|
97
|
+
logger.level = ::Logger::WARN
|
98
|
+
em_logger = EventMachine::Logger.new(logger)
|
99
|
+
expect(em_logger.level).to eq(::Logger::WARN)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'respond_to?' do
|
104
|
+
it 'responds to methods defined on the logger' do
|
105
|
+
em_logger = EventMachine::Logger.new(logger)
|
106
|
+
expect(em_logger.respond_to?('level')).to be_truthy
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Valery Mayatsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: eventmachine
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.12.10
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.12.10
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
description: An async wrapper for ruby logger for EventMachine applications.
|
42
|
+
email: valerymayatsky@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- ".gitignore"
|
48
|
+
- ".rspec"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.md
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- em_logger.gemspec
|
55
|
+
- lib/em_logger.rb
|
56
|
+
- lib/em_logger/logger.rb
|
57
|
+
- lib/em_logger/version.rb
|
58
|
+
- spec/logger_spec.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
homepage: https://github.com/Arugin/em_logger
|
61
|
+
licenses: []
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.4.8
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: An async wrapper for ruby logger for EventMachine applications.
|
83
|
+
test_files: []
|
84
|
+
has_rdoc:
|