ruby - "\xE6" from ASCII-8BIT to UTF 的问题解决办法

访问量: 397

今天早上遇到这个情况,我在提交一个最普通的查询form的时候, 报错。 

这个一看就是rails 日志,在记录每个请求的时候,只要参数中有一些 对号  这样的东西,就会有问题。

/managements/members?utf8=✓&email=22&commit=查询

 33 2019-05-07 07:25:49 +0800: Rack app error handling request { GET /managements/members }
 34 #
 35 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/iooutputter.rb:42:in `write'
 36 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/iooutputter.rb:42:in `print'
 37 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/iooutputter.rb:42:in `write'
 38 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/datefileoutputter.rb:80:in `write'
 39 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/outputter.rb:110:in `block in canonical_log'
 40 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/outputter.rb:130:in `block in synch'
 41 /root/.rbenv/versions/2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
 42 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/outputter.rb:130:in `synch'
 43 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/outputter.rb:110:in `canonical_log'
 44 (eval):3:in `info'
 45 (eval):8:in `block in info'
 46 (eval):8:in `each'
 47 (eval):8:in `info'
 48 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/rack/logger.rb:37:in `call_app'
 49 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/rack/logger.rb:22:in `call'
 50 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-4.2.10/lib/action_dispatch/middleware/request_id.rb:21:in `call'
 51 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rack-1.6.8/lib/rack/methodoverride.rb:22:in `call'
 52 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rack-1.6.8/lib/rack/runtime.rb:18:in `call'
 53 /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-4.2.10/lib/action_dispatch/middleware/static.rb:120:in `call'

解决办法:

修改log4r 中的 rb文件

# ~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/log4r-1.1.9/lib/log4r/outputter/iooutputter.rb
    # perform the write
    def write(data)
      begin
        @out.print data
        @out.flush
      rescue IOError => ioe # recover from this instead of crash
        Logger.log_internal {"IOError in Outputter '#{@name}'!"}
        Logger.log_internal {ioe}
        close
      rescue NameError => ne
        Logger.log_internal {"Outputter '#{@name}' IO is #{@out.class}!"}
        Logger.log_internal {ne}
        close
      # 增加这句话就可以了。
      rescue Encoding::UndefinedConversionError => e
        puts "==!!! by dashi , log4r utf error ? ... #{e}"
        close
      end 
    end

订阅/RSS Feed

Subscribe

分类/category