structured-event-logger 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/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +9 -0
- data/lib/structured-event-logger.rb +1 -0
- data/lib/structured_event_logger.rb +63 -0
- data/lib/structured_event_logger/version.rb +3 -0
- data/structured-event-logger.gemspec +28 -0
- data/test/structured_event_logger_test.rb +67 -0
- data/test/test_helper.rb +8 -0
- metadata +159 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Shopify Inc.
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
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
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# StructuredEventLogger
|
2
|
+
|
3
|
+
Structured event logger that writes events to both a human readable log and a JSON formatted log
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'structured-event-logger'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install structured-event-logger
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
# Creating an instance
|
22
|
+
json_logger = Logger.new('events.log')
|
23
|
+
human_readable_logger = Rails.logger
|
24
|
+
event_logger = StructuredEventLogger.new(json_logger, human_readable_logger)
|
25
|
+
|
26
|
+
# Basic usage
|
27
|
+
event_logger.event('scope', event, field: 'value', other_field, 'other value')
|
28
|
+
|
29
|
+
# Add context per thread/request (e.g. in an around_filter)
|
30
|
+
around_filter do
|
31
|
+
event_logger.add_context(my_value: 'whatever')
|
32
|
+
yield
|
33
|
+
event_logger.delete_context
|
34
|
+
end
|
35
|
+
|
36
|
+
# later, while processing a request inside that filter
|
37
|
+
event_logger.event('scope', 'event', other_value: 'blah') # will also include { my_value: 'whatever' }
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'structured_event_logger'
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'active_support/json'
|
3
|
+
|
4
|
+
class StructuredEventLogger
|
5
|
+
|
6
|
+
attr_reader :json_logger, :unstructured_logger
|
7
|
+
|
8
|
+
def initialize(json_logger, unstructured_logger = nil)
|
9
|
+
@json_logger, @unstructured_logger = json_logger, unstructured_logger
|
10
|
+
@thread_contexts = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def log(msg = nil)
|
14
|
+
unstructured_logger.add(nil, msg)
|
15
|
+
end
|
16
|
+
|
17
|
+
def event(scope, event, content = {})
|
18
|
+
log_event({:scope => scope, :event => event}.merge(flatten_hash(content)))
|
19
|
+
end
|
20
|
+
|
21
|
+
def context
|
22
|
+
@thread_contexts[thread_key] ||= {}
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def format_hash(hash, separator = ', ')
|
28
|
+
"[Event Logger] " + hash.map {|k, v| "#{k}=#{escape(v)}"}.join(separator)
|
29
|
+
end
|
30
|
+
|
31
|
+
def escape(value)
|
32
|
+
output = value.to_s
|
33
|
+
if output =~ /[\s"\\]/
|
34
|
+
'"' + output.gsub('\\', '\\\\\\').gsub('"', '\\"') + '"'
|
35
|
+
else
|
36
|
+
output
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def flatten_hash(hash, keys = nil, separator = "_")
|
41
|
+
flat_hash = {}
|
42
|
+
hash.each_pair do |key, val|
|
43
|
+
conc_key = keys.nil? ? key : "#{keys}#{separator}#{key}"
|
44
|
+
if val.is_a?(Hash)
|
45
|
+
flat_hash.merge!(flatten_hash(val, conc_key))
|
46
|
+
else
|
47
|
+
flat_hash[conc_key] = val
|
48
|
+
end
|
49
|
+
end
|
50
|
+
flat_hash
|
51
|
+
end
|
52
|
+
|
53
|
+
def log_event(hash)
|
54
|
+
unstructured_logger.add(nil, format_hash(hash)) if unstructured_logger
|
55
|
+
hash = hash.merge(context)
|
56
|
+
hash[:timestamp] ||= Time.now.utc
|
57
|
+
json_logger.add(nil, ActiveSupport::JSON.encode(hash))
|
58
|
+
end
|
59
|
+
|
60
|
+
def thread_key
|
61
|
+
Thread.current
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'structured_event_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "structured-event-logger"
|
8
|
+
spec.version = StructuredEventLogger::VERSION
|
9
|
+
spec.authors = ["Emilie Noel", "Aaron Olson", "Willem van Bergen"]
|
10
|
+
spec.email = ["willem@shopify.com"]
|
11
|
+
spec.description = %q{Structured event logging interface}
|
12
|
+
spec.summary = %q{Structured event logger that writes events to both a human readable log and a JSON formatted log}
|
13
|
+
spec.homepage = "https://github.com/Shopify/structured-event-logger"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "activesupport", "~> 3.2"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "minitest"
|
26
|
+
spec.add_development_dependency "mocha"
|
27
|
+
spec.add_development_dependency "timecop"
|
28
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
class StructuredEventLoggerTest < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@json_logger = Logger.new(StringIO.new)
|
7
|
+
@buffered_logger = Logger.new(StringIO.new)
|
8
|
+
@event_logger = StructuredEventLogger.new(@json_logger, @buffered_logger)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_log_msg_to_buffered_logger
|
12
|
+
@buffered_logger.expects(:add).with(nil, "a message")
|
13
|
+
@json_logger.expects(:add).never
|
14
|
+
|
15
|
+
@event_logger.log "a message"
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_log_event_to_both_loggers
|
19
|
+
Timecop.freeze(Time.now) do
|
20
|
+
@buffered_logger.expects(:add).with(nil, "[Event Logger] scope=render, event=error, status=status, message=message")
|
21
|
+
@json_logger.expects(:add).with(nil, "{\"scope\":\"render\",\"event\":\"error\",\"status\":\"status\",\"message\":\"message\",\"timestamp\":\"#{Time.now.utc.strftime('%FT%TZ')}\"}")
|
22
|
+
|
23
|
+
@event_logger.event "render", "error", {:status => "status", :message => "message"}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_log_flatten_hash
|
28
|
+
Timecop.travel(Time.now) do
|
29
|
+
@buffered_logger.expects(:add).with(nil, "[Event Logger] scope=render, event=error, status=status, message_first=first, message_second=second")
|
30
|
+
@json_logger.expects(:add).with(nil, "{\"scope\":\"render\",\"event\":\"error\",\"status\":\"status\",\"message_first\":\"first\",\"message_second\":\"second\",\"timestamp\":\"#{Time.now.utc.strftime('%FT%TZ')}\"}")
|
31
|
+
@event_logger.event "render", "error", {:status => "status", :message => {:first => "first", :second => "second"}}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_should_log_to_current_context
|
36
|
+
Timecop.freeze(Time.now) do
|
37
|
+
@json_logger.expects(:add).with(nil, "{\"scope\":\"render\",\"event\":\"error\",\"request_id\":\"2\",\"timestamp\":\"#{Time.now.utc.strftime('%FT%TZ')}\"}")
|
38
|
+
|
39
|
+
Thread.new do
|
40
|
+
@event_logger.context[:request_id] = '1'
|
41
|
+
|
42
|
+
Thread.new do
|
43
|
+
@event_logger.context[:request_id] = '2'
|
44
|
+
@event_logger.event :render, :error
|
45
|
+
end.join
|
46
|
+
end.join
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_delete_context
|
51
|
+
|
52
|
+
Timecop.freeze(Time.now) do
|
53
|
+
order = sequence('log message order')
|
54
|
+
@json_logger.expects(:add).with(nil, "{\"scope\":\"render\",\"event\":\"error\",\"request_id\":\"1\",\"timestamp\":\"#{Time.now.utc.strftime('%FT%TZ')}\"}").in_sequence(order)
|
55
|
+
@json_logger.expects(:add).with(nil, "{\"scope\":\"render\",\"event\":\"error\",\"timestamp\":\"#{Time.now.utc.strftime('%FT%TZ')}\"}").in_sequence(order)
|
56
|
+
|
57
|
+
Thread.new do
|
58
|
+
@event_logger.context[:request_id] = '1'
|
59
|
+
@event_logger.event :render, :error
|
60
|
+
@event_logger.context.clear
|
61
|
+
end.join
|
62
|
+
|
63
|
+
@event_logger.event :render, :error
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: structured-event-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Emilie Noel
|
9
|
+
- Aaron Olson
|
10
|
+
- Willem van Bergen
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activesupport
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '3.2'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ~>
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '3.2'
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: bundler
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ~>
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.3'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rake
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: minitest
|
66
|
+
requirement: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
type: :development
|
73
|
+
prerelease: false
|
74
|
+
version_requirements: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: mocha
|
82
|
+
requirement: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: timecop
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
description: Structured event logging interface
|
113
|
+
email:
|
114
|
+
- willem@shopify.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- .gitignore
|
120
|
+
- .travis.yml
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- lib/structured-event-logger.rb
|
126
|
+
- lib/structured_event_logger.rb
|
127
|
+
- lib/structured_event_logger/version.rb
|
128
|
+
- structured-event-logger.gemspec
|
129
|
+
- test/structured_event_logger_test.rb
|
130
|
+
- test/test_helper.rb
|
131
|
+
homepage: https://github.com/Shopify/structured-event-logger
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 1.8.23
|
153
|
+
signing_key:
|
154
|
+
specification_version: 3
|
155
|
+
summary: Structured event logger that writes events to both a human readable log and
|
156
|
+
a JSON formatted log
|
157
|
+
test_files:
|
158
|
+
- test/structured_event_logger_test.rb
|
159
|
+
- test/test_helper.rb
|