Beautiful::Log(beta)
Make Rails log beautiful!
Colored log

- Thanks to fazibear/colorize, logged messages stand out according to their levels.
- Messages align beautifully so you can start reading custom message instantly.
Backtrace

Your backtrace will be neat and understandable with Beautiful::Log::Formatter.
- Only the file paths of your codes (app/..) are displayed and highlighted.
- The paths are no longer verbosely long, they are shrunk to relative path from your project root.
Status Code


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.
Pretty-printd object
Thanks to awesome-print/awesome_print, a complex object is beautifully displayed. You won't be annoyed with messy long string of object description.
- Hash

- ActiveRecord instance

Rescue error, just log it Rails.logger.error e .
Logs in Rake tasks
Rake tasks are fully logged with Beautiful::Log::TaskLogging just by adding some codes to Rakefile .
Installation
Add this line to your application's Gemfile:
gem 'beautiful-log'
# or
gem 'beautiful-log', git: '[email protected]:nogahighland/beautiful-log.git'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install beautiful-log
Usage
- config/application.rb
config.logger = Logger.new(config.paths["log"].first)
config.logger.formatter = Beautiful::Log::Formatter.new
- config/environments/development.rb
config.log_level = :debug # set the level you need
You can change log level from :debug to :fatal depending on staging level (develop/production/test).
- Rakefile
Rails.logger = Logger.new(STDOUT)
Rails.logger.formatter = Beautiful::Log::Formatter.new
Rails.logger.level = Logger::DEBUG # set the level you need
- Or just include in application.rb
module YourApplication
class Application < Rails::Application
# This is equivalent to code below:
# Rails.logger = Logger.new(config.paths['log'].first)
# Rails.logger.formatter = Beautiful::Log::Formatter.new
include Beautiful::Log
:
end
end
Configurations
You can customize displayed log by passing a hash to constructor of Beautiful::Log::Formatter.
Below is default value.
Beautiful::Log::Formatter.new(
only_project_code: true,
backtrace_ignore_paths: [],
highlighted_line_range: 3,
highlighted_line_styles: :cyan,
backtrace_styles: :light_red,
severity_styles: {}
status_code_styles: { (1..3) => [:green, :bold], 'other' => [:red, :bold] },
error_file_path_styles: { FATAL: [:red, :swap], ERROR: :red, WARN: :light_red }
)
Note
backtrace_ignore_pathsincludes bundle path if you use Bundler. The bundle path is a stringBundler.bundle_pathreturns, whch is written in.bundle/config.If you pass a hash as
status_code_stylesorseverity_styles, those styles are merged with default values shown above.
Style specification
For
*_styleskeys, 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.Pick your favorite color or style (called 'mode' in fazibear/colorize) below.
Requirements
- Ruby 2.3-
Contribution
- If you find any problematic behaviors, please make an issue with problem backtrace.
- If you want to make changes, fork this repository, then make a pull request.
TODOs
- [ ] Replace old images of pretty-printd object
- [ ] Specs
- [ ] Is is smarter to pass a proc/block to customize log style?