Last active
December 28, 2015 23:09
-
-
Save windy/7577376 to your computer and use it in GitHub Desktop.
最简洁的, 支持同时打印到 STDOUT 与 FILE 的日志系统.
This file contains hidden or 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
require 'logger' | |
require 'forwardable' | |
module Ping | |
class MultiIO | |
def initialize(*targets) | |
@targets = targets | |
end | |
def write(*args) | |
@targets.each { |t| t.write(*args) } | |
end | |
def close | |
@targets.each { |t| t.close } | |
end | |
end | |
class Logger | |
log_file = File.open( File.join( File.dirname(__FILE__), '..', 'log', 'dev.log'), 'a' ) | |
@logger = ::Logger.new( MultiIO.new( STDOUT, log_file ) ) | |
class <<self | |
extend Forwardable | |
def_delegators :@logger, :debug, :info, :warn, :error | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment