Log4r is a Ruby gem that features a heirachical logging system of any number of levels, custom level names, multiple output destinations per log event, custom formatting, and more. The Log4r gem is open sourced on GitHub, and with comphrehensive documentations available on Rubyforge.
I have been using log4r at work in most of the applications and systems written in Ruby. It’s easy to use, and provide the features that an application would need:
- Support multiple message levels such as Fatal, Error, Warn and Msg.
- Customizable log file format
- Multiple output destinations, like print out on the screen, and store in a log file
Here in this post, I’ll show you how to use log4r in your Ruby project.
Step 1. Install Gem log4r
1
|
|
Step 2: Include log4r in your project
One thing to note is the log4r supports configurations through YAML file, and you can define the configuration file when including the log4r.
1 2 3 4 5 6 7 |
|
Step 3: Configure log4r with a config file
You can define the logging levels, logger names, output destinations, etc. The following code shows an example of the configurations that I use.
File name: log4r.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
Step 4: Initialize a logger
You need initialize a logger in your project first.
1
|
|
Step 5: Use your logger
1 2 3 4 |
|