cabin 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cabin/channel.rb +3 -0
- data/lib/cabin/outputs/io.rb +37 -0
- data/test/test_logging.rb +11 -0
- data/test/test_metrics.rb +2 -1
- metadata +20 -18
data/lib/cabin/channel.rb
CHANGED
@@ -3,6 +3,7 @@ require "cabin/namespace"
|
|
3
3
|
require "cabin/timer"
|
4
4
|
require "cabin/context"
|
5
5
|
require "cabin/outputs/stdlib-logger"
|
6
|
+
require "cabin/outputs/io"
|
6
7
|
require "cabin/metrics"
|
7
8
|
require "logger"
|
8
9
|
|
@@ -80,6 +81,8 @@ class Cabin::Channel
|
|
80
81
|
# Wrap ruby stdlib Logger if given.
|
81
82
|
if output.is_a?(::Logger)
|
82
83
|
output = Cabin::Outputs::StdlibLogger.new(output)
|
84
|
+
elsif output.is_a?(::IO)
|
85
|
+
output = Cabin::Outputs::IO.new(output)
|
83
86
|
end
|
84
87
|
@outputs << output
|
85
88
|
# TODO(sissel): Return a method or object that allows you to easily
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "cabin"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
# Wrap IO objects with a reasonable log output.
|
5
|
+
#
|
6
|
+
# If the IO instance is attached to a TTY, the output will try to be a bit more
|
7
|
+
# human-friendly in this format:
|
8
|
+
#
|
9
|
+
# message {json data}
|
10
|
+
#
|
11
|
+
# If the IO instance is not attached to a TTY, the output will be the JSON
|
12
|
+
# representation of the event:
|
13
|
+
#
|
14
|
+
# { "timestamp": ..., "message": message, ... }
|
15
|
+
class Cabin::Outputs::IO
|
16
|
+
public
|
17
|
+
def initialize(io)
|
18
|
+
@io = io
|
19
|
+
end # def initialize
|
20
|
+
|
21
|
+
# Receive an event
|
22
|
+
public
|
23
|
+
def <<(event)
|
24
|
+
if @io.tty?
|
25
|
+
data = event.clone
|
26
|
+
# delete things from the 'data' portion that's not really data.
|
27
|
+
data.delete(:message)
|
28
|
+
data.delete(:timestamp)
|
29
|
+
message = "#{event[:message]} #{data.to_json}"
|
30
|
+
|
31
|
+
@io.puts(message)
|
32
|
+
@io.flush if @io.tty?
|
33
|
+
else
|
34
|
+
@io.puts(event.to_json)
|
35
|
+
end
|
36
|
+
end # def <<
|
37
|
+
end # class Cabin::Outputs::StdlibLogger
|
data/test/test_logging.rb
CHANGED
@@ -139,4 +139,15 @@ describe Cabin::Channel do
|
|
139
139
|
@logger.info("foo", { "foo" => "bar" }, "bar")
|
140
140
|
end
|
141
141
|
end
|
142
|
+
|
143
|
+
test "output to queue" do
|
144
|
+
require "thread"
|
145
|
+
queue = Queue.new
|
146
|
+
@logger.subscribe(queue)
|
147
|
+
|
148
|
+
@logger.info("Hello world")
|
149
|
+
event = queue.pop
|
150
|
+
assert_equal("Hello world", event[:message])
|
151
|
+
assert_equal(:info, event[:level])
|
152
|
+
end
|
142
153
|
end # describe Cabin::Channel do
|
data/test/test_metrics.rb
CHANGED
@@ -56,7 +56,7 @@ describe Cabin::Metrics do
|
|
56
56
|
30.times do |i|
|
57
57
|
assert_equal(i, timer.value)
|
58
58
|
assert_equal(i, timer.to_hash[:count])
|
59
|
-
timer.time {
|
59
|
+
timer.time { sleep(0.01) }
|
60
60
|
assert(timer.to_hash[:total] > 0, "total should be nonzero")
|
61
61
|
assert(timer.to_hash[:mean] > 0, "mean should be nonzero")
|
62
62
|
assert(timer.to_hash[:max] > 0, "max should be nonzero")
|
@@ -69,6 +69,7 @@ describe Cabin::Metrics do
|
|
69
69
|
assert_equal(i, timer.value)
|
70
70
|
assert_equal(i, timer.to_hash[:count])
|
71
71
|
t = timer.time
|
72
|
+
sleep(0.01)
|
72
73
|
t.stop
|
73
74
|
assert(timer.to_hash[:total] > 0, "total should be nonzero")
|
74
75
|
assert(timer.to_hash[:mean] > 0, "mean should be nonzero")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cabin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &17771920 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *17771920
|
25
25
|
description: This is an experiment to try and make logging more flexible and more
|
26
26
|
consumable. Plain text logs are bullshit, let's emit structured and contextual logs.
|
27
27
|
Metrics, too!
|
@@ -32,32 +32,33 @@ executables:
|
|
32
32
|
extensions: []
|
33
33
|
extra_rdoc_files: []
|
34
34
|
files:
|
35
|
-
- lib/cabin/inspectable.rb
|
36
|
-
- lib/cabin/context.rb
|
37
|
-
- lib/cabin/metric.rb
|
38
|
-
- lib/cabin/timer.rb
|
39
|
-
- lib/cabin/outputs/stdlib-logger.rb
|
40
|
-
- lib/cabin/outputs/em/stdlib-logger.rb
|
41
|
-
- lib/cabin/mixins/logger.rb
|
42
|
-
- lib/cabin/mixins/dragons.rb
|
43
|
-
- lib/cabin/mixins/CAPSLOCK.rb
|
44
|
-
- lib/cabin/namespace.rb
|
45
|
-
- lib/cabin/metrics/gauge.rb
|
46
35
|
- lib/cabin/metrics/histogram.rb
|
47
36
|
- lib/cabin/metrics/timer.rb
|
37
|
+
- lib/cabin/metrics/gauge.rb
|
48
38
|
- lib/cabin/metrics/meter.rb
|
49
39
|
- lib/cabin/metrics/counter.rb
|
50
|
-
- lib/cabin/metrics.rb
|
51
40
|
- lib/cabin/publisher.rb
|
52
41
|
- lib/cabin/channel.rb
|
42
|
+
- lib/cabin/mixins/logger.rb
|
43
|
+
- lib/cabin/mixins/dragons.rb
|
44
|
+
- lib/cabin/mixins/CAPSLOCK.rb
|
45
|
+
- lib/cabin/timer.rb
|
46
|
+
- lib/cabin/metric.rb
|
47
|
+
- lib/cabin/metrics.rb
|
48
|
+
- lib/cabin/namespace.rb
|
49
|
+
- lib/cabin/inspectable.rb
|
50
|
+
- lib/cabin/outputs/io.rb
|
51
|
+
- lib/cabin/outputs/stdlib-logger.rb
|
52
|
+
- lib/cabin/outputs/em/stdlib-logger.rb
|
53
|
+
- lib/cabin/context.rb
|
53
54
|
- lib/cabin.rb
|
54
55
|
- examples/fibonacci-timing.rb
|
55
56
|
- examples/sinatra-logging.rb
|
56
57
|
- examples/sample.rb
|
57
58
|
- examples/metrics.rb
|
58
59
|
- test/test_logging.rb
|
59
|
-
- test/minitest-patch.rb
|
60
60
|
- test/test_metrics.rb
|
61
|
+
- test/minitest-patch.rb
|
61
62
|
- test/all.rb
|
62
63
|
- LICENSE
|
63
64
|
- CHANGELIST
|
@@ -84,8 +85,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
85
|
version: '0'
|
85
86
|
requirements: []
|
86
87
|
rubyforge_project:
|
87
|
-
rubygems_version: 1.8.
|
88
|
+
rubygems_version: 1.8.16
|
88
89
|
signing_key:
|
89
90
|
specification_version: 3
|
90
91
|
summary: Experiments in structured and contextual logging
|
91
92
|
test_files: []
|
93
|
+
has_rdoc:
|