Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# backported from rails 4 to use inside rails 3
# if you're using rails 4, you do not actually need this.
module ActiveSupport
class Logger < ::Logger
# extend ActiveSupport::TaggedLogging
# Broadcasts logs to multiple loggers. Returns a module to be
# `extended`'ed into other logger instances.
def self.broadcast(logger)
Module.new do
define_method(:add) do |*args, &block|
logger.add(*args, &block)
super(*args, &block)
end
define_method(:<<) do |x|
logger << x
super(x)
end
define_method(:close) do
logger.close
super()
end
define_method(:progname=) do |name|
logger.progname = name
super(name)
end
define_method(:formatter=) do |formatter|
logger.formatter = formatter
super(formatter)
end
define_method(:level=) do |level|
logger.level = level
super(level)
end
end # Module.new
end # broadcast
def initialize(*args)
super
@formatter = SimpleFormatter.new
end
# Simple formatter which only displays the message.
class SimpleFormatter < ::Logger::Formatter
# This method is invoked when a log event occurs
def call(severity, time, progname, msg)
# caller it's — stacktrace array. caller[4] — a filename and methodname which puts a message.
# E.G.: "[b4bc77d7a61a8641755544e74e6ee824"] 2015-03-13 15:48:40 INFO log_subscriber.rb:105:in `info' -- SOME MESSAGE
# caller[4]l: log_subscriber.rb:105:in `info'. log_subscriber.rb:105 — filename and line No, `info' — method called.
element = caller[4] ? caller[4].split("/").last : "UNDEFINED"
"#{Thread.current[:logstash_logger_tags]||nil } #{time.to_s(:db)} #{severity} #{element} -- #{String === msg ? msg : msg.inspect}\n"
end # call
end # class SimpleFormatter
end # class Logger
end # module ActiveSupport
app_logger = ActiveSupport::Logger.new(Rails.root.join("log/#{Rails.env}.log"))
# Rails logger did not get log level from config because of... stains on the Sun.
# So, we just override it here.
app_logger.level = Logger::INFO if Rails.env.production?
# For rails 4 use just this line
Rails.logger.extend(ActiveSupport::Logger.broadcast(app_logger))
config.log_tags = [:uuid]
config.logger = LogStashLogger.new([
{type: :file, path: "log/logstash_development.log"}
])
# adding backtrace fragment to logstash
# this line will add caller method and line number to the log by default.
# E.G. ["db7ce7ebcf3960ca4b2f4efe53c33f2c"] 2016-03-21 09:12:44 INFO log_subscriber.rb:105:in `info' -- Processing by PspController#entrypoint as */*
LogStashLogger.configure do |config|
config.customize_event do |event|
event["@caller"] = caller[6] ? caller[6].split("/").last : "UNDEFINED"
end
end
Sidekiq.redis = {host: ENV['redis_host'], port: (ENV['redis_port'] || 6379)}
class SidekiqLoggerFormatter < ::Logger::Formatter
SPACE = ' '
# Provide a call() method that returns the formatted message.
def call(severity, time, _program_name, message)
"#{time.utc.iso8601(3)} #{::Process.pid}"\
" TID-#{Thread.current.object_id.to_s(36)}"\
" #{context} #{severity}: #{message}\n"
end
def context
ctx = Thread.current[:sidekiq_context]
"#{ctx}#{SPACE}" if ctx
end
end # class SimpleFormatter
if %(staging production development).include? Rails.env
type = ENV['logstash_type'].to_sym
host = ENV['logstash_host']
port = ENV['logstash_port']
Sidekiq::Logging.logger = LogStashLogger.new(
[{ type: :file,
path: 'log/logstash_sidekiq_'\
"#{Rails.env}.log"
},
{ type: type,
host: host,
port: port
}
])
sidekiq_logger = ActiveSupport::Logger.new(Rails.root.join('log/sidekiq.log'))
Sidekiq::Logging.logger.extend(ActiveSupport::Logger.broadcast(sidekiq_logger))
Sidetiq.logger = LogStashLogger.new(
[{ type: :file,
path: "log/logstash_sidetiq_#{Rails.env}.log"
},
{ type: type,
host: host,
port: port
}
])
sidetiq_logger = ActiveSupport::Logger.new(Rails.root.join('log/sidetiq.log'))
Sidekiq::Logging.logger.extend(ActiveSupport::Logger.broadcast(sidetiq_logger))
end
Sidekiq.logger.formatter = SidekiqLoggerFormatter.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment