0.0
The project is in a healthy, maintained state
Simple structured logger.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.21
~> 0.11.1
~> 13.1
~> 3.13

Runtime

 Project Readme

OnStrum::Logs - Simple configurable structured logger with JSON formatter out of the box

Maintainability Test Coverage CircleCI Gem Version Downloads GitHub Contributor Covenant

Simple configurable structured logger with JSON formatter out of the box.

Table of Contents

  • Features
  • Requirements
  • Installation
  • Configuring
    • Custom formatter
    • Resetting configuration
  • Usage
    • Hash
    • Exception
    • Other
    • Debug mode
  • Contributing
  • License
  • Code of Conduct
  • Credits
  • Versioning
  • Changelog

Features

  • Structured JSON log
  • Built-in detailed (debug) formatter
  • Ability to configure builtin attribute key names
  • Ability to configure required attributes
  • Ability to configure formatters
  • Ability to serialize an exceptions into structured log

Requirements

Ruby MRI 2.5.0+

Installation

Add this line to your application's Gemfile:

gem 'on_strum-logs'

And then execute:

bundle

Or install it yourself as:

gem install on_strum-logs

Configuring

To start working with this gem, you must configure it first as in the example below:

# config/initializers/on_strum_logs.rb

require 'on_strum/logs'

OnStrum::Logs.configure do |config|
  # Optional parameter. Ability to define static fields in the logger document root.
  # It is equal to empty hash by default.
  config.root_fields = {
    service_name: 'My Great Application',
    service_version: '1.42.0'
  }

  # Optional parameter. The colorized structured log in STDOUT. It could be useful
  # for debug mode.
  config.detailed_formatter = true

  # Optional parameter. You can use your custom formatter insted of built-in.
  # Please note, using this option will override using detailed_formatter option.
  config.custom_formatter = YourCustomFormatter

  # Optional parameter. You can override log level builtin attribute key.
  # It is equal to :level by default.
  config.field_name_level = :log_level

  # Optional parameter. You can override log time builtin attribute key.
  # It is equal to :time by default.
  config.field_name_time = :log_time

  # Optional parameter. You can override log message builtin attribute key.
  # It is equal to :message by default.
  config.field_name_message = :log_message

  # Optional parameter. You can override log context builtin attribute key.
  # It is equal to :context by default.
  config.field_name_context = :log_context

  # Optional parameter. You can override log exception message builtin attribute key.
  # It is equal to :message by default.
  config.field_name_exception_message = :log_exception_message

  # Optional parameter. You can override log exception stack trace builtin attribute key.
  # It is equal to :stack_trace by default.
  config.field_name_exception_stack_trace = :log_exception_stack_trace
end

Custom formatter

Example of defining custom formatter:

YourCustomFormatter = Class.new do
  def self.call(**log_data)
    log_data
  end
end

Resetting configuration

To reset current configuration you can use built-in interface:

OnStrum::Logs.reset_configuration!

Usage

In OnStrum::Logs supports 3 standard log levels: INFO, ERROR and DEBUG.

OnStrum::Logs.info(object)
OnStrum::Logs.error(object)
OnStrum::Logs.debug(object)

As methods argument you can use instance of Hash, Exception or any other class (it will be stringified).

Hash

Please note, when you uses Hash, :message is required key. Otherwise you will get an exception.

OnStrum::Logs.info(message: 'My Message', some_attribute: 'some attribute')
{
  "level": "INFO",
  "time":" 2023-10-15T13:15:21.129+02:00",
  "message": "My Message",
  "context": {
    "some_attribute": "some attribute"
  },
  "service_name": "My Great Application",
  "service_version": "1.42.0"
}

Exception

OnStrum::Logs.error(StandardError.new('error message'))
{
  "level": "ERROR",
  "time": "2023-10-15T13:32:15.851+02:00",
  "message": "Exception: StandardError",
  "context": {
    "message": "error message",
    "stack_trace": null
  },
  "service_name": "My Great Application",
  "service_version": "1.42.0"
}

Other

OnStrum::Logs.debug(42)
{
  "level": "DEBUG",
  "time": "2023-10-15T13:33:51.889+02:00",
  "message": "42",
  "context": null,
  "service_name": "My Great Application",
  "service_version": "1.42.0"
}

Debug mode

For view detailed colorized logs you can use configuration option detailed_formatter = true:

require 'on_strum/logs'

OnStrum::Logs.configure do |config|
  config.root_fields = {
    service_name: 'My Great Application',
    service_version: '1.42.0'
  }
  config.detailed_formatter = true
end

OnStrum::Logs.info(
  message: 'My Message',
  attribute_1: 'attribute 1',
  attribute_2: {
    a: 42,
    b: Class.new
  }
)
{
            :level => "INFO",
            :time  => 2023-10-15 14:02:11.441533 +0200,
          :message => "My Message",
          :context => {
      :attribute_1 => "attribute 1",
      :attribute_2 => {
          :a => 42,
          :b => #<Class:0x0000000103763528> < Object
      }
  },
      :service_name => "My Great Application",
   :service_version => "1.42.0"
}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/on-strum/ruby-on-strum-logs. 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. Please check the open tickets. Be sure to follow Contributor Code of Conduct below and our Contributing Guidelines.

License

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

Code of Conduct

Everyone interacting in the on_strum-logs project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Credits

Versioning

on_strum-logs uses Semantic Versioning 2.0.0