logr 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/logr/entry.rb +79 -0
- data/lib/logr/event.rb +17 -0
- data/lib/logr/json_formatter.rb +15 -0
- data/lib/logr/metric.rb +20 -0
- data/lib/logr/version.rb +3 -0
- data/lib/logr.rb +45 -0
- data/logr.gemspec +27 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: edc6a47b0f6e94f82ae3e8d98162b465cdd52d80
|
4
|
+
data.tar.gz: 797e47c86a404a48fca101fb572617311bf0ecb4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a472a43d476c50e83fd523ab22ebeee3cdea01463ffcda5d5724135dab53143404616ec5e58264e03cd239dcb12251e09d3ec90568a24446614d97ac4a35489
|
7
|
+
data.tar.gz: c2a461065fa95c510d37c5acc66d7eac59f0a3e965b6686a277a0da7b69bd505d3d5e1f7d1032c958558e4837527f94a53b984a4a0fffec885053420e615ca86
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Peter Marton
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Logr
|
2
|
+
|
3
|
+
Structured logging with metrics and events.
|
4
|
+
|
5
|
+
![Travis](https://api.travis-ci.org/sspinc/logr.svg?branch=master)
|
6
|
+
|
7
|
+
## Description
|
8
|
+
|
9
|
+
“Chaos was the law of nature; Order was the dream of man.”
|
10
|
+
― Henry Adams
|
11
|
+
|
12
|
+
Logr is a machine-friendly logging library for Ruby. It brings structure
|
13
|
+
to what is usually just a messy stream of human-readable output, so you
|
14
|
+
can later slice and dice your logs with ease. The library was designed with
|
15
|
+
monitoring and analytics platforms in mind: bringing together events and
|
16
|
+
metrics from your services and applications has never been easier.
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'logr'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install logr
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'logr'
|
38
|
+
|
39
|
+
class YourClass
|
40
|
+
def self.logger
|
41
|
+
@logger ||= Logr::Logger.new('your-logger-name')
|
42
|
+
end
|
43
|
+
|
44
|
+
.
|
45
|
+
.
|
46
|
+
.
|
47
|
+
|
48
|
+
# A complex event that you want to monitor and has metrics associated
|
49
|
+
YourClass.logger.event('event-name', arbitrary: 'context', add_what: 'you_need')
|
50
|
+
.monitored('Title of the event', 'A longer description.')
|
51
|
+
.metric('metric-name', 34.35)
|
52
|
+
.metric('loglines', 1234535, type: 'counter')
|
53
|
+
.info('Human readable old-school logline')
|
54
|
+
|
55
|
+
# A simple logline
|
56
|
+
YourClass.logger.warn('Oh-oh something is fishy!')
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
The first log line pretty printed:
|
61
|
+
```json
|
62
|
+
{
|
63
|
+
"timestamp":"2015-11-16 16:47:42 UTC",
|
64
|
+
"level":"INFO",
|
65
|
+
"logger":"your-logger-name",
|
66
|
+
"event":{
|
67
|
+
"name":"event-name",
|
68
|
+
"arbitrary":"context",
|
69
|
+
"add_what":"you_need",
|
70
|
+
"monitored":true,
|
71
|
+
"title":"Title of the event",
|
72
|
+
"text":"A longer description."},
|
73
|
+
"metrics":[
|
74
|
+
{
|
75
|
+
"name":"metric-name",
|
76
|
+
"value":34.35,
|
77
|
+
"type":"counter"
|
78
|
+
},
|
79
|
+
{
|
80
|
+
"name":"loglines",
|
81
|
+
"value":1234535,
|
82
|
+
"type":"counter"
|
83
|
+
}],
|
84
|
+
"message":"Human readable old-school logline"
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
Log line with simple message:
|
89
|
+
```json
|
90
|
+
{
|
91
|
+
"timestamp":"2015-11-16 16:47:42 UTC",
|
92
|
+
"level":"WARN",
|
93
|
+
"logger":"your-logger-name",
|
94
|
+
"message":"Oh-oh somethign is fishy!"
|
95
|
+
}
|
96
|
+
```
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
101
|
+
|
102
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sspinc/logr.
|
107
|
+
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
112
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "logr"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/logr/entry.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require "logr/event"
|
2
|
+
require "logr/metric"
|
3
|
+
|
4
|
+
module Logr
|
5
|
+
class Entry
|
6
|
+
|
7
|
+
attr_reader :metrics, :message
|
8
|
+
|
9
|
+
def initialize(logger, event=nil, metrics=[], message=nil)
|
10
|
+
@logger = logger
|
11
|
+
@event = event
|
12
|
+
@metrics = metrics
|
13
|
+
@message = message
|
14
|
+
end
|
15
|
+
|
16
|
+
def event(name=nil, tags={})
|
17
|
+
if name.nil?
|
18
|
+
@event
|
19
|
+
else
|
20
|
+
Entry.new(@logger, Event.new(name, tags), @metrics)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def with(tags={})
|
25
|
+
if @event.nil?
|
26
|
+
raise 'No event to append to. Please call #event first.'
|
27
|
+
end
|
28
|
+
Entry.new(@logger, @event.with(tags), @metrics)
|
29
|
+
end
|
30
|
+
|
31
|
+
def metric(name, value, type: "counter")
|
32
|
+
metric = Metric.new(name, value, type)
|
33
|
+
Entry.new(@logger, @event, @metrics + [metric])
|
34
|
+
end
|
35
|
+
|
36
|
+
def monitored(title, text)
|
37
|
+
if @event.nil?
|
38
|
+
raise 'No event to monitor. Please call #event first.'
|
39
|
+
end
|
40
|
+
event = @event.with(monitored: true, title: title, text: text)
|
41
|
+
Entry.new(@logger, event, @metrics)
|
42
|
+
end
|
43
|
+
|
44
|
+
def debug(message=nil)
|
45
|
+
add(:debug, message)
|
46
|
+
end
|
47
|
+
|
48
|
+
def info(message=nil)
|
49
|
+
add(:info, message)
|
50
|
+
end
|
51
|
+
|
52
|
+
def warn(message=nil)
|
53
|
+
add(:warn, message)
|
54
|
+
end
|
55
|
+
|
56
|
+
def error(message=nil)
|
57
|
+
add(:error, message)
|
58
|
+
end
|
59
|
+
|
60
|
+
def fatal(message=nil)
|
61
|
+
add(:fatal, message)
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_hash
|
65
|
+
result = {}
|
66
|
+
result[:event] = @event.to_hash if @event
|
67
|
+
result[:metrics] = @metrics.map(&:to_hash) if @metrics.any?
|
68
|
+
result[:message] = @message if @message
|
69
|
+
|
70
|
+
result
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
def add(severity, message)
|
75
|
+
entry = Entry.new(@logger, @event, @metrics, message)
|
76
|
+
@logger.send(severity, entry)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/logr/event.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Logr
|
2
|
+
class Event
|
3
|
+
attr_reader :name, :tags
|
4
|
+
|
5
|
+
def initialize(name, tags)
|
6
|
+
@name, @tags = name, tags
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_hash
|
10
|
+
{ name: @name }.merge(@tags)
|
11
|
+
end
|
12
|
+
|
13
|
+
def with(tags)
|
14
|
+
Event.new(@name, @tags.merge(tags))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/logr/metric.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Logr
|
2
|
+
class Metric
|
3
|
+
attr_reader :name, :value, :type
|
4
|
+
|
5
|
+
def initialize(name, value, type)
|
6
|
+
raise TypeError, "Metric must be a numeric value" unless value.is_a?(Numeric)
|
7
|
+
@name = name
|
8
|
+
@value = value
|
9
|
+
@type = type
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hash
|
13
|
+
{
|
14
|
+
name: name,
|
15
|
+
value: value,
|
16
|
+
type: type
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/logr/version.rb
ADDED
data/lib/logr.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "logger"
|
2
|
+
|
3
|
+
require "logr/version"
|
4
|
+
require "logr/entry"
|
5
|
+
require "logr/json_formatter"
|
6
|
+
|
7
|
+
module Logr
|
8
|
+
class Logger
|
9
|
+
def initialize(name, level: ::Logger::INFO, log_device: STDOUT)
|
10
|
+
@logger = ::Logger.new(log_device).tap do |logger|
|
11
|
+
logger.formatter = JSONFormatter.new
|
12
|
+
logger.progname = name
|
13
|
+
logger.level = level
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def event(name, tags={})
|
18
|
+
Entry.new(@logger).event(name, tags)
|
19
|
+
end
|
20
|
+
|
21
|
+
def metric(name, value, type: "counter")
|
22
|
+
Entry.new(@logger).metric(name, value, type: type)
|
23
|
+
end
|
24
|
+
|
25
|
+
def debug(message)
|
26
|
+
Entry.new(@logger).debug(message)
|
27
|
+
end
|
28
|
+
|
29
|
+
def info(message)
|
30
|
+
Entry.new(@logger).info(message)
|
31
|
+
end
|
32
|
+
|
33
|
+
def warn(message)
|
34
|
+
Entry.new(@logger).warn(message)
|
35
|
+
end
|
36
|
+
|
37
|
+
def error(message)
|
38
|
+
Entry.new(@logger).error(message)
|
39
|
+
end
|
40
|
+
|
41
|
+
def fatal(message)
|
42
|
+
Entry.new(@logger).fatal(message)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/logr.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'logr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "logr"
|
8
|
+
spec.version = Logr::VERSION
|
9
|
+
spec.authors = ["Peter Marton"]
|
10
|
+
spec.email = ["martonpe@secretsaucepartners.com"]
|
11
|
+
|
12
|
+
spec.summary = "Structured logging with events and metrics"
|
13
|
+
spec.description = "Structured logging with events and metrics"
|
14
|
+
spec.homepage = "https://github.com/sspinc/logr"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "byebug"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "timecop", "~> 0.8"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Marton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: timecop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.8'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.8'
|
83
|
+
description: Structured logging with events and metrics
|
84
|
+
email:
|
85
|
+
- martonpe@secretsaucepartners.com
|
86
|
+
executables:
|
87
|
+
- console
|
88
|
+
- setup
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/logr.rb
|
102
|
+
- lib/logr/entry.rb
|
103
|
+
- lib/logr/event.rb
|
104
|
+
- lib/logr/json_formatter.rb
|
105
|
+
- lib/logr/metric.rb
|
106
|
+
- lib/logr/version.rb
|
107
|
+
- logr.gemspec
|
108
|
+
homepage: https://github.com/sspinc/logr
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.4.5
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Structured logging with events and metrics
|
132
|
+
test_files: []
|