app_logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3be24df59f69af60102d39b866fa7701562a560b
4
+ data.tar.gz: aaca211499bef43ec91482ba7881ec2f19a25548
5
+ SHA512:
6
+ metadata.gz: 232d22d8d7ae197207befad6bd1860c35047288c564b1bed8919e002e478c05bb95af5675b482ab28e424192c12cd8b190dc53cd9cde40cc288fd3f3278aed95
7
+ data.tar.gz: 6f12835cf9072e9c2b297dfcd4b3b876b335a6b970d84baebd3c4bb52bf908c93e3a33732eb06a730ce90c7b639b208768e7eaab8f1eeaae97416bfd03f61d06
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+ *.swp
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.2.4
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in app_logger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 arukoh
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,41 @@
1
+ # AppLogger
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/app_logger`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'app_logger'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install app_logger
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/app_logger.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
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
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'app_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "app_logger"
8
+ spec.version = AppLogger::VERSION
9
+ spec.authors = ["arukoh"]
10
+ spec.email = ["arukoh10@gmail.com"]
11
+
12
+ spec.summary = %q{Rack middleware to output like access log.}
13
+ spec.description = %q{Rack middleware to output like access log.}
14
+ spec.homepage = "https://github.com/arukoh/app_logger"
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.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.4"
25
+ spec.add_development_dependency "rack-test", "~> 0.6"
26
+
27
+ spec.add_dependency "rack", "~> 1.6"
28
+ spec.add_dependency "activesupport", "~> 4"
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "app_logger"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ module AppLogger
2
+ module Formatter
3
+ class Aws < Json
4
+ class << self
5
+ def pattern
6
+ pattern = []
7
+ pattern << ":client_class"
8
+ pattern << ":http_response_status_code"
9
+ pattern << ":time"
10
+ pattern << ":retries"
11
+ pattern << ":operation(:request_params)"
12
+ pattern << ":error_class"
13
+ pattern << ":error_message"
14
+ pattern.join(' ') + "\n"
15
+ end
16
+ end
17
+
18
+ private
19
+ def msg2hash(message)
20
+ msg = message.to_s.strip
21
+ client, code, time, retries, ope, e_class, e_msg = msg.split(/ /, 7)
22
+ {
23
+ _formatter: :aws,
24
+ client_class: client,
25
+ status_code: code,
26
+ response_time: time, # sec
27
+ retries: retries,
28
+ operation: ope,
29
+ error_class: e_class,
30
+ error_message: e_msg
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ require "json"
2
+
3
+ module AppLogger
4
+ module Formatter
5
+ class Json
6
+ FORMAT = "%s\n"
7
+
8
+ def initialize(hostname=`hostname`.chomp)
9
+ @hostname = hostname
10
+ end
11
+
12
+ def call(severity, datetime, progname, message)
13
+ msg = msg2hash(message)
14
+ FORMAT % common_msg(severity, datetime, progname).merge(msg).to_json
15
+ end
16
+
17
+ private
18
+ def msg2hash(message)
19
+ case message
20
+ when Hash
21
+ message
22
+ else
23
+ { log: message.to_s }
24
+ end
25
+ end
26
+
27
+ def common_msg(severity, datetime, progname)
28
+ {
29
+ _host: @hostname,
30
+ _pid: $$,
31
+ _severity: severity,
32
+ _time: datetime.iso8601,
33
+ _progname: progname
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ require "app_logger/formatter/json"
2
+ require "app_logger/formatter/aws"
@@ -0,0 +1,66 @@
1
+ require "logger"
2
+ require "rack"
3
+
4
+ module AppLogger
5
+ class Middleware < Rack::CommonLogger
6
+ attr_reader :log_level
7
+
8
+ def initialize(app, options)
9
+ super(app, options[:logger])
10
+ @log_level = options[:log_level] || Logger::INFO
11
+ end
12
+
13
+ private
14
+ FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %s\n}
15
+
16
+ def log(env, status, header, began_at)
17
+ msg = message(env, status, header, began_at)
18
+
19
+ logger = @logger || env['rack.errors']
20
+ if logger.respond_to?(:log)
21
+ msg[:request_line] = "#{msg[:method]} #{msg[:path]}#{msg[:query]} #{msg[:http_version]}"
22
+ logger.log(log_level, msg)
23
+ elsif logger.respond_to?(:write)
24
+ logger.write(format_message(msg))
25
+ else
26
+ logger << format_message(msg)
27
+ end
28
+ end
29
+
30
+ def message(env, status, header, began_at)
31
+ method = env[Rack::REQUEST_METHOD]
32
+ path = env[Rack::PATH_INFO]
33
+ query = env[Rack::QUERY_STRING].empty? ? "" : "?"+env[Rack::QUERY_STRING]
34
+ http_version = env["HTTP_VERSION"] || "-"
35
+
36
+ now = Time.now
37
+ {
38
+ host: env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
39
+ user: env["REMOTE_USER"] || "-",
40
+ time: now.iso8601,
41
+ method: method,
42
+ path: path,
43
+ query: query,
44
+ http_version: http_version,
45
+ status_code: status.to_s[0..3],
46
+ length: extract_content_length(header),
47
+ response_time: "%0.4f" % (now - began_at)
48
+ }
49
+ end
50
+
51
+ def format_message(msg)
52
+ FORMAT % [
53
+ msg[:host],
54
+ msg[:user],
55
+ msg[:time],
56
+ msg[:method],
57
+ msg[:path],
58
+ msg[:query],
59
+ msg[:http_version],
60
+ msg[:status_code],
61
+ msg[:length],
62
+ msg[:response_time]
63
+ ]
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module AppLogger
2
+ VERSION = "0.0.1"
3
+ end
data/lib/app_logger.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "app_logger/version"
2
+ require "app_logger/middleware"
3
+ require "app_logger/formatter"
4
+ require "active_support"
5
+
6
+ module AppLogger
7
+ class << self
8
+ def new_logger(logdev, level: nil, formatter: nil, console: false)
9
+ logger = ActiveSupport::Logger.new(logdev)
10
+ logger.level = level if level
11
+ logger.formatter = formatter if formatter
12
+ logger.extend ActiveSupport::Logger.broadcast(console_logger) if console
13
+ logger
14
+ end
15
+
16
+ def new_force_formatted_logger(logdev, level: nil, formatter: nil, console: false, severity: Logger::UNKNOWN)
17
+ logger = new_logger(logdev, level: level, formatter: formatter, console: console)
18
+ logger.instance_eval do |obj|
19
+ class << self
20
+ define_method :<< do |msg|
21
+ return true if @logdev.nil? or severity < @level
22
+ @logdev.write(format_message(format_severity(severity), Time.now, @progname, msg))
23
+ true
24
+ end
25
+ end
26
+ end
27
+ logger
28
+ end
29
+
30
+ def console_logger
31
+ @@console_logger ||= ActiveSupport::Logger.new(STDOUT)
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: app_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - arukoh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-24 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4'
97
+ description: Rack middleware to output like access log.
98
+ email:
99
+ - arukoh10@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - app_logger.gemspec
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/app_logger.rb
115
+ - lib/app_logger/formatter.rb
116
+ - lib/app_logger/formatter/aws.rb
117
+ - lib/app_logger/formatter/json.rb
118
+ - lib/app_logger/middleware.rb
119
+ - lib/app_logger/version.rb
120
+ homepage: https://github.com/arukoh/app_logger
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Rack middleware to output like access log.
144
+ test_files: []