MojoLogger
This gem is a wrapper around log4j and is primarily meant to be used in the Mojo framework. It will standardize logs accross Mojo applications and provide a single location to edit the Mojo Logger code base. It comes with a few benefits:
- JSON Logs will be printed for nice splunk logging when the mojo_* methods are called.
- No need for a log4j.properties file (although you can use one if you prefer). You can configure the logger on the fly and this gem will build a StringIO object to mock the log4j.properties file
Installation
Add this line to your application's Gemfile:
gem 'mojo_logger'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mojo_logger
Usage
Simple access to a CONSOLE appender:
require 'mojo_logger'
MojoLogger.debug "This is a message"
#=> This is a message
Mojo Formatted JSON logs
require 'mojo_logger'
api_request = {
session_id: '111111-11111',
reference_id: 'client',
api: 'get-user-info'
}
MojoLogger.mojo_warn(api_request, "Oops", "Some Warn Message")
#=> {"time":"08-25-2015 19:22:59.940 +0000","app":"Mojo","session_id":"111111-11111","reference_id":"client","api":"get-user-info","category":"Oops","message":"Some Warn Message"}
Mojo Formatted JSON logs with cusom keys
require 'mojo_logger'
api_request = {
session_id: '111111-11111',
reference_id: 'client',
api: 'get-user-info'
}
MojoLogger.mojo_warn(api_request, "Oops", "Some Warn Message", {my_key: "Some value", my_other_key: :another_val})
#=> {"time":"08-25-2015 19:24:57.575 +0000","app":"Mojo","session_id":"111111-11111","reference_id":"client","api":"get-user-info","category":"Oops","message":"Some Warn Message","my_key":"Some value","my_other_key":"another_val"}
Configuration
CONSOLE appender with "WARN" log level
require 'mojo_logger'
MojoLogger.config do |config|
config.default_log_level = :warn
end
MojoLogger.debug "This won't show up"
#=> nil
MojoLogger.warn "This will show up"
#=> This will show up
Rolling File appender
This will create a rolling file logger rather than a console logger with the following settings: Max File Size = 10MB Max Backup Index = 10 Pattern = '%n %m %n' (I need to change this but this is what it is for now.)
require 'mojo_logger'
MojoLogger.config do |config|
config.log_file = '/var/log/my_log.log'
end
Add Custom lines to the generated log4j.properties 'file'
require 'mojo_logger'
MojoLogger.config do |config|
config.custom_line("log4j.logger.org.apache.zookeeper.ClientCnxn=INFO")
end
Add Custom adapter for format your logs
Add an adapter that will accept whatever fields you want to input and output a hash. This hash will be converted to json by MojoLogger.
require 'mojo_logger'
include MojoLogger
ENV['RACK_ENV'] = 'development'
# by default you use the standard adapter
mojo_debug({}, 'category', 'this is my message', { custom: :field })
# => {"time":"04-01-2016 22:25:33.019 +0000","app":"Mojo","env":"development","session_id":null,"reference_id":null,"api":null,"category":"category","message":"this is my message","custom":"field"}
class CustomLogAdapter
# an adapter must repond to #format() and return a Hash
def format(message)
{
'message' => message,
'constant' => 'Hello!'
}
end
end
MojoLogger.config do |config|
config.adapter = CustomLogAdapter.new
end
# now you can call the #mojo_* methods with the parameter you define
mojo_debug('my message')
# => {"time":"04-01-2016 22:26:43.441 +0000","app":"Mojo","env":"development","message":"my message","constant":"Hello!"}
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
- Fork it ( https://github.com/[my-github-username]/mojo_logger/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request