datalogger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Rakefile +11 -0
- data/Readme.md +34 -0
- data/datalogger.gemspec +17 -0
- data/lib/datalogger/logger.rb +32 -0
- data/lib/datalogger/stdout_sink.rb +15 -0
- data/lib/datalogger/version.rb +3 -0
- data/lib/datalogger.rb +6 -0
- data/spec/logger_spec.rb +8 -0
- data/spec/stdout_sink_spec.rb +0 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# DataLogger (alpha)
|
2
|
+
A simple structured data logger. Pass it a hash, and it takes care of storing it to the data sink of your choice (currently stdout, though plugins are on the way).
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
```ruby
|
6
|
+
require 'datalogger'
|
7
|
+
|
8
|
+
logger = DataLogger::Logger.new('myapp')
|
9
|
+
logger.log a: 1, b: 2, c: 3
|
10
|
+
```
|
11
|
+
|
12
|
+
This will generate a log message to stdout that looks like so:
|
13
|
+
|
14
|
+
```
|
15
|
+
myapp a=1 b=2 c=3
|
16
|
+
```
|
17
|
+
|
18
|
+
Or, perhaps you'd like to log how long it takes to perform a given action:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
require 'datalogger'
|
22
|
+
|
23
|
+
logger = DataLogger::Logger.new('myapp')
|
24
|
+
logger.log(event: 'create_user') do
|
25
|
+
# code to create a user
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
This will generate a message that looks something like so:
|
30
|
+
|
31
|
+
```
|
32
|
+
myapp action=create_user at=start
|
33
|
+
myapp action=create_user at=finish elapsed=0.003116
|
34
|
+
```
|
data/datalogger.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/datalogger/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Michael Gorsuch"]
|
6
|
+
gem.email = ["michael.gorsuch@gmail.com"]
|
7
|
+
gem.description = %q{Easy to use structured data logger}
|
8
|
+
gem.summary = %q{Structurd data logger}
|
9
|
+
gem.homepage = "https://github.com/gorsuch/datalogger"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "datalogger"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = DataLogger::VERSION
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module DataLogger
|
2
|
+
class Logger
|
3
|
+
attr_accessor :component, :sinks
|
4
|
+
|
5
|
+
def initialize(component='app')
|
6
|
+
self.component = component
|
7
|
+
self.sinks = []
|
8
|
+
self.sinks << STDOUTSink.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def log_to_sinks(data)
|
12
|
+
self.sinks.each do |sink|
|
13
|
+
sink.log(self.component, data)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def log(data, &blk)
|
18
|
+
if data.kind_of?(String)
|
19
|
+
data = { message: data }
|
20
|
+
end
|
21
|
+
|
22
|
+
if blk
|
23
|
+
start = Time.now
|
24
|
+
log_to_sinks(data.merge at: 'start')
|
25
|
+
yield
|
26
|
+
log_to_sinks(data.merge at: 'finish', elapsed: (Time.now - start))
|
27
|
+
else
|
28
|
+
log_to_sinks(data)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DataLogger
|
2
|
+
class STDOUTSink
|
3
|
+
def initialize(sync=true)
|
4
|
+
STDOUT.sync = sync
|
5
|
+
end
|
6
|
+
|
7
|
+
def stringify_data(data)
|
8
|
+
data.inject("") { |s, (k,v)| s + "#{k}=#{v} " }.strip
|
9
|
+
end
|
10
|
+
|
11
|
+
def log(component, data)
|
12
|
+
puts "#{component} #{stringify_data(data)}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/datalogger.rb
ADDED
data/spec/logger_spec.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datalogger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Gorsuch
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-21 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Easy to use structured data logger
|
15
|
+
email:
|
16
|
+
- michael.gorsuch@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- Rakefile
|
24
|
+
- Readme.md
|
25
|
+
- datalogger.gemspec
|
26
|
+
- lib/datalogger.rb
|
27
|
+
- lib/datalogger/logger.rb
|
28
|
+
- lib/datalogger/stdout_sink.rb
|
29
|
+
- lib/datalogger/version.rb
|
30
|
+
- spec/logger_spec.rb
|
31
|
+
- spec/stdout_sink_spec.rb
|
32
|
+
homepage: https://github.com/gorsuch/datalogger
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.10
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Structurd data logger
|
56
|
+
test_files:
|
57
|
+
- spec/logger_spec.rb
|
58
|
+
- spec/stdout_sink_spec.rb
|