h1. Logging assist
A wrapper of "Log4r-color":https://github.com/kristianmandrup/log4r-color to make it easy to add logging to your projects.
This project was originally born from a need to make it easy to debug Rails 3 Generators. It is used extensively in the "Cream":https://github.com/kristianmandrup/cream framework
h2. Install
gem install logging_assist
h2. Usage
The default logger logs to a ColorOutputter with a default color scheme for each log level.
h3. Setup
Inside any class you want to add logging
class MyClassToBeLogged
include RailsAssist::BasicLogger
...
end
h3. Logging
You can use the methods #debug, #info, #warn, #error and #fatal
def my_method
logger.debug 'wtf??'
logger.info "now inside #my_method"
end
h3. Add outputters
You can easily add one or more file outputters to the mix
class MyClassToBeLogged
def initialize
# setup logging file
logger.add_logfile :logfile => 'here.log'
end
end
h4. Custom outputters
For custom outputters (any supported by log4r) you can use logger.add_outputter(my_custom_outputter)
Here my_custom_outputter must be an instance of a subclass of Log4r::Outputter
You can remove the outputter like this logger.remove_outputter(my_custom_outputter)
h4. Advanced outputter configuration
Every logger also takes a :formatter and :level option. Use like this:
def initialize
my_custom_formatter = PatternFormatter.new(:pattern => "[%l] %d :: %.40m")
logger.add_logfile :logfile => 'here.log', :formatter => my_custom_formatter, :level => :info
end
See "PatternFormatter docs":http://log4r.sourceforge.net/rdoc/files/log4r/formatter/patternformatter_rb.html for more info on customizing the output formats
Currently the formatter and level of an outputter can only (easily) be set on creation when added.
h3. Remove outputters
You can remove color output to the console like this:
class MyClassToBeLogged
def initialize
# setup logging file
logger.remove_outputs :color
end
end
Remove file outputs logger.remove_outputs :file
Here The following convention: Log4r::[name]Outputter is used to find the types of Outputter to remove.
h3. Define custom color schemes
A color scheme can be set for any log level like this
obj.logger.set_color :info, {:color => :green, :background => :white}
Please see "colorize":https://github.com/fazibear/colorize for more info. Use String.colors
to see list of available colors.
h2. Note on Patches/Pull Requests
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a
future version unintentionally.
- Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
h2. Copyright
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.