log-analyzer 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eccd209a81035144e2f86390c024ab6cea0f64fc
4
- data.tar.gz: d0614f2ae83b0bf049da0ac37e5a99dd9c2ff4e2
3
+ metadata.gz: 9f8d3ab240961adfb8a7a8f95f38b19ab967dc88
4
+ data.tar.gz: c07499a1e0a7001e39cba2d8ccb606e3f066aa9e
5
5
  SHA512:
6
- metadata.gz: 3a70d4208ae639505149d6c9712ffcb7124d86259a9e406f46cdb8857aa2a65e122b9593abf4f6189622fc573dc3a0a577b09cce18cf6a0d34edaeafef6dfaec
7
- data.tar.gz: c38689ba60da004b6f63210f18b8bfa8b6bbef8035b5d5fbbe589a34d97d70920ffac30105352cc6b476c146019b17bc1a8de3f3129f0ac362b564fdccb27ac2
6
+ metadata.gz: c070b05e228ea3539d920b4a2bcee2552d96787bc5e9127537860d8d5e3e333860ae82ff4d16bcd97f09427e2d0a33813c70bc198665848b4bc287259c8818fa
7
+ data.tar.gz: 0549d9c8cd21aaba0df5c722f41e7f9ffdb10c1ee95eeeb3e4ab739f3ce83d65298cdcb4dcf145d86bde5e83cb9c58ea52bda4eb3b47644ae7183612c94ad94c
data/README.md CHANGED
@@ -30,6 +30,31 @@ Other Framework Application:
30
30
 
31
31
  $ log-analyzer -r routing_file <file ...>
32
32
 
33
+ ## CLI Reference
34
+
35
+ Help:
36
+
37
+ ```shell
38
+ $ log-analyzer --help
39
+ Usage: log-analyzer [options] <file ...>
40
+ -h, --help output usage information
41
+ -V, --version output the version number
42
+ -r FILE routing format file
43
+ -R, --route-regexp VALUE route regexp
44
+ -L, --log-regexp VALUE log regexp
45
+ ```
46
+
47
+ Example:
48
+
49
+ ```shell
50
+ $ log-analyzer -r path/to/routes.txt path/to/application.log
51
+ ┏━━━━━━━━┳━━━━━━━━━━┳━━━━━━━┓
52
+ ┃ method ┃ endpoint ┃ count ┃
53
+ ┣━━━━━━━━╊━━━━━━━━━━╊━━━━━━━┫
54
+ ┃ GET ┃ / ┃ 1 ┃
55
+ ┗━━━━━━━━┻━━━━━━━━━━┻━━━━━━━┛
56
+ ```
57
+
33
58
  ## Development
34
59
 
35
60
  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.
data/lib/log-analyzer.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'log-analyzer/version'
2
+ require 'log-analyzer/config'
2
3
  require 'log-analyzer/routes'
3
4
  require 'log-analyzer/analyze'
4
5
  require 'log-analyzer/log'
@@ -9,5 +10,13 @@ require 'log-analyzer/rails/railtie.rb' if defined?(Rails)
9
10
 
10
11
  module Log
11
12
  module Analyzer
13
+ module_function
14
+ def self.configure(&block)
15
+ yield(config)
16
+ end
17
+
18
+ def config
19
+ @_config ||= Config.new
20
+ end
12
21
  end
13
22
  end
@@ -3,7 +3,7 @@ require 'command_line_reporter'
3
3
  module Log::Analyzer
4
4
  class CLI
5
5
  def self.run(files, options)
6
- routing_text = File.read(options[:r])
6
+ routing_text = File.read(options[:routing_file])
7
7
  analyze = Analyze.new(files, routing_text)
8
8
  Report.new(analyze.sort).run
9
9
  end
@@ -18,12 +18,19 @@ op.on('-V', '--version', 'output the version number') do |v|
18
18
  exit
19
19
  end
20
20
 
21
- op.on('-r VALUE', 'routing format file') { |v| opts[:r] = v }
21
+ op.on('-r FILE', 'routing format file') { |v| opts[:routing_file] = v }
22
+ op.on('-R VALUE', '--route-regexp', 'route regexp') { |v| opts[:route_regexp] = v }
23
+ op.on('-L VALUE', '--log-regexp', 'log regexp') { |v| opts[:log_regexp] = v }
22
24
 
23
25
  op.parse!(ARGV)
24
26
 
25
27
  if ARGV.empty?
26
28
  puts op
27
29
  else
30
+ Log::Analyzer.configure do |config|
31
+ config.route_regexp = Regexp.new(opts[:route_regexp]) if opts[:route_regexp]
32
+ config.log_regexp = Regexp.new(opts[:log_regexp]) if opts[:log_regexp]
33
+ end
34
+
28
35
  Log::Analyzer::CLI.run(ARGV, opts)
29
36
  end
@@ -0,0 +1,10 @@
1
+ module Log::Analyzer
2
+ class Config
3
+ attr_accessor :log_regexp, :route_regexp
4
+
5
+ def initialize
6
+ @route_regexp = /(GET|POST|DELETE|PATCH|PUT)\s+([^\s]*)\s/i
7
+ @log_regexp = /Started (?<request_method>GET|POST|DELETE|PATCH|PUT) "(?<path_info>[^"]+)"/i
8
+ end
9
+ end
10
+ end
@@ -18,7 +18,7 @@ module Log::Analyzer
18
18
  end
19
19
 
20
20
  def self.all
21
- @endpoints.values
21
+ self.endpoints.values
22
22
  end
23
23
 
24
24
  def self.endpoints
@@ -1,14 +1,12 @@
1
1
  module Log::Analyzer
2
2
  class Log
3
- LOG_REGEXP = /Started (?<request_method>GET|POST|DELETE|PATCH|PUT) "(?<path_info>[^"]+)"/i
4
-
5
3
  def initialize(path)
6
4
  @file = File.open(path)
7
5
  end
8
6
 
9
7
  def each_line(&block)
10
8
  @file.each_line do |line|
11
- next unless md = line.match(LOG_REGEXP)
9
+ next unless md = line.match(regexp)
12
10
  env = md.names.inject({}) do |hash, name|
13
11
  hash[name.upcase] = md[name]
14
12
  hash
@@ -16,5 +14,10 @@ module Log::Analyzer
16
14
  block.call(ActionDispatch::Request.new(env))
17
15
  end
18
16
  end
17
+
18
+ private
19
+ def regexp
20
+ ::Log::Analyzer.config.log_regexp
21
+ end
19
22
  end
20
23
  end
@@ -3,14 +3,17 @@ require 'action_dispatch'
3
3
 
4
4
  module Log::Analyzer
5
5
  class Routes < ActionDispatch::Journey::Routes
6
- ROUTE_REGEXP = /(GET|POST|DELETE|PATCH|PUT)\s+([^\s]*)\s/i
7
-
8
6
  def initialize(routes_text)
9
7
  super()
10
- routes_text.scan(ROUTE_REGEXP) do |request_method, request_path|
8
+ routes_text.scan(regexp) do |request_method, request_path|
11
9
  path_pattern = Pattern.from_string(request_method, request_path)
12
10
  add_route(nil, path_pattern, {request_method: /^#{request_method}$/}, {}, {})
13
11
  end
14
12
  end
13
+
14
+ private
15
+ def regexp
16
+ ::Log::Analyzer.config.route_regexp
17
+ end
15
18
  end
16
19
  end
@@ -1,5 +1,5 @@
1
1
  module Log
2
2
  module Analyzer
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log-analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shiro16
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-01 00:00:00.000000000 Z
11
+ date: 2016-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -145,6 +145,7 @@ files:
145
145
  - lib/log-analyzer/analyze.rb
146
146
  - lib/log-analyzer/cli.rb
147
147
  - lib/log-analyzer/command/log-analyzer.rb
148
+ - lib/log-analyzer/config.rb
148
149
  - lib/log-analyzer/endpoint.rb
149
150
  - lib/log-analyzer/log.rb
150
151
  - lib/log-analyzer/pattern.rb