custom_rails_logger 0.0.1
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 +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/custom_rails_logger.gemspec +23 -0
- data/lib/custom_rails_logger/configuration.rb +12 -0
- data/lib/custom_rails_logger/formatter.rb +39 -0
- data/lib/custom_rails_logger/rails_ext/rack/logger.rb +10 -0
- data/lib/custom_rails_logger/version.rb +3 -0
- data/lib/custom_rails_logger.rb +15 -0
- data/test/custom_rails_logger_test.rb +85 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0473180c06ce711214a7ea5f49c4a71837a1993a
|
4
|
+
data.tar.gz: 346e62f0372992f2a8e469f9c925c1988127c893
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8a4253096b441b10a5c8492ec6b0f4a74decce978987ac21cf18cbe81428902d87e5b35e18f7bf6d5b0aaec47baabcd100f91a8ffd27fab4531ab1533b50316a
|
7
|
+
data.tar.gz: 01aca1ba5e63e5e4d60c18156c4be7b80ef4760a5162b22cbf828e8d680cca768f7a7ce0295903b130d98ac70e2fcca912f20340f64e87c4fd01640932457b4e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Suguru Odai
|
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,54 @@
|
|
1
|
+
# CustomRailsLogger
|
2
|
+
|
3
|
+
# Installation
|
4
|
+
|
5
|
+
Add `custom_rails_logger` in the `Gemfile`.
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'custom_rails_logger'
|
9
|
+
```
|
10
|
+
|
11
|
+
Download and install by running.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
bundle install
|
15
|
+
```
|
16
|
+
|
17
|
+
# Configuration
|
18
|
+
|
19
|
+
Create an initializer in the `config/initializers/custom_rails_logger.rb`.
|
20
|
+
|
21
|
+
That by adding with:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
CustomRailsLogger.configure do |config|
|
25
|
+
config.started_request_message_format = 'Started %m "%f" for %a by %u at %t'
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
When requested to `/session/new`, write the customized log message to `log/*.log`.
|
30
|
+
|
31
|
+
```
|
32
|
+
Started GET "/session/new" for 128.0.0.1 by Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0 at 2012-09-26 14:51:42 -0700
|
33
|
+
```
|
34
|
+
|
35
|
+
# Custom Log Formats
|
36
|
+
|
37
|
+
```
|
38
|
+
Format String
|
39
|
+
%%: The percent sign
|
40
|
+
%a: The request IP-address
|
41
|
+
%{Foobar}C: The contents of cookie Foobar in the request sent to the server.
|
42
|
+
%{Foobar}e: The contents of Foobar: header line(s) in the request sent to the server.
|
43
|
+
%f: The filtered path
|
44
|
+
%h: The request Host
|
45
|
+
%m: The request method
|
46
|
+
%p: The port of the server serving the request
|
47
|
+
%s: The request schema
|
48
|
+
%t: Time the request was received
|
49
|
+
%u: The request user agent
|
50
|
+
```
|
51
|
+
|
52
|
+
# Get involved
|
53
|
+
|
54
|
+
TODO
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'custom_rails_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "custom_rails_logger"
|
8
|
+
spec.version = CustomRailsLogger::VERSION
|
9
|
+
spec.authors = ["Suguru Odai"]
|
10
|
+
spec.email = ["ourqwers@gmail.com"]
|
11
|
+
spec.summary = %q{custom_rails_logger}
|
12
|
+
spec.description = %q{Custom a access log format for Rails.}
|
13
|
+
spec.homepage = "https://github.com/odaillyjp/custom_rails_logger"
|
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
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module CustomRailsLogger
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :started_request_message_format
|
4
|
+
|
5
|
+
# Default format
|
6
|
+
# started request message:
|
7
|
+
# 'Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700'
|
8
|
+
def initialize
|
9
|
+
@started_request_message_format = 'Started %m "%f" for %a at %t'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module CustomRailsLogger
|
2
|
+
module Formatter
|
3
|
+
module_function
|
4
|
+
|
5
|
+
# Format String
|
6
|
+
# %%: The percent sign
|
7
|
+
# %a: The request IP-address
|
8
|
+
# %{Foobar}C: The contents of cookie Foobar in the request sent to the server.
|
9
|
+
# %{Foobar}e: The contents of Foobar: header line(s) in the request sent to the server.
|
10
|
+
# %f: The filtered path
|
11
|
+
# %h: The request Host
|
12
|
+
# %m: The request method
|
13
|
+
# %p: The port of the server serving the request
|
14
|
+
# %s: The request schema
|
15
|
+
# %t: Time the request was received
|
16
|
+
# %u: The request user agent
|
17
|
+
def call(message_format, request)
|
18
|
+
tr_maps = message_format.scan(/%(?:{(.+?)})?(.)/)
|
19
|
+
.map do |var_name, format_string|
|
20
|
+
case format_string
|
21
|
+
when '%' then '%'
|
22
|
+
when 'a' then request.ip
|
23
|
+
when 'C' then request.cookies[var_name]
|
24
|
+
when 'e' then request.env[var_name]
|
25
|
+
when 'f' then request.filtered_path
|
26
|
+
when 'h' then request.host
|
27
|
+
when 'm' then request.request_method
|
28
|
+
when 'p' then request.port
|
29
|
+
when 's' then request.scheme
|
30
|
+
when 't' then Time.now.to_s
|
31
|
+
when 'u' then request.user_agent
|
32
|
+
else "%#{format_string}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
message_format.gsub(/%(?:{.+?})?./, '%s') % tr_maps
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Rails
|
2
|
+
module Rack
|
3
|
+
class Logger < ActiveSupport::LogSubscriber
|
4
|
+
def started_request_message(request)
|
5
|
+
message_format = CustomRailsLogger.configuration.started_request_message_format
|
6
|
+
CustomRailsLogger::Formatter.call(message_format, request)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'custom_rails_logger/configuration'
|
2
|
+
require 'custom_rails_logger/formatter'
|
3
|
+
require 'custom_rails_logger/version'
|
4
|
+
require 'custom_rails_logger/rails_ext/rack/logger'
|
5
|
+
|
6
|
+
module CustomRailsLogger
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= CustomRailsLogger::Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
yield configuration if block_given?
|
13
|
+
configuration.setting
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'minitest/unit'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.expand_path('../lib/', __dir__))
|
5
|
+
require 'custom_rails_logger/formatter'
|
6
|
+
|
7
|
+
class DummyRequest
|
8
|
+
attr_accessor :ip,
|
9
|
+
:cookies,
|
10
|
+
:env,
|
11
|
+
:filtered_path,
|
12
|
+
:host,
|
13
|
+
:request_method,
|
14
|
+
:port,
|
15
|
+
:scheme,
|
16
|
+
:user_agent
|
17
|
+
end
|
18
|
+
|
19
|
+
module CustomRailsLogger
|
20
|
+
class TestFormatter < MiniTest::Unit::TestCase
|
21
|
+
def setup
|
22
|
+
@request = DummyRequest.new.tap do |req|
|
23
|
+
req.ip = '192.168.0.1'
|
24
|
+
req.cookies = { 'foo' => 'foo' }
|
25
|
+
req.env = { 'bar' => 'bar'}
|
26
|
+
req.filtered_path = '/path/to'
|
27
|
+
req.host = 'localhost'
|
28
|
+
req.request_method = 'GET'
|
29
|
+
req.port = '80'
|
30
|
+
req.scheme = 'http'
|
31
|
+
req.user_agent = 'Browser'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_call_parcent_pattern
|
36
|
+
result = CustomRailsLogger::Formatter.call('Started % %% %%%', @request)
|
37
|
+
assert_equal 'Started % % %%', result
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_call_ip_pattern
|
41
|
+
result = CustomRailsLogger::Formatter.call('Started %a %%a %%%a', @request)
|
42
|
+
assert_equal 'Started 192.168.0.1 %a %192.168.0.1', result
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_call_cookies_pattern
|
46
|
+
result = CustomRailsLogger::Formatter.call('Started %{foo}C %{hoge}C', @request)
|
47
|
+
assert_equal 'Started foo ', result
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_call_env_pattern
|
51
|
+
result = CustomRailsLogger::Formatter.call('Started %{bar}e %{hoge}e', @request)
|
52
|
+
assert_equal 'Started bar ', result
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_call_path_pattern
|
56
|
+
result = CustomRailsLogger::Formatter.call('Started "%f"', @request)
|
57
|
+
assert_equal 'Started "/path/to"', result
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_call_host_pattern
|
61
|
+
result = CustomRailsLogger::Formatter.call('Started %h', @request)
|
62
|
+
assert_equal 'Started localhost', result
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_call_method_pattern
|
66
|
+
result = CustomRailsLogger::Formatter.call('Started %m', @request)
|
67
|
+
assert_equal 'Started GET', result
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_call_port_pattern
|
71
|
+
result = CustomRailsLogger::Formatter.call('Started %p', @request)
|
72
|
+
assert_equal 'Started 80', result
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_call_scheme_pattern
|
76
|
+
result = CustomRailsLogger::Formatter.call('Started %s', @request)
|
77
|
+
assert_equal 'Started http', result
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_call_agent_pattern
|
81
|
+
result = CustomRailsLogger::Formatter.call('Started %u', @request)
|
82
|
+
assert_equal 'Started Browser', result
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: custom_rails_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Suguru Odai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-11 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
|
+
description: Custom a access log format for Rails.
|
42
|
+
email:
|
43
|
+
- ourqwers@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- custom_rails_logger.gemspec
|
54
|
+
- lib/custom_rails_logger.rb
|
55
|
+
- lib/custom_rails_logger/configuration.rb
|
56
|
+
- lib/custom_rails_logger/formatter.rb
|
57
|
+
- lib/custom_rails_logger/rails_ext/rack/logger.rb
|
58
|
+
- lib/custom_rails_logger/version.rb
|
59
|
+
- test/custom_rails_logger_test.rb
|
60
|
+
homepage: https://github.com/odaillyjp/custom_rails_logger
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.2.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: custom_rails_logger
|
84
|
+
test_files:
|
85
|
+
- test/custom_rails_logger_test.rb
|