Removing color decorations from strings before writing them to logfile
This is my current solution
class ColourBlind def initialize(*targets) @targets = targets end def write(*args) @targets.each {|t| t.write(*args.map {|x| x.gsub(/\e\[(\d+)m/, '')}.compact)} end def close @targets.each(&:close) endend
And then:
$logger = Logger.new MultiIO.new($stdout, ColourBlind.new(log_file))
From the colorize gem:
class String REGEXP_PATTERN = /\033\[([0-9]+);([0-9]+);([0-9]+)m(.+?)\033\[0m|([^\033]+)/m def uncolorize self.scan(REGEXP_PATTERN).inject("") do |str, match| str << (match[3] || match[4]) end endend
For removal of ANSI colors, I would recommend
string_with_ascii = "..."string_no_ascii = string_with_ascii.gsub(/\e\[([;\d]+)?m/, '')