0.03
No commit activity in last 3 years
No release in over 3 years
log formatter for json. For Plain Ruby Logger, and for log4r and so on, details to read the REAME.md file.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 1.1
~> 10.0
~> 3.0
 Project Readme

LogFormatter

Gem Version

Log Formatter for Ruby and other logger.

Details as following:

  • log_formatter/ruby_json_formatter' : ruby logger json formatter
  • log_formatter/log4r_json_formatter' : log4r logger json formatter

Installation

Add this line to your application's Gemfile:

gem 'log_formatter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install log_formatter

Usage

Ruby Logger Json Formatter

quick start

require 'log_formatter'
require 'log_formatter/ruby_json_formatter'

logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG

logger.formatter = Ruby::JSONFormatter::Base.new

logger.info 'test data'

result:

{
  "message": "test data",
  "log_level": "INFO",
  "log_type": null,
  "log_app": null,
  "log_timestamp": "2016-08-25T15:34:25+08:00"
}

set app and common ext info

logger.formatter =  Ruby::JSONFormatter::Base.new('app', {'source': 'examples'})
logger.info 'test data'

result:

{
  "source": "examples",
  "message": "test data",
  "log_level": "INFO",
  "log_type": null,
  "log_app": "app",
  "log_timestamp": "2016-08-25T15:34:25+08:00"
}

log with hash

logger.formatter =  Ruby::JSONFormatter::Base.new('app', {'source': 'examples'})

logger.debug({data: "test data", author: 'chad'})

result:

{
  "source": "examples",
  "data": "test data",
  "author": "chad",
  "log_level": "DEBUG",
  "log_type": null,
  "log_app": "app",
  "log_timestamp": "2016-08-25T15:34:25+08:00"
}

reset the defaut key

json formatter will add log_type,log_level,log_timestamplog_app as default key, but you can change them if needed.

logger.formatter =  Ruby::JSONFormatter::Base.new('app', {'source': 'examples'}) do |config|
  config[:level] = :cus_level
  config[:type] = :cus_type
  config[:app] = :cus_app
  config[:timestamp] = :cus_timestamp
end

logger.debug({data: "test data", age: 18})

result:

{
  "source": "examples",
  "data": "test data",
  age: 18,
  "cus_level": "DEBUG",
  "cus_type": null,
  "cus_app": "app",
  "cus_timestamp": "2016-08-25T15:34:25+08:00"
}

disable auto generate keys

json formatter will add log_type,log_level,log_timestamplog_app as default key, but you can remove them if needed by setted to false.

logger.formatter =  Ruby::JSONFormatter::Base.new('app', {'source': 'examples'}) do |config|
  config[:level] = false
  config[:type] = false
  config[:app] = :cus_app
  config[:timestamp] = false
end

logger.debug({data: "test data", age: 18})

result:

{
  "source": "examples",
  "data": "test data",
  age: 18,
  "cus_app": "app"
}

full code to see examples/ruby_logger

Ruby Logger Text Formatter

Get Start

require 'log_formatter'
require 'log_formatter/ruby_json_formatter'

logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG

logger.formatter = Ruby::JSONFormatter::Base.new

logger.debug("Created logger")

other cases just same as ruby json formatter.

full code to see examples/ruby_text_logger

Log4r JSON Formatter

Get Start

require 'log4r'
require 'log_formatter'
require 'log_formatter/log4r_json_formatter'

logger = Log4r::Logger.new('Log4RTest')
outputter = Log4r::StdoutOutputter.new(
  "console",
  :formatter => Log4r::JSONFormatter::Base.new
)
logger.add(outputter)

logger.debug("Created logger")

other cases just same as ruby json formatter.

full code to see examples/log4r_logger

Test with Rspec

bundle exec rspec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/chadlwm/log_formatter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.