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
awesome-print/awesome_print supports more types to beautiflize.
All you need to do is 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: 'git@github.com: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 the 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 the log appearance by passing a hash to constructor of Beautiful::Log::Formatter
.
Below is a hash containing default values.
Beautiful::Log::Formatter.new(
only_project_code: true,
shrink_bundle_path: true,
backtrace_ignore_paths: [],
highlighted_line_range: 3,
highlighted_line_styles: :cyan,
backtrace_styles: :light_red,
error_file_path_styles: :red,
severity_styles: { FATAL: [:red, :swap], ERROR: :red, WARN: :light_red },
status_code_styles: { (1..3) => [:green, :bold], 'other' => [:red, :bold] },
occurence_line: :light_blue
)
Note
-
backtrace_ignore_paths
includes bundle path if you use Bundler. The bundle path is a stringBundler.bundle_path
returns, which is written in.bundle/config
. -
If you pass a hash as
status_code_styles
orseverity_styles
, those styles are merged with default values shown above.
Style specification
-
For
*_styles
keys, you can set a Symbol or an Array of Symbol to style a 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
- Specs
- Is is smarter to pass a proc/block to customize log style?