Skip to content
10 changes: 5 additions & 5 deletions lib/chitragupta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Chitragupta

# The gem can be used by adding the following in any of the rails initializations: application.rb / environment.rb
# Chitragupta::setup_application_logger(RailsApplicationModule, current_user_function)
def setup_application_logger(app, current_user_caller=nil)
def setup_application_logger(app, current_user_caller=nil, should_trim_long_string=false)

# Should be required only when the rails application is configuring the gem to be used.
require "chitragupta/active_support/tagged_logging/formatter"
Expand All @@ -38,10 +38,10 @@ def setup_application_logger(app, current_user_caller=nil)
end

if Chitragupta::Util.called_as_sidekiq?
Sidekiq.logger.formatter = JsonLogFormatter.new
Sidekiq.logger.formatter = JsonLogFormatter.new(should_trim_long_string)
end

configure_app(app)
configure_app(app, should_trim_long_string)
end


Expand All @@ -50,9 +50,9 @@ def get_unique_log_id
end

private
def configure_app(app)
def configure_app(app, should_trim_long_string)
app::Application.configure do
config.log_formatter = JsonLogFormatter.new if Chitragupta::Util.called_as_rails_server? || Chitragupta::Util.called_as_rake? || Chitragupta::Util.called_as_sidekiq?
config.log_formatter = JsonLogFormatter.new(should_trim_long_string) if Chitragupta::Util.called_as_rails_server? || Chitragupta::Util.called_as_rake? || Chitragupta::Util.called_as_sidekiq?
if Chitragupta::Util.called_as_rails_server?
require "chitragupta/request_log_formatter"
config.lograge.enabled = true
Expand Down
2 changes: 2 additions & 0 deletions lib/chitragupta/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ module Constants
CATEGORY_SERVER = "server"
CATEGORY_PROCESS = "process"
CATEGORY_WORKER = "worker"

MAX_STR_LENGTH = 400000
end
end
5 changes: 4 additions & 1 deletion lib/chitragupta/json_log_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module Chitragupta
class JsonLogFormatter < Logger::Formatter
def initialize(should_trim_long_string=false)
@should_trim_long_string = should_trim_long_string
end

def call(log_level, timestamp, _progname, message)
return Chitragupta::Util::sanitize_keys(log_level, timestamp, message)
return Chitragupta::Util::sanitize_keys(log_level, timestamp, message, @should_trim_long_string)
end
end
end
4 changes: 2 additions & 2 deletions lib/chitragupta/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Chitragupta
class Logger < Syslog::Logger
def initialize(*args)
def initialize(*args, should_trim_long_string: false)
super(*args)
@formatter = Chitragupta::JsonLogFormatter.new
@formatter = Chitragupta::JsonLogFormatter.new(should_trim_long_string)
end
end
end
29 changes: 18 additions & 11 deletions lib/chitragupta/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Chitragupta
module Util
extend self

def sanitize_keys(log_level, timestamp, message)
data = initialize_data(message)
def sanitize_keys(log_level, timestamp, message, should_trim_long_string)
data = initialize_data(message, should_trim_long_string)

data[:log][:level] = log_level
data[:meta][:timestamp] = timestamp
Expand Down Expand Up @@ -37,15 +37,15 @@ def called_as_console?
end

private
def populate_server_data(data, message)
def populate_server_data(data, message, should_trim_long_string)
data[:data][:request] = {}
data[:data][:response] = {}
data[:data][:request][:method] = Chitragupta.payload[:method]
data[:data][:request][:endpoint] = Chitragupta.payload[:path]
data[:data][:request][:ip] = Chitragupta.payload[:ip]
data[:data][:request][:id] = Chitragupta.payload[:request_id]
data[:data][:request][:user_id] = Chitragupta.payload[:user_id]
data[:data][:request][:params] = Chitragupta.payload[:params].to_json.to_s
data[:data][:request][:params] = trim_long_string(Chitragupta.payload[:params].to_json.to_s, should_trim_long_string)

data[:data][:response][:status] = message[:status] rescue nil
data[:data][:response][:duration] = message[:duration] rescue nil
Expand All @@ -57,17 +57,17 @@ def populate_server_data(data, message)
data[:log][:id] ||= Chitragupta.payload[:log_id]
end

def populate_rails_server_data(data, message)
populate_server_data(data, message)
def populate_rails_server_data(data, message, should_trim_long_string)
populate_server_data(data, message, should_trim_long_string)
data[:data][:request][:controller] = Chitragupta.payload[:controller]
data[:data][:request][:action] = Chitragupta.payload[:action]

data[:data][:response][:view_rendering_duration] = message[:view] rescue nil
data[:data][:response][:db_query_duration] = message[:db] rescue nil
end

def populate_ruby_server_data(data, message)
populate_server_data(data, message)
def populate_ruby_server_data(data, message, should_trim_long_string)
populate_server_data(data, message, should_trim_long_string)
end

def populate_task_data(data, message)
Expand All @@ -93,7 +93,7 @@ def populate_worker_data(data, message)
data[:data][:worker_name] = worker_name
end

def initialize_data(message)
def initialize_data(message, should_trim_long_string)
data = {}
data[:data] = {}

Expand All @@ -109,9 +109,9 @@ def initialize_data(message)
data[:meta][:format] ||= {}
begin
if called_as_rails_server?
populate_rails_server_data(data, message)
populate_rails_server_data(data, message, should_trim_long_string)
elsif called_as_rack_server?
populate_ruby_server_data(data, message)
populate_ruby_server_data(data, message, should_trim_long_string)
elsif called_as_rake?
populate_task_data(data, message)
elsif called_as_sidekiq?
Expand All @@ -121,5 +121,12 @@ def initialize_data(message)
return data
end

def trim_long_string(input_str, should_trim_long_string)
if(should_trim_long_string)
return input_str.slice(0, Chitragupta::Constants::MAX_STR_LENGTH)
end
return input_str
end

end
end