2012-07-15 20:04:45 +02:00
|
|
|
# Use the Ruby/Rails logger
|
2013-10-20 04:54:57 +02:00
|
|
|
require 'active_support/notifications'
|
2014-01-02 23:38:18 +01:00
|
|
|
require 'active_support/logger'
|
2013-04-10 06:52:25 +02:00
|
|
|
require 'thread'
|
2012-07-15 20:04:45 +02:00
|
|
|
|
|
|
|
module Middleman
|
|
|
|
# The Middleman Logger
|
2014-01-02 23:38:18 +01:00
|
|
|
class Logger < ActiveSupport::Logger
|
2013-12-31 23:41:17 +01:00
|
|
|
def self.singleton(*args)
|
|
|
|
if !@_logger || args.length > 0
|
|
|
|
if args.length == 1 && (args.first.is_a?(::String) || args.first.respond_to?(:write))
|
|
|
|
args = [0, false, args.first]
|
|
|
|
end
|
|
|
|
@_logger = new(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
@_logger
|
|
|
|
end
|
|
|
|
|
2013-10-20 04:54:57 +02:00
|
|
|
def initialize(log_level=1, is_instrumenting=false, target=$stdout)
|
|
|
|
super(target)
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-07-15 20:04:45 +02:00
|
|
|
self.level = log_level
|
|
|
|
@instrumenting = is_instrumenting
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-07-15 20:04:45 +02:00
|
|
|
if @instrumenting != false
|
|
|
|
::ActiveSupport::Notifications.subscribe(/\.middleman$/, self)
|
|
|
|
end
|
2013-04-10 06:52:25 +02:00
|
|
|
|
|
|
|
@mutex = Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(*args)
|
|
|
|
@mutex.synchronize do
|
|
|
|
super
|
|
|
|
end
|
2012-07-15 20:04:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(message, *args)
|
2013-12-28 01:26:31 +01:00
|
|
|
return if @instrumenting.is_a?(String) && @instrumenting != 'instrument' && !message.include?(@instrumenting)
|
2012-07-15 20:04:45 +02:00
|
|
|
|
2015-02-24 20:06:28 +01:00
|
|
|
evt = ::ActiveSupport::Notifications::Event.new(message, *args)
|
2014-08-25 00:49:53 +02:00
|
|
|
info "== Instrument (#{evt.name.sub(/.middleman$/, '')}): #{evt.duration}ms\n#{args.last}"
|
2012-07-15 20:04:45 +02:00
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|