0.0
No commit activity in last 3 years
No release in over 3 years
This gem is a wrapper around log4j that allow you to construct a log4j.properties file from a config block, rather than actually having a log4j.properties file. Useful for setting the log levels dynamically.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.8
>= 0
~> 10.0
>= 0
 Project Readme

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

  1. Fork it ( https://github.com/[my-github-username]/mojo_logger/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request