ey-logger 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91dc69daebff7bb091ae258a0a3765992d926858
4
+ data.tar.gz: 165cd1b6a487c826b470fdf842b22769951703d3
5
+ SHA512:
6
+ metadata.gz: 4ef0782ce54228ec7910ca6e160274df90035a5e9bedff473a35c11a2ba8aa819bc7cc50158bf63011c8ec7079de3701a5235f5fc07e08bd07ae5b3ae584f534
7
+ data.tar.gz: 8683940f8f23dbae363c5a7ba559af00a9297ee482560768466e4c5db0e0f6f632629d97a8a9f3a7658ee27fb2a922abefd4b4c1b4d10356242c8f80bdb1a912
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ey-logger.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'faraday', '~> 0.9'
9
+ gem 'rack-test'
10
+ gem 'pry-nav'
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Michelle Noorali & Josh Lane
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.
@@ -0,0 +1,31 @@
1
+ # Ey::Logger
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ey-logger'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ey-logger
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/ey-logger/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ey/logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ey-logger"
8
+ spec.version = Ey::Logger::VERSION
9
+ spec.authors = ["Michelle Noorali & Josh Lane"]
10
+ spec.email = ["mnoorali@engineyard.com"]
11
+ spec.summary = %q{Standardized logging helpers for EngineYard}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency "json", "~> 1.8"
25
+ spec.add_dependency "awesome_print", "~> 1.0"
26
+ end
@@ -0,0 +1,30 @@
1
+ require "ey/logger/version"
2
+
3
+ require "awesome_print"
4
+ require "json"
5
+
6
+ require "logger"
7
+
8
+ module Ey
9
+ class Logger
10
+
11
+ attr_reader :device, :formatter, :default_level, :prefix
12
+
13
+ def initialize(options={})
14
+ @device = options.fetch(:device)
15
+ @default_level = options[:default_level] || :info
16
+ @prefix = options[:prefix] || nil
17
+ @formatter = Ey::Logger::Formatter.for(options[:format] || :human).new(prefix)
18
+ end
19
+
20
+ def call(options)
21
+ level = options[:level] || default_level
22
+ output = formatter.call(options.fetch(:tag), options.fetch(:data))
23
+
24
+ device.public_send(level, output)
25
+ end
26
+ end
27
+ end
28
+
29
+ require 'ey/logger/formatter'
30
+ require 'ey/logger/machine_parser'
@@ -0,0 +1,41 @@
1
+ class Ey::Logger::Faraday
2
+
3
+ attr_reader :adapter
4
+
5
+ def initialize(app, options={})
6
+ @app = app
7
+
8
+ options[:prefix] ||= "ey.faraday"
9
+
10
+ @adapter = Ey::Logger.new(options)
11
+ end
12
+
13
+ def call(request_env)
14
+ # request phase
15
+ # :method - :get, :post, ...
16
+ # :url - URI for the current request; also contains GET parameters
17
+ # :body - POST parameters for :post/:put requests
18
+ # :request_headers
19
+ adapter.call(tag: "request", data: {
20
+ :method => request_env.method,
21
+ :url => request_env.url.to_s,
22
+ :headers => request_env.request_headers,
23
+ :body => request_env.body,
24
+ })
25
+
26
+ @app.call(request_env).on_complete do |response_env|
27
+ # response phase
28
+ # :status - HTTP response status code, such as 200
29
+ # :body - the response body
30
+ # :response_headers
31
+
32
+ adapter.call(tag: "response", data: {
33
+ :method => request_env.method,
34
+ :url => request_env.url.to_s,
35
+ :status => response_env.status,
36
+ :body => response_env.body,
37
+ :headers => response_env.response_headers,
38
+ })
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,26 @@
1
+ class Ey::Logger::Formatter
2
+ def self.types
3
+ @@types ||= {}
4
+ end
5
+
6
+ def self.for(type)
7
+ types.fetch(type)
8
+ end
9
+
10
+ attr_reader :prefix
11
+
12
+ def initialize(prefix)
13
+ @prefix = prefix
14
+ end
15
+
16
+ def call(_tag, data)
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def tag_for(tag)
21
+ [prefix, tag].compact.join(".")
22
+ end
23
+ end
24
+
25
+ require 'ey/logger/machine_formatter'
26
+ require 'ey/logger/human_formatter'
@@ -0,0 +1,10 @@
1
+ class Ey::Logger::HumanFormatter < Ey::Logger::Formatter
2
+
3
+ Ey::Logger::Formatter.types[:human] = self
4
+
5
+ def call(tag, data)
6
+ prefixed_tag = tag_for(tag)
7
+
8
+ {tag: prefixed_tag}.merge(data).ai
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class Ey::Logger::MachineFormatter < Ey::Logger::Formatter
2
+
3
+ Ey::Logger::Formatter.types[:machine] = self
4
+
5
+ def call(tag, data)
6
+ prefixed_tag = tag_for(tag)
7
+ "[#{prefixed_tag}] #{JSON.dump(data)}"
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class Ey::Logger::MachineParser
2
+ def self.parse(line)
3
+ tag, data = line.match(/\A\[(\S+)\]\s+(\{.*\})\z/).captures
4
+ new(tag, JSON.load(data))
5
+ end
6
+
7
+ attr_reader :tag, :data
8
+
9
+ def initialize(tag, data)
10
+ @tag, @data = tag, data
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Ey
2
+ class Logger
3
+ VERSION = "0.0.4"
4
+ end
5
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ require 'ey/logger/faraday'
3
+
4
+ describe 'Ey::Logger::Farday' do
5
+ let!(:output) { StringIO.new }
6
+ let!(:device) {
7
+ logger = Logger.new(output)
8
+ logger.formatter = proc { |_, _, _, msg| "#{msg}\n" }
9
+ logger
10
+ }
11
+ let!(:connection) {
12
+ Faraday.new { |builder|
13
+ builder.use Ey::Logger::Faraday, device: device, format: format
14
+ builder.adapter(:rack, lambda { |env| [200, {"Content-Type" => "application/json"}, ["{}"]] })
15
+ }
16
+ }
17
+
18
+ context "with machine formatting" do
19
+ let(:format) { :machine }
20
+
21
+ it "should record request data" do
22
+ connection.post do |req|
23
+ req.url 'http://nigiri'
24
+ req.headers['Content-Type'] = 'application/json'
25
+ req.body = '{ "name": "Unagi" }'
26
+ end
27
+ request_line, response_line = output.tap(&:rewind).read.split("\n")
28
+
29
+ request_output = Ey::Logger::MachineParser.parse(request_line)
30
+
31
+ expect(request_output.tag).to eq("ey.faraday.request")
32
+ expect(request_output.data).to eq({
33
+ "method" => "post",
34
+ "url" => "http://nigiri",
35
+ "headers" => {"User-Agent" => "Faraday v0.9.1", "Content-Type" => "application/json"},
36
+ "body" => "{ \"name\": \"Unagi\" }",
37
+ })
38
+
39
+ response_output = Ey::Logger::MachineParser.parse(response_line)
40
+
41
+ expect(response_output.tag).to eq("ey.faraday.response")
42
+ expect(response_output.data).to eq({
43
+ "status" => 200,
44
+ "method" => "post",
45
+ "url" => "http://nigiri",
46
+ "body" => "{}",
47
+ "headers" => {"Content-Type" => "application/json", "Content-Length" => "2"},
48
+ })
49
+ end
50
+ end
51
+
52
+ context "with human formatting" do
53
+ let(:format) { :human }
54
+
55
+ it "should record request data" do
56
+ connection.post do |req|
57
+ req.url 'http://nigiri'
58
+ req.headers['Content-Type'] = 'application/json'
59
+ req.body = '{ "name": "Unagi" }'
60
+ end
61
+
62
+ # hard to parse this
63
+ expect(output.tap(&:rewind).read).not_to be_empty
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,10 @@
1
+ require 'tempfile'
2
+
3
+ Bundler.require(:default, :test)
4
+
5
+ require File.expand_path("../../lib/ey/logger", __FILE__)
6
+ Dir[File.expand_path("../{shared,support}/*.rb", __FILE__)].each{|f| require(f)}
7
+
8
+ RSpec.configure do |config|
9
+ config.order = "random"
10
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ey-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Michelle Noorali & Josh Lane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: ''
70
+ email:
71
+ - mnoorali@engineyard.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - ey-logger.gemspec
82
+ - lib/ey/logger.rb
83
+ - lib/ey/logger/faraday.rb
84
+ - lib/ey/logger/formatter.rb
85
+ - lib/ey/logger/human_formatter.rb
86
+ - lib/ey/logger/machine_formatter.rb
87
+ - lib/ey/logger/machine_parser.rb
88
+ - lib/ey/logger/version.rb
89
+ - spec/faraday_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Standardized logging helpers for EngineYard
115
+ test_files:
116
+ - spec/faraday_spec.rb
117
+ - spec/spec_helper.rb
118
+ has_rdoc: