[Awesome Ruby Gem] Use rails_semantic_logger gem to replaces the Rails default logger

rails_semantic_logger

Semantic Logger is a comprehensive Rails logging interface that enables human and machine readable logging outputs.

  • Colorful text log files for humans to read.

  • JSON log files containing all Semantic information that machines can easily comsume.

  • Write to multiple output destinations at the same time.

  • Forward logs to a centralized logging system, via JSON output or built-in appenders.

Installation

You can install it as a gem:

1
2
3
$ gem install amazing_print

$ gem install rails_semantic_logger

or add it into a Gemfile (Bundler):

1
2
3
4
5
6
7
8
# Gemfile

# is optional, but is recommended to get colorized output of semantic data (Hash output).
gem 'amazing_print', '1.2.2'

# rocketjob/rails_semantic_logger: Rails Semantic Logger replaces the Rails default logger with Semantic Logger
# https://github.com/rocketjob/rails_semantic_logger
gem 'rails_semantic_logger', '4.4.4'

Then, run bundle install.

1
$ bundle install

This will automatically replace the standard Rails logger with Semantic Logger which will write all log data to the configured Rails log file.

Conflicting Gems

Remove all of the following gems since they conflict or duplicate what Rails Semantic Logger already achieves.

  • lograge

  • rails_stdout_logging

  • rails_12factor

Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# config/initializers/semantic_logger.rb

if defined? SemanticLogger
ApplicationRecord.include(SemanticLogger::Loggable)

# # Semantic Logger for Ruby or Rails. Supports Graylog, Bugsnag, MongoDB, Splunk, Syslog, NewRelic.
# # https://logger.rocketjob.io/rails.html
# STDOUT.sync = true
# # all logging needs to be set to standard out.
# config.rails_semantic_logger.add_file_appender = false

# Semantic Logger for Ruby or Rails. Supports Graylog, Bugsnag, MongoDB, Splunk, Syslog, NewRelic.
# https://logger.rocketjob.io/appenders.html
# Semantic Logger can log data to any IO Stream instance, such as $stderr or $stdout
SemanticLogger.add_appender(io: $stdout, level: :debug)
# SemanticLogger.add_appender(io: $stderr, level: :error)
else
puts("\e[31mWarning: ApplicationRecord not include SemanticLogger::Loggable!\e[0m")
end

Usage

Example:

1
2
3
4
5
6
7
8
9
10
11
require 'semantic_logger'

# Set the global default log level
SemanticLogger.default_level = :trace

# Log to a file, and use the colorized formatter
SemanticLogger.add_appender(file_name: 'development.log', formatter: :color)

# Create an instance of a logger
# Add the application/class name to every log message
logger = SemanticLogger['MyClass']

Informational logging

1
logger.info("Calling Supplier")

Error information

1
logger.error("Oops external call failed", :result => :failed, :reason_code => -10)

References

[1] rocketjob/rails_semantic_logger: Rails Semantic Logger replaces the Rails default logger with Semantic Logger - https://github.com/rocketjob/rails_semantic_logger

[2] Semantic Logger for Ruby or Rails. Supports Graylog, Bugsnag, MongoDB, Splunk, Syslog, NewRelic. - https://logger.rocketjob.io/index.html