Skip to content

Instantly share code, notes, and snippets.

@windy
Last active December 28, 2015 23:09
Show Gist options
  • Save windy/7577376 to your computer and use it in GitHub Desktop.
Save windy/7577376 to your computer and use it in GitHub Desktop.
最简洁的, 支持同时打印到 STDOUT 与 FILE 的日志系统.
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