bullet_log_parser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d44e398a26b680c801f106de1efba1b73eeb854aedfd204712eb05a8a7fc2770
4
+ data.tar.gz: 04e5cb8c0c54f5274a415883b34868f8e806978ffd2bcc2b9ca11a00bb241c36
5
+ SHA512:
6
+ metadata.gz: 2d452a2b730d1ea0d0a3963ee05a47e438ae9c83392f4bbb9f1a3622d57c3d176866a58f59b8ec5b6974362501f1ec512b3c290edb68e02861a30b122a723d03
7
+ data.tar.gz: 504f4a137bcf5aba63bb1d6876999390d9a8ad9ede26e8547081013edf9e8916eaae47f50e682397a2f686ae1becf30f514041f90acd734c2f60f53961191906
@@ -0,0 +1,6 @@
1
+ # 1.0.0 (2020-07-31)
2
+
3
+
4
+ ### Features
5
+
6
+ * first release ([5e28e39](https://github.com/satoruk/bullet_log_parser/commit/5e28e39dba70218f4e93de904f53d7018eed4472))
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 satoruk <koyanagi3106@gmail.com>
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.
@@ -0,0 +1,114 @@
1
+ <h1 align="center">bullet log parser</h2>
2
+
3
+ <p align="center">
4
+ <a href="https://github.com/satoruk/bullet_log_parser/actions?query=workflow%3ACI"><img src="https://github.com/satoruk/bullet_log_parser/workflows/CI/badge.svg" /></a>
5
+ </p>
6
+
7
+ This parser can help to convert the Bullet logs to prefer format you want.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ bundle add bullet_log_parser --group "development, test"
13
+ # or
14
+ gem install bullet_log_parser
15
+ ```
16
+
17
+ ## Example
18
+
19
+ ```sh
20
+ cat log/bullet.log | bullet_log_filter.rb
21
+ ```
22
+
23
+ `bullet_log_filter.rb`
24
+
25
+ ```rb
26
+ #!/usr/bin/env ruby
27
+ # frozen_string_literal: true
28
+
29
+ require 'bullet_log_parser'
30
+
31
+ if __FILE__ == $PROGRAM_NAME
32
+ results = BulletLogParser.uniq_log($stdin) do |ast|
33
+ # skip no call stack
34
+ next if ast[:stack].empty?
35
+
36
+ stack = ast[:stack].last
37
+ puts "#{stack[:filename]}:#{stack[:lineno]}:#{ast[:level]}"
38
+ ast[:details].each do |detail|
39
+ puts " #{detail}"
40
+ end
41
+ puts ''
42
+ end
43
+ puts '-- summary ------'
44
+ puts results
45
+ .keys
46
+ .map { |k| "#{k}:#{results[k].size}" }
47
+ .join("\n")
48
+ end
49
+
50
+ ```
51
+
52
+ ## AST format
53
+
54
+ <details>
55
+ <summary>ast format example</summary>
56
+
57
+ ```txt
58
+ 2020-07-27 02:35:50[WARN] user: root
59
+ GET /posts
60
+ USE eager loading detected
61
+ Post => [:comments]
62
+ Add to your query: .includes([:comments])
63
+ Call stack
64
+ /app/app/views/posts/_post.json.jbuilder:4:in `block in _app_views_posts__post_json_jbuilder__2280256895687227436_47114295576380'
65
+ /app/app/views/posts/_post.json.jbuilder:3:in `_app_views_posts__post_json_jbuilder__2280256895687227436_47114295576380'
66
+ /app/app/views/posts/index.json.jbuilder:1:in `_app_views_posts_index_json_jbuilder__4177424529561133656_47114295515920'
67
+ /app/app/controllers/posts_controller.rb:13:in `index'
68
+ /app/spec/requests/posts_spec.rb:45:in `block (4 levels) in <main>'
69
+ ```
70
+
71
+ ```json
72
+ {
73
+ "detectedAt": "2020-07-27 02:35:50",
74
+ "level": "WARN",
75
+ "user": "root",
76
+ "request": "GET /posts",
77
+ "detection": "USE eager loading detected",
78
+ "details": [
79
+ "Post => [:comments]",
80
+ "Add to your query: .includes([:comments])"
81
+ ],
82
+ "stack": [
83
+ {
84
+ "filename": "/app/app/views/posts/_post.json.jbuilder",
85
+ "lineno": 4,
86
+ "message": "in `block in _app_views_posts__post_json_jbuilder__2280256895687227436_47114295576380'"
87
+ },
88
+ {
89
+ "filename": "/app/app/views/posts/_post.json.jbuilder",
90
+ "lineno": 3,
91
+ "message": "in `_app_views_posts__post_json_jbuilder__2280256895687227436_47114295576380'"
92
+ },
93
+ {
94
+ "filename": "/app/app/views/posts/index.json.jbuilder",
95
+ "lineno": 1,
96
+ "message": "in `_app_views_posts_index_json_jbuilder__4177424529561133656_47114295515920'"
97
+ },
98
+ {
99
+ "filename": "/app/app/controllers/posts_controller.rb",
100
+ "lineno": 13,
101
+ "message": "in `index'"
102
+ },
103
+ {
104
+ "filename": "/app/spec/requests/posts_spec.rb",
105
+ "lineno": 45,
106
+ "message": "in `block (4 levels) in <main>'"
107
+ }
108
+ ]
109
+ }
110
+ ```
111
+
112
+ </details>
113
+
114
+ [ci badge]: https://github.com/satoruk/bullet_log_parser/workflows/CI/badge.svg
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/bullet_log_parser/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'bullet_log_parser'
7
+ spec.version = BulletLogParser::VERSION
8
+ spec.authors = ['satoruk']
9
+ spec.email = ['koyanagi3106@gmail.com']
10
+
11
+ spec.summary = 'Bullet log perser.'
12
+ spec.description = 'Bullet log perser.'
13
+ spec.homepage = 'https://github.com/satoruk/bullet_log_parser/'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = spec.homepage
20
+ spec.metadata['changelog_uri'] = spec.homepage
21
+
22
+ spec.files = %w[CHANGELOG.md LICENSE.txt README.md bullet_log_parser.gemspec]
23
+ spec.files += Dir.glob('lib/**/*.rb')
24
+
25
+ spec.require_paths = ['lib']
26
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bullet_log_parser/version'
4
+ require 'bullet_log_parser/parser'
5
+
6
+ module BulletLogParser # :nodoc:
7
+ def self.generate_bare_ast(ast)
8
+ bare_ast = ast.reject { |k, _| %i[detectedAt request].include?(k) }
9
+ bare_ast[:stack] = bare_ast[:stack].map { |stack| stack.reject { |k, _| k == :message } }
10
+ bare_ast
11
+ end
12
+
13
+ def self.uniq_log(io)
14
+ memo = Hash.new { |h, k| h[k] = [] }
15
+ parse(io) do |ast|
16
+ bare_ast = generate_bare_ast(ast)
17
+ detection = memo[bare_ast[:detection]]
18
+ next if detection.include?(bare_ast)
19
+
20
+ detection << bare_ast
21
+ yield ast
22
+ end
23
+ memo
24
+ end
25
+
26
+ def self.parse(io)
27
+ parser = Parser.new
28
+ loop do
29
+ str = io.gets
30
+ break unless str
31
+
32
+ parser.puts(str.chomp)
33
+ next unless parser.terminated?
34
+
35
+ yield parser.ast unless parser.failed?
36
+
37
+ parser = Parser.new
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BulletLogParser # :nodoc:
4
+ # bullert log persing class
5
+ class Parser
6
+ REGEX_CALL_STACK = /\A (.+):(\d+):(.+)\z/.freeze
7
+ REGEX_DETAIL = /\A (.+)\z/.freeze
8
+ REGEX_LINE1 = /\A(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\[(\w+)\] user: (.*)\z/.freeze
9
+
10
+ def initialize
11
+ @parse_method = :parse_line1
12
+ @ast = {}
13
+ @terminated = false
14
+ @failed = false
15
+ end
16
+
17
+ attr_reader :ast
18
+
19
+ def failed
20
+ @failed = true
21
+ end
22
+
23
+ def failed?
24
+ @failed
25
+ end
26
+
27
+ def terminated
28
+ @terminated = true
29
+ end
30
+
31
+ def terminated?
32
+ @terminated || @failed
33
+ end
34
+
35
+ def puts(str)
36
+ perse_proc = method(@parse_method).curry
37
+ perse_proc.call(str)
38
+ end
39
+
40
+ private
41
+
42
+ def parse_line1(str)
43
+ matched = REGEX_LINE1.match(str)
44
+ return failed unless matched
45
+
46
+ @parse_method = :parse_line2
47
+ @ast.update({
48
+ detectedAt: matched[1],
49
+ level: matched[2],
50
+ user: matched[3]
51
+ })
52
+ end
53
+
54
+ def parse_line2(str)
55
+ @parse_method = :parse_line_detector
56
+ return if str.empty?
57
+
58
+ @ast.update({ request: str })
59
+ end
60
+
61
+ def parse_line_detector(str)
62
+ @parse_method = :parse_line_detector_detail
63
+ return failed if str.empty?
64
+
65
+ @ast.update({
66
+ detection: str,
67
+ details: [],
68
+ stack: []
69
+ })
70
+ end
71
+
72
+ def parse_line_detector_detail(str)
73
+ if str == 'Call stack'
74
+ @parse_method = :parse_line_call_stack_detail
75
+ return
76
+ end
77
+
78
+ return terminated if str.empty?
79
+
80
+ matched = REGEX_DETAIL.match(str)
81
+ return failed unless matched
82
+
83
+ @ast[:details] << matched[1]
84
+ end
85
+
86
+ def parse_line_call_stack_detail(str)
87
+ return terminated if str.empty?
88
+
89
+ matched = REGEX_CALL_STACK.match(str)
90
+ return failed unless matched
91
+
92
+ @ast[:stack] << {
93
+ filename: matched[1],
94
+ lineno: matched[2].to_i,
95
+ message: matched[3]
96
+ }
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BulletLogParser
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bullet_log_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - satoruk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Bullet log perser.
14
+ email:
15
+ - koyanagi3106@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - bullet_log_parser.gemspec
24
+ - lib/bullet_log_parser.rb
25
+ - lib/bullet_log_parser/parser.rb
26
+ - lib/bullet_log_parser/version.rb
27
+ homepage: https://github.com/satoruk/bullet_log_parser/
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ allowed_push_host: https://rubygems.org
32
+ homepage_uri: https://github.com/satoruk/bullet_log_parser/
33
+ source_code_uri: https://github.com/satoruk/bullet_log_parser/
34
+ changelog_uri: https://github.com/satoruk/bullet_log_parser/
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.5.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.0.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Bullet log perser.
54
+ test_files: []