beautiful-log 0.1.2
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 +10 -0
- data/.rspec +2 -0
- data/.rubocop.yml +5 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +159 -0
- data/Rakefile +6 -0
- data/beautiful-log.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/beautiful/log.rb +15 -0
- data/lib/beautiful/log/formatter.rb +85 -0
- data/lib/beautiful/log/modules.rb +14 -0
- data/lib/beautiful/log/modules/code_range_extractable.rb +68 -0
- data/lib/beautiful/log/modules/complete_log_formatter.rb +30 -0
- data/lib/beautiful/log/modules/error_formattable.rb +45 -0
- data/lib/beautiful/log/modules/path_ommittable.rb +31 -0
- data/lib/beautiful/log/modules/render_log_formatter.rb +37 -0
- data/lib/beautiful/log/modules/stylable.rb +22 -0
- data/lib/beautiful/log/task_logging.rb +21 -0
- data/lib/beautiful/log/version.rb +6 -0
- metadata +139 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 1b27fc6230688732c1f9213c69b81aba3cc6fe68
|
|
4
|
+
data.tar.gz: fe67380cce8ab6d20670bdaf109a0d03d6536116
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1602dffff9e27c38c3d4ad183514eb67e178f451a57d6850670508e49a9d79fc48a62c09e5e135ad424eef5f9090bb76f413fb05d614cd5517c6b98433af3c3e
|
|
7
|
+
data.tar.gz: 3c2564328c4d401dac1b84e380550c209ac0ef1a9942136efc394d9d693b04111b853362eb74f80e9e84a5f9b1f5d9a5a6648e998c925f5f1d1101ebd0b96bfa
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.3.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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 noga.highland@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
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 nogahighland
|
|
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,159 @@
|
|
|
1
|
+
# Beautiful::Log(beta)
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Make **Rails** log beautiful!
|
|
6
|
+
|
|
7
|
+
## Colored log
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
- Thanks to [fazibear/colorize](https://github.com/fazibear/colorize), logged messages stand out according to their levels.
|
|
12
|
+
- Messages align beautifully so you can start reading custom message instantly.
|
|
13
|
+
|
|
14
|
+
## Backtrace
|
|
15
|
+
|
|
16
|
+
<img width="709" alt="_2016-12-10_2_54_42" src="https://cloud.githubusercontent.com/assets/1780339/21059065/286b1932-be84-11e6-9d2e-5587afaf86ef.png">
|
|
17
|
+
|
|
18
|
+
Your backtrace will be neat and understandable with `Beautiful::Log::Formatter`.
|
|
19
|
+
|
|
20
|
+
- Only the file paths of **your codes** (app/..) are displayed and highlighted.
|
|
21
|
+
- The paths are no longer verbosely long, they are shrunk to relative path from your project root.
|
|
22
|
+
|
|
23
|
+
## Status Code
|
|
24
|
+
|
|
25
|
+

|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
You don't miss the responses' safety any more. A log of response completion tells either your app responded correctly or not by intuitive colors. You can customize color according to status code range (hundread level e.g: `1..3`.
|
|
30
|
+
|
|
31
|
+
## Pretty-printd object
|
|
32
|
+
|
|
33
|
+
Thanks to [awesome-print/awesome_print](https://github.com/awesome-print/awesome_print), a complex object is beautifully displayed. You won't be annoyed with messy long string of object description.
|
|
34
|
+
|
|
35
|
+
- Hash
|
|
36
|
+
|
|
37
|
+
<img width="666" alt="_2016-12-12_0_53_25 2" src="https://cloud.githubusercontent.com/assets/1780339/21081243/a083793c-c005-11e6-8d7b-73308a1cc277.png">
|
|
38
|
+
|
|
39
|
+
- ActiveRecord instance
|
|
40
|
+
|
|
41
|
+
<img width="666" alt="_2016-12-12_0_53_25" src="https://cloud.githubusercontent.com/assets/1780339/21081244/a0bb1892-c005-11e6-81ef-dcf4c153400a.png">
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
_Rescue error, just log it `Rails.logger.error e` ._
|
|
45
|
+
|
|
46
|
+
## Logs in Rake tasks
|
|
47
|
+
|
|
48
|
+
Rake tasks are fully logged with `Beautiful::Log::TaskLogging` just by adding some codes to `Rakefile` .
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
Add this line to your application's Gemfile:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
gem 'beautiful-log'
|
|
56
|
+
# or
|
|
57
|
+
gem 'beautiful-log', git: 'git@github.com:nogahighland/beautiful-log.git'
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
And then execute:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
$ bundle install
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Or install it yourself as:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
$ gem install beautiful-log
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Usage
|
|
73
|
+
|
|
74
|
+
- config/application.rb
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
config.logger = Logger.new(config.paths["log"].first)
|
|
78
|
+
config.logger.formatter = Beautiful::Log::Formatter.new
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
- config/environments/development.rb
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
config.log_level = :debug # set the level you need
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
You can change log level from `:debug` to `:fatal` depending on staging level (develop/production/test).
|
|
88
|
+
|
|
89
|
+
- Rakefile
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
Rails.logger = Logger.new(STDOUT)
|
|
93
|
+
Rails.logger.formatter = Beautiful::Log::Formatter.new
|
|
94
|
+
Rails.logger.level = Logger::DEBUG # set the level you need
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- Or just include in application.rb
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
module YourApplication
|
|
101
|
+
class Application < Rails::Application
|
|
102
|
+
# This is equivalent to code below:
|
|
103
|
+
# Rails.logger = Logger.new(config.paths['log'].first)
|
|
104
|
+
# Rails.logger.formatter = Beautiful::Log::Formatter.new
|
|
105
|
+
include Beautiful::Log
|
|
106
|
+
:
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Configurations
|
|
112
|
+
|
|
113
|
+
You can customize displayed log by passing a hash to constructor of `Beautiful::Log::Formatter`.
|
|
114
|
+
|
|
115
|
+
Below is default value.
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
Beautiful::Log::Formatter.new(
|
|
119
|
+
only_project_code: true,
|
|
120
|
+
backtrace_ignore_paths: [],
|
|
121
|
+
highlighted_line_range: 3,
|
|
122
|
+
highlighted_line_styles: :cyan,
|
|
123
|
+
backtrace_styles: :light_red,
|
|
124
|
+
severity_styles: {}
|
|
125
|
+
status_code_styles: { (1..3) => [:green, :bold], 'other' => [:red, :bold] },
|
|
126
|
+
error_file_path_styles: { FATAL: [:red, :swap], ERROR: :red, WARN: :light_red }
|
|
127
|
+
)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Note
|
|
131
|
+
|
|
132
|
+
- `backtrace_ignore_paths` includes bundle path if you use [Bundler](http://bundler.io/). The bundle path is a string `Bundler.bundle_path` returns, whch is written in `.bundle/config` .
|
|
133
|
+
|
|
134
|
+
- If you pass a hash as `status_code_styles` or `severity_styles`, those styles are merged with default values shown above.
|
|
135
|
+
|
|
136
|
+
#### Style specification
|
|
137
|
+
|
|
138
|
+
- For `*_styles` keys, you can set a **Symbol** or an **Array of Symbol** to style the string (color, bold, underline, etc). The elements of the array are applied in order.
|
|
139
|
+
|
|
140
|
+
- Pick your favorite color or style (called 'mode' in [fazibear/colorize](https://github.com/fazibear/colorize/)) below.
|
|
141
|
+
- [color](https://github.com/fazibear/colorize/blob/master/lib/colorize/class_methods.rb#L61)
|
|
142
|
+
- [mode](https://github.com/fazibear/colorize/blob/master/lib/colorize/class_methods.rb#L78)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
## Requirements
|
|
146
|
+
|
|
147
|
+
- Ruby 2.3-
|
|
148
|
+
|
|
149
|
+
## Contribution
|
|
150
|
+
|
|
151
|
+
- If you find any problematic behaviors, please make an issue with problem backtrace.
|
|
152
|
+
- If you want to make changes, fork this repository, then make a pull request.
|
|
153
|
+
|
|
154
|
+
# TODOs
|
|
155
|
+
|
|
156
|
+
- [ ] Replace old images of pretty-printd object
|
|
157
|
+
- [ ] Specs
|
|
158
|
+
- [ ] Is is smarter to pass a proc/block to customize log style?
|
|
159
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'beautiful/log/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'beautiful-log'
|
|
8
|
+
spec.version = Beautiful::Log::VERSION
|
|
9
|
+
spec.authors = ['nogahighland']
|
|
10
|
+
spec.email = ['noga.highland@gmail.com']
|
|
11
|
+
|
|
12
|
+
spec.summary = ''
|
|
13
|
+
spec.description = 'beautiful-log provides a delightful means of displaying useful and beautiful log in Ruby on Rails application.'
|
|
14
|
+
spec.homepage = 'https://github.com/nogahighland/beautiful-log/'
|
|
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.12'
|
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
25
|
+
spec.add_dependency 'colorize', '~> 0.8.1'
|
|
26
|
+
spec.add_dependency 'awesome_print', '~> 1.7.0'
|
|
27
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
require 'beautiful/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,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'beautiful/log/version'
|
|
3
|
+
require 'beautiful/log/formatter'
|
|
4
|
+
require 'beautiful/log/task_logging'
|
|
5
|
+
|
|
6
|
+
module Beautiful
|
|
7
|
+
module Log
|
|
8
|
+
def self.included(klass)
|
|
9
|
+
return unless defined?(Rails) && klass < Rails::Application
|
|
10
|
+
config = klass.config
|
|
11
|
+
Rails.logger = Logger.new(config.paths['log'].first)
|
|
12
|
+
Rails.logger.formatter = Beautiful::Log::Formatter.new
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'pp'
|
|
3
|
+
require 'beautiful/log/modules'
|
|
4
|
+
require "awesome_print"
|
|
5
|
+
|
|
6
|
+
module Beautiful
|
|
7
|
+
module Log
|
|
8
|
+
class Formatter < ::Logger::Formatter
|
|
9
|
+
attr_reader :only_project_code, :ignore_paths, :allow_path, :backtrace_ignore_paths,
|
|
10
|
+
:highlighted_line_range, :highlighted_line_styles, :backtrace_styles,
|
|
11
|
+
:error_file_path_styles, :status_code_styles, :severity_styles
|
|
12
|
+
|
|
13
|
+
cattr_accessor(:datetime_format) { '%Y-%m-%d %H:%m:%S' }
|
|
14
|
+
|
|
15
|
+
include Modules::CodeRangeExtractable, Modules::PathOmmittable,
|
|
16
|
+
Modules::ErrorFormattable, Modules::RenderLogFoematter,
|
|
17
|
+
Modules::CompleteLogFormatter, Modules::Stylable
|
|
18
|
+
|
|
19
|
+
DEFAULT_STATUS_CODE_STYLES = { (1..3) => [:green, :bold], 'other' => [:red, :bold] }.freeze
|
|
20
|
+
DEFAULT_SEVERITY_STYLES = { FATAL: [:red, :swap], ERROR: :red, WARN: :light_red }.freeze
|
|
21
|
+
|
|
22
|
+
# rubocop: disable Metrics/AbcSize, Style/ParameterLists, Style/MethodLength
|
|
23
|
+
def initialize(
|
|
24
|
+
only_project_code: true,
|
|
25
|
+
backtrace_ignore_paths: [],
|
|
26
|
+
highlighted_line_range: 3,
|
|
27
|
+
highlighted_line_styles: :cyan,
|
|
28
|
+
backtrace_styles: :light_red,
|
|
29
|
+
error_file_path_styles: :red,
|
|
30
|
+
status_code_styles: {},
|
|
31
|
+
severity_styles: {}
|
|
32
|
+
)
|
|
33
|
+
@only_project_code = only_project_code
|
|
34
|
+
@ignore_paths = backtrace_ignore_paths.map do |path|
|
|
35
|
+
Regexp.new "#{Rails.root}/#{path}"
|
|
36
|
+
end << Regexp.new(bundle_path)
|
|
37
|
+
@allow_path = Regexp.new bundle_install_path
|
|
38
|
+
@backtrace_ignore_paths = backtrace_ignore_paths
|
|
39
|
+
@highlighted_line_range = highlighted_line_range
|
|
40
|
+
@highlighted_line_styles = highlighted_line_styles
|
|
41
|
+
@backtrace_styles = backtrace_styles
|
|
42
|
+
@error_file_path_styles = error_file_path_styles
|
|
43
|
+
@status_code_styles = DEFAULT_STATUS_CODE_STYLES.merge(status_code_styles).with_indifferent_access
|
|
44
|
+
@severity_styles = DEFAULT_SEVERITY_STYLES.merge(severity_styles).with_indifferent_access
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def call(severity, timestamp, _progname, message)
|
|
48
|
+
problem_code = highlighted_code(message) if message.is_a?(Exception)
|
|
49
|
+
header = message_header(timestamp, severity)
|
|
50
|
+
message = "#{header} -- : #{message_body(message, header.uncolorize.length + 6)}\n"
|
|
51
|
+
message = "#{message}\n#{problem_code}" if problem_code.present?
|
|
52
|
+
message = "\n#{message}\n" if %w(FATAL ERROR).include?(severity)
|
|
53
|
+
message
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def message_header(timestamp, severity)
|
|
59
|
+
header = "[#{timestamp.strftime(datetime_format)}] (pida=#{$PROCESS_ID}) #{format('%5s', severity)}"
|
|
60
|
+
colored_header(severity, header)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def colored_header(severity, header)
|
|
64
|
+
styles = severity_styles[severity.to_sym]
|
|
65
|
+
return header if styles.blank?
|
|
66
|
+
apply_styles(header, styles)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def message_body(message, header_length)
|
|
70
|
+
return format_exception(message) if message.is_a?(Exception)
|
|
71
|
+
return format_render_log(message) if render_log?(message)
|
|
72
|
+
return format_complete_log(message) if complete_log?(message)
|
|
73
|
+
return message if message.is_a?(String)
|
|
74
|
+
message.ai.gsub(/\n/, "\n" + ' ' * header_length)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def highlighted_code(error)
|
|
78
|
+
problem_point = error.backtrace.find { |backtrace_line| !ignore_path?(backtrace_line) }
|
|
79
|
+
return unless problem_point.present?
|
|
80
|
+
file_path = problem_point.split(':')[0]
|
|
81
|
+
"\t#{file_path.cyan}\n#{numbered_code_lines(problem_point)}\n"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'beautiful/log/modules/code_range_extractable'
|
|
3
|
+
require 'beautiful/log/modules/path_ommittable'
|
|
4
|
+
require 'beautiful/log/modules/error_formattable'
|
|
5
|
+
require 'beautiful/log/modules/render_log_formatter'
|
|
6
|
+
require 'beautiful/log/modules/complete_log_formatter'
|
|
7
|
+
require 'beautiful/log/modules/stylable'
|
|
8
|
+
|
|
9
|
+
module Beautiful
|
|
10
|
+
module Log
|
|
11
|
+
module Modules
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'colorize'
|
|
3
|
+
require 'beautiful/log/modules/stylable'
|
|
4
|
+
|
|
5
|
+
module Beautiful
|
|
6
|
+
module Log
|
|
7
|
+
module Modules
|
|
8
|
+
module CodeRangeExtractable
|
|
9
|
+
include Stylable
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def numbered_code_lines(backtrace_line)
|
|
14
|
+
file_path, line_number = backtrace_line.split(':')[0..1]
|
|
15
|
+
min_index, max_index = extracted_range(line_number.to_i)
|
|
16
|
+
code_lines = code_lines(file_path, min_index, max_index)
|
|
17
|
+
prepend_number_to_code_lines(code_lines, line_number.to_i, min_index + 1)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def extracted_range(line_number)
|
|
21
|
+
min_index = line_number - 1 - highlighted_line_range
|
|
22
|
+
min_index = 0 if min_index.negative?
|
|
23
|
+
max_index = line_number - 1 + highlighted_line_range
|
|
24
|
+
[min_index, max_index]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def code_lines(file_path, min_index, max_index)
|
|
28
|
+
code_lines = []
|
|
29
|
+
open(file_path) do |f|
|
|
30
|
+
file_content = f.read
|
|
31
|
+
code_lines = file_content.split("\n")[min_index..max_index]
|
|
32
|
+
end
|
|
33
|
+
code_lines
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def prepend_number_to_code_lines(code_lines, line_number, min_line_number)
|
|
37
|
+
line_numbers = line_numbers(min_line_number, code_lines.length)
|
|
38
|
+
line_code_pairs = [line_numbers, code_lines].transpose
|
|
39
|
+
max_length = line_number_max_length(line_numbers)
|
|
40
|
+
line_code_pairs.map do |line_code_pair|
|
|
41
|
+
number, code = line_code_pair
|
|
42
|
+
code = number == line_number ? emphasize(code) : code
|
|
43
|
+
number = format_line_number(number, max_length)
|
|
44
|
+
apply_styles("\t #{number}: #{code}", highlighted_line_styles)
|
|
45
|
+
end.join("\n")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def emphasize(code)
|
|
49
|
+
match = code.match(/\A(\s*)(\S.+)\z/)
|
|
50
|
+
return code unless match
|
|
51
|
+
match[1] + match[2].bold
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def line_number_max_length(line_numbers)
|
|
55
|
+
Math.log10(line_numbers.max).floor.to_i + 1
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def format_line_number(number, length)
|
|
59
|
+
format("%#{length}d", number)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def line_numbers(min_line_number, code_line_length)
|
|
63
|
+
(min_line_number..min_line_number + code_line_length - 1).to_a
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'beautiful/log/modules/stylable'
|
|
3
|
+
|
|
4
|
+
module Beautiful
|
|
5
|
+
module Log
|
|
6
|
+
module Modules
|
|
7
|
+
module CompleteLogFormatter
|
|
8
|
+
include Stylable
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
COMPLETE_LOG_REGEXP = /\ACompleted (\d{3})/
|
|
13
|
+
|
|
14
|
+
def complete_log?(log)
|
|
15
|
+
return false unless log.is_a?(String)
|
|
16
|
+
log =~ COMPLETE_LOG_REGEXP
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def format_complete_log(log)
|
|
20
|
+
status_code_hundread = log.match(COMPLETE_LOG_REGEXP)[1][0].to_i
|
|
21
|
+
_status_range, color = status_code_styles.find do |range, _color|
|
|
22
|
+
next range.include?(status_code_hundread) if range.is_a?(Range)
|
|
23
|
+
range == status_code_hundread if range.is_a?(Integer)
|
|
24
|
+
end
|
|
25
|
+
apply_styles(log, color || status_code_styles[:other]) + "\n"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'colorize'
|
|
3
|
+
require 'beautiful/log/modules/stylable'
|
|
4
|
+
|
|
5
|
+
module Beautiful
|
|
6
|
+
module Log
|
|
7
|
+
module Modules
|
|
8
|
+
module ErrorFormattable
|
|
9
|
+
include Stylable
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def format_exception(e)
|
|
14
|
+
error_message = e.to_s
|
|
15
|
+
error_message = apply_styles(error_message, backtrace_styles)
|
|
16
|
+
"#{error_message}\n#{format_backtrace(e)}"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def format_backtrace(e)
|
|
20
|
+
lines = e.backtrace
|
|
21
|
+
lines = lines.select do |line|
|
|
22
|
+
(only_project_code && project_code?(line)) && !ignore_path?(line)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
format_lines = lines.map do |line|
|
|
26
|
+
line = highlighted_line(line) if project_code?(line)
|
|
27
|
+
"\t#{line}"
|
|
28
|
+
end
|
|
29
|
+
format_lines.join("\n")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def highlighted_line(line)
|
|
33
|
+
line = omit_project_path(line)
|
|
34
|
+
file_path, line_number, method = line.split(':')
|
|
35
|
+
levels = file_path.split('/')
|
|
36
|
+
|
|
37
|
+
file_path = (levels[0..-2] << levels[-1].bold).join('/')
|
|
38
|
+
line = [file_path, line_number, method].join(':')
|
|
39
|
+
|
|
40
|
+
apply_styles(line, backtrace_styles)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module Beautiful
|
|
3
|
+
module Log
|
|
4
|
+
module Modules
|
|
5
|
+
module PathOmmittable
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def omit_project_path(line)
|
|
9
|
+
line.gsub(Rails.root.to_s + '/', '')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def project_code?(backtrace_line)
|
|
13
|
+
backtrace_line.index(Rails.root.to_s)&.zero?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def ignore_path?(backtrace_line)
|
|
17
|
+
return false if backtrace_line =~ allow_path
|
|
18
|
+
ignore_paths.find { |path| backtrace_line =~ path } unless ignore_paths.empty?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def bundle_install_path
|
|
22
|
+
Bundler.install_path.to_s if defined?(Bundler)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def bundle_path
|
|
26
|
+
Bundler.bundle_path.to_s if defined?(Bundler)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'beautiful/log/modules/stylable'
|
|
3
|
+
|
|
4
|
+
module Beautiful
|
|
5
|
+
module Log
|
|
6
|
+
module Modules
|
|
7
|
+
module RenderLogFoematter
|
|
8
|
+
include Stylable
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def format_render_log(backtrace_line)
|
|
13
|
+
formatted_log = omit_project_path(backtrace_line)
|
|
14
|
+
if project_render_view?(backtrace_line)
|
|
15
|
+
apply_styles(formatted_log, backtrace_color)
|
|
16
|
+
else
|
|
17
|
+
formatted_log
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def project_render_view?(backtrace_line)
|
|
22
|
+
view_path = view_path(backtrace_line)
|
|
23
|
+
view_path.strip.start_with?('app/view')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def render_log?(backtrace_line)
|
|
27
|
+
return false unless backtrace_line.is_a?(String)
|
|
28
|
+
backtrace_line.strip.start_with?('Rendered')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def view_path(backtrace_line)
|
|
32
|
+
backtrace_line.gsub('Rendered ', '')
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'colorize'
|
|
3
|
+
|
|
4
|
+
module Beautiful
|
|
5
|
+
module Log
|
|
6
|
+
module Modules
|
|
7
|
+
module Stylable
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def apply_styles(string, styles)
|
|
11
|
+
styles = [styles] if styles.is_a?(Symbol)
|
|
12
|
+
styles.each do |style|
|
|
13
|
+
style = style.to_sym if style.is_a?(String)
|
|
14
|
+
next unless ''.respond_to?(style)
|
|
15
|
+
string = string.send(style)
|
|
16
|
+
end
|
|
17
|
+
string
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module Beautiful
|
|
3
|
+
module Log
|
|
4
|
+
module TaskLogging
|
|
5
|
+
def task(*args)
|
|
6
|
+
Rake::Task.define_task(*args) do |task, task_args|
|
|
7
|
+
if block_given?
|
|
8
|
+
Rails.logger&.debug "[#{task.name} #{task.comment}] started"
|
|
9
|
+
begin
|
|
10
|
+
yield(task, task_args)
|
|
11
|
+
Rails.logger&.debug "[#{task.name} #{task.comment}] finished"
|
|
12
|
+
rescue => e
|
|
13
|
+
Rails.logger&.error e
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
extend Beautiful::Log::TaskLogging
|
metadata
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: beautiful-log
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- nogahighland
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-12-12 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.12'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.12'
|
|
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
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: colorize
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 0.8.1
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 0.8.1
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: awesome_print
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 1.7.0
|
|
76
|
+
type: :runtime
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 1.7.0
|
|
83
|
+
description: beautiful-log provides a delightful means of displaying useful and beautiful
|
|
84
|
+
log in Ruby on Rails application.
|
|
85
|
+
email:
|
|
86
|
+
- noga.highland@gmail.com
|
|
87
|
+
executables: []
|
|
88
|
+
extensions: []
|
|
89
|
+
extra_rdoc_files: []
|
|
90
|
+
files:
|
|
91
|
+
- ".gitignore"
|
|
92
|
+
- ".rspec"
|
|
93
|
+
- ".rubocop.yml"
|
|
94
|
+
- ".ruby-version"
|
|
95
|
+
- ".travis.yml"
|
|
96
|
+
- CODE_OF_CONDUCT.md
|
|
97
|
+
- Gemfile
|
|
98
|
+
- LICENSE.txt
|
|
99
|
+
- README.md
|
|
100
|
+
- Rakefile
|
|
101
|
+
- beautiful-log.gemspec
|
|
102
|
+
- bin/console
|
|
103
|
+
- bin/setup
|
|
104
|
+
- lib/beautiful/log.rb
|
|
105
|
+
- lib/beautiful/log/formatter.rb
|
|
106
|
+
- lib/beautiful/log/modules.rb
|
|
107
|
+
- lib/beautiful/log/modules/code_range_extractable.rb
|
|
108
|
+
- lib/beautiful/log/modules/complete_log_formatter.rb
|
|
109
|
+
- lib/beautiful/log/modules/error_formattable.rb
|
|
110
|
+
- lib/beautiful/log/modules/path_ommittable.rb
|
|
111
|
+
- lib/beautiful/log/modules/render_log_formatter.rb
|
|
112
|
+
- lib/beautiful/log/modules/stylable.rb
|
|
113
|
+
- lib/beautiful/log/task_logging.rb
|
|
114
|
+
- lib/beautiful/log/version.rb
|
|
115
|
+
homepage: https://github.com/nogahighland/beautiful-log/
|
|
116
|
+
licenses:
|
|
117
|
+
- MIT
|
|
118
|
+
metadata: {}
|
|
119
|
+
post_install_message:
|
|
120
|
+
rdoc_options: []
|
|
121
|
+
require_paths:
|
|
122
|
+
- lib
|
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
|
+
requirements:
|
|
125
|
+
- - ">="
|
|
126
|
+
- !ruby/object:Gem::Version
|
|
127
|
+
version: '0'
|
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
|
+
requirements:
|
|
130
|
+
- - ">="
|
|
131
|
+
- !ruby/object:Gem::Version
|
|
132
|
+
version: '0'
|
|
133
|
+
requirements: []
|
|
134
|
+
rubyforge_project:
|
|
135
|
+
rubygems_version: 2.5.1
|
|
136
|
+
signing_key:
|
|
137
|
+
specification_version: 4
|
|
138
|
+
summary: ''
|
|
139
|
+
test_files: []
|