forked from jscruggs/metric_fu
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a real logger. Maintain compatibility with MfDebugger
For 5.0, see #113
- Loading branch information
Showing
12 changed files
with
116 additions
and
67 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'logger' | ||
require 'forwardable' | ||
module MetricFu | ||
|
||
def self.logger | ||
@logger ||= ::MetricFu::Logger.new($stdout) | ||
end | ||
|
||
class Logger | ||
extend Forwardable | ||
MfLogger = ::Logger | ||
|
||
def initialize(stdout) | ||
@logger = MfLogger.new(stdout) | ||
self.debug_on = false | ||
self.formatter = ->(severity, time, progname, msg){ "#{msg}\n" } | ||
self.level = 'info' | ||
end | ||
|
||
def debug_on=(bool) | ||
self.level = bool ? 'debug' : 'info' | ||
end | ||
|
||
def debug_on | ||
@logger.level == MfLogger::DEBUG | ||
end | ||
|
||
def_delegators :@logger, :info, :warn, :error, :fatal, :unknown | ||
|
||
LEVELS = { | ||
'debug' => MfLogger::DEBUG, | ||
'info' => MfLogger::INFO, | ||
'warn' => MfLogger::WARN, | ||
'error' => MfLogger::ERROR, | ||
'fatal' => MfLogger::FATAL, | ||
'unknown' => MfLogger::UNKNOWN, | ||
} | ||
|
||
def level=(level) | ||
@logger.level = LEVELS.fetch(level.to_s.downcase) { level } | ||
end | ||
|
||
def formatter=(formatter) | ||
@logger.formatter = formatter | ||
end | ||
|
||
def log(msg) | ||
@logger.info '*'*5 + msg.to_s | ||
end | ||
|
||
def debug(msg) | ||
@logger.debug '*'*5 + msg.to_s | ||
end | ||
|
||
end | ||
|
||
end | ||
# For backward compatibility | ||
def mf_debug(msg,&block) | ||
MetricFu.logger.debug(msg, &block) | ||
end | ||
|
||
def mf_log(msg,&block) | ||
MetricFu.logger.log(msg, &block) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,23 @@ | ||
warn "MfDebugger if deprecated. Please use MetricFu.logger" | ||
MetricFu.lib_require { 'logger' } | ||
module MfDebugger | ||
extend self | ||
|
||
class Logger | ||
class << self | ||
attr_accessor :debug_on | ||
@debug_on = false | ||
def self.debug_on | ||
warn "MfDebugger if deprecated. Please use MetricFu.logger" | ||
MetricFu.logger.debug_on | ||
end | ||
def self.debug_on=(bool) | ||
warn "MfDebugger if deprecated. Please use MetricFu.logger" | ||
MetricFu.logger.level = bool ? 'debug' : 'info' | ||
end | ||
def self.log(msg, &block) | ||
if block_given? | ||
block.call | ||
end | ||
STDOUT.puts '*'*5 + msg.to_s | ||
warn "MfDebugger if deprecated. Please use MetricFu.logger" | ||
MetricFu.logger.info msg | ||
end | ||
def self.debug(msg, &block) | ||
if MfDebugger::Logger.debug_on | ||
if block_given? | ||
log(msg,&block) | ||
else | ||
log(msg) | ||
end | ||
end | ||
end | ||
# From episode 029 of Ruby Tapas by Avdi | ||
# https://rubytapas.dpdcart.com/subscriber/post?id=88 | ||
def self.capture_output(stream=STDOUT, &block) | ||
old_stdout = stream.clone | ||
pipe_r, pipe_w = IO.pipe | ||
pipe_r.sync = true | ||
output = "" | ||
reader = Thread.new do | ||
begin | ||
loop do | ||
output << pipe_r.readpartial(1024) | ||
end | ||
rescue EOFError | ||
end | ||
end | ||
stream.reopen(pipe_w) | ||
yield | ||
ensure | ||
stream.reopen(old_stdout) | ||
pipe_w.close | ||
reader.join | ||
pipe_r.close | ||
return output | ||
warn "MfDebugger if deprecated. Please use MetricFu.logger" | ||
MetricFu.logger.debug msg | ||
end | ||
end | ||
|
||
def mf_debug(msg,&block) | ||
MfDebugger::Logger.debug(msg, &block) | ||
end | ||
|
||
def mf_log(msg,&block) | ||
MfDebugger::Logger.log(msg, &block) | ||
end | ||
|
||
end |
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
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
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
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
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