Last active
May 18, 2017 19:23
-
-
Save Sohair63/106dd168af3bc5f8f37f82c05947ce0f to your computer and use it in GitHub Desktop.
Rails Logging with Custom Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Logging | |
class CustomLogger | |
DEFAULTS = { | |
level: :info | |
} | |
LEVELS = %w(debug info warn error fatal unknown).map(&:to_sym) | |
def self.log(*tags, **params) | |
level = determine_log_level(**params) | |
params.delete(:level) | |
message = build_message(*tags, **params) | |
Rails.logger.send(level, message) | |
end | |
private | |
def self.determine_log_level(**params) | |
params.has_key?(:level) && params[:level].to_sym.in?(LEVELS) ? params[:level].to_sym : :info | |
end | |
def self.build_message(*tags, **params) | |
tags.map!{ |tag| format_tag(tag) } | |
params = params.map{ |args| format_param(args[0], args[1]) } | |
tags.join(' ') + ' ' + params.join(' ') | |
end | |
def self.format_tag(tag) | |
tag = tag.to_s.gsub(/[^\w]/i, '').upcase | |
"[#{tag}]" | |
end | |
def self.format_param(key, value) | |
key = key.to_s.gsub(/[^\w]/i, '').downcase | |
value = value.to_s.gsub('"','') | |
"#{key}=\"#{value}\"" | |
end | |
end | |
end | |
# Can be used in conjunction with Rails.logger.tagged and Rails.logger.silence | |
def log(*tags, **params) | |
Logging::CustomLogger.log(*tags, **params) | |
end | |
# Usage | |
log('MAIN_TAG', 'SUB_TAG', key1: value1, key2: value2, error_message: e.message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage