line_log 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 611389b342684afe006c3dcfeb262a30ab7c05d6
4
+ data.tar.gz: edf202c79a2d06c1efdb56ea98c371c9a3c72c58
5
+ SHA512:
6
+ metadata.gz: 8e39b9763572ba6072d4763f8f58a8c0c20e6664ff15f679887bade5d3c2cdfde5aac75ceddaa07a7c2f25138c0dd00845ffececcf691f2bcbeb36f7b37d36c9
7
+ data.tar.gz: c7f7ade086f9257d745eca89c5f68011456a1b2d57136b917115fc40e3a2635ee71c70608046d5b08e0cd826274c7ad5a6b843763c32716136d00b1afe0f6a76
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at skillvendor193@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in LineLog.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Skillvendor
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/LineLog.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'line_log/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "line_log"
8
+ spec.version = LineLog::VERSION
9
+ spec.authors = ["Skillvendor"]
10
+ spec.email = ["skillvendor193@gmail.com"]
11
+
12
+ spec.summary = %q{ custom logger for rack based applications }
13
+ spec.description = %q{ LineLog is built to help you have customs logs in Sinatra. LineLog welcomes contribution that would make it better and also any constructive feedback.}
14
+ spec.homepage = "https://github.com/Skillvendor/LineLog"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # LineLog
2
+
3
+ LineLog is built to help you have customs logs in sinatra(should support any rack application but it is not tested for that).
4
+
5
+ LineLog welcomes contribution that would make it better and also any constructive feedback.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'line_log'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install line_log
22
+
23
+ ## Usage
24
+
25
+ Initialize LineLog with a logger of your choosing.
26
+
27
+ ```
28
+ configure do
29
+ #implement custom logging
30
+ logger = Logger.new("#{RAILS_ROOT}/log/#{settings.environment}.log")
31
+ use LineLog::Customizer, logger
32
+ end
33
+
34
+ ```
35
+ By default the logger will output the method, path, format, ip, status and duration of the request:
36
+
37
+ ```
38
+ I, [2018-03-15T10:52:52.344791 #188] INFO -- : method='GET' path='/favicon.ico' format='image/webp,image/apng,image/*,*/*;q=0.8' ip='127.0.0.0' status=404 duration=2.36
39
+ ```
40
+
41
+ This can be overwritten by custom data.
42
+
43
+
44
+ To have your own custom data:
45
+
46
+ Form a hash with your custom data and pass it to data. The hash will be transformed into a string of key=value pairs separated by a space in the order they were given.
47
+
48
+ E.g: Before every action in sinatra add custom data to our request.
49
+
50
+ ```
51
+ before do
52
+ LineLog::Customizer.data = {
53
+ user: '-',
54
+ params: request.params,
55
+ agent: request.env['HTTP_USER_AGENT'] || '',
56
+ protocol: request.scheme,
57
+ requester: 'John Smith'
58
+ }
59
+ end
60
+
61
+ ```
62
+
63
+ The output of this would be:
64
+ ```
65
+ I, [2018-03-15T10:52:52.344791 #188] INFO -- : method='GET' path='/favicon.ico' format='image/webp,image/apng,image/*,*/*;q=0.8' ip='127.0.0.0' status=404 duration=0.00 user='-' params={} agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36' protocol='http' requester='John Smith'
66
+ ```
67
+
68
+ ## Inner Workings
69
+
70
+ LineLog::Customizer exposes a class variable(data) that you can populate with a custom hash. That hash is added to the log by calling the info method of the logger with a string that is formed from the hash given plus the default params explained above.
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/skillvendor/LineLog. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "line_log"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ module LineLog
2
+ class Customizer
3
+ class << self; attr_accessor :formatter, :data end
4
+
5
+ def initialize(app, logger=nil, formatter=LineLog::Formatters::KeyValue.new)
6
+ @app = app
7
+ @logger = logger
8
+ self.class.formatter = formatter
9
+ end
10
+
11
+ # making it thread safe. Visit https://github.com/cerner/gc_stats/issues/3 for more info about this
12
+ def call(event)
13
+ dup._call(event)
14
+ end
15
+
16
+ def _call(event)
17
+ began_at = Time.now
18
+ @status, @headers, @response = @app.call(event)
19
+
20
+ message = LineLog::MessageBuilder.new(event, @status, began_at).call
21
+ LineLog::Writer.call(message, @logger)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ module LineLog
2
+ module Formatters
3
+ class KeyValue
4
+ def call(data)
5
+ data.keys
6
+ .map { |key| format(key, data[key]) }
7
+ .join(' ')
8
+ end
9
+
10
+ protected
11
+
12
+ def format(key, value)
13
+ "#{key}=#{parse_value(key, value)}"
14
+ end
15
+
16
+ def parse_value(key, value)
17
+ # Exactly preserve the previous output
18
+ # Parsing this can be ambigious if the error messages contains
19
+ # a single quote
20
+ return "'#{value}'" if value.is_a? String
21
+ return Kernel.format('%.2f', value) if value.is_a? Float
22
+
23
+ value
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ module LineLog
2
+ class MessageBuilder
3
+ def initialize(event, status, began_at)
4
+ @event = event
5
+ @status = status
6
+ @began_at = began_at
7
+ end
8
+
9
+ def call
10
+ data = LineLog::RequestDataExtractor.call(@event, @status, @began_at)
11
+ formatted_message = LineLog::Customizer.formatter.call(data)
12
+
13
+ formatted_message
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+ module LineLog
2
+ class RequestDataExtractor
3
+ def self.call(event, status, began_at)
4
+ extract_data_from_request(event, status, began_at)
5
+ end
6
+
7
+ private
8
+
9
+ def self.extract_data_from_request(event, status, began_at)
10
+ data = default_data(event, status, began_at)
11
+ data.merge!(custom_data)
12
+ end
13
+
14
+ def self.default_data(event, status, began_at)
15
+ {
16
+ method: event['REQUEST_METHOD'],
17
+ path: extract_path(event),
18
+ format: event['HTTP_ACCEPT'],
19
+ ip: event['HTTP_X_REAL_IP'],
20
+ status: status,
21
+ duration: (Time.now - began_at).round(2)
22
+ }
23
+ end
24
+
25
+ def self.extract_path(event)
26
+ path = event['REQUEST_PATH']
27
+ index = path.index('?')
28
+ index ? path[0, index] : path
29
+ end
30
+
31
+ def self.custom_data
32
+ LineLog::Customizer.data || {}
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module LineLog
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ module LineLog
2
+ class Writer
3
+ def self.call(message, logger)
4
+ logger.info(message)
5
+ end
6
+ end
7
+ end
data/lib/line_log.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'line_log/version'
2
+ require 'line_log/writer'
3
+ require 'line_log/request_data_extractor'
4
+ require 'line_log/formatters/key_value'
5
+ require 'line_log/customizer'
6
+ require 'line_log/message_builder'
7
+
8
+ module LineLog
9
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: line_log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Skillvendor
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-15 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: " LineLog is built to help you have customs logs in Sinatra. LineLog
56
+ welcomes contribution that would make it better and also any constructive feedback."
57
+ email:
58
+ - skillvendor193@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - LineLog.gemspec
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/line_log.rb
75
+ - lib/line_log/customizer.rb
76
+ - lib/line_log/formatters/key_value.rb
77
+ - lib/line_log/message_builder.rb
78
+ - lib/line_log/request_data_extractor.rb
79
+ - lib/line_log/version.rb
80
+ - lib/line_log/writer.rb
81
+ homepage: https://github.com/Skillvendor/LineLog
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.2.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: custom logger for rack based applications
105
+ test_files: []