Closed1

Ruby 環境周りのエラーメモ

エラー log writing failed. "\xE7" from ASCII-8BIT to UTF-8

環境

$ rbenv -v
rbenv 1.1.2-61-g585ed84
$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [arm64-darwin20]

自作Gem calendly のメンテのため、irbでAPIのレスポンスログに日本語が含まれるコマンドを試したら、エンコーディング関連エラーに遭遇。

> Calendly.configuration.logger.level = :debug
> client = Calendly::Client.new 'ACCESS_TOKEN'
> client.me
D, [2021-05-24T10:33:46.158518 #2656] DEBUG -- : Request GET https://api.calendly.com/users/me params:, body:
log writing failed. "\xE7" from ASCII-8BIT to UTF-8

TL;DR

logger にStringを渡すときにエンコードが別になるケースがあれば、指定して渡すようにして回避。

# res [Faraday::Response]
res.body.dup.force_encoding(Encoding::UTF_8)

refs encode, force_encoding

https://docs.ruby-lang.org/ja/2.7.0/method/String/i/encode.html
https://docs.ruby-lang.org/ja/2.7.0/method/String/i/force_encoding.html
https://qiita.com/kuracux/items/95c4192e8bc7e66e5e38
https://github.com/trusche/httplog/pull/29/files

試したこと (関係ないことも含まれているかと)

以下を試した。
refs https://stackoverflow.com/questions/4975973/gem-update-unable-to-convert-xe7-to-utf-8-in-conversion-from-ascii-8bit-to-u

$ bundle update

そうすると別の警告が発生。

$ ./bin/console
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/version.rb:3: warning: already initialized constant URI::VERSION_CODE
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/version.rb:3: warning: previous definition of VERSION_CODE was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/version.rb:4: warning: already initialized constant URI::VERSION
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/version.rb:4: warning: previous definition of VERSION was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:29: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::ALPHA
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:28: warning: previous definition of ALPHA was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:31: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::ALNUM
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:30: warning: previous definition of ALNUM was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:35: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::HEX
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:34: warning: previous definition of HEX was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:37: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::ESCAPED
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:36: warning: previous definition of ESCAPED was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:41: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::UNRESERVED
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:40: warning: previous definition of UNRESERVED was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:46: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::RESERVED
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:45: warning: previous definition of RESERVED was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:49: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::DOMLABEL
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:48: warning: previous definition of DOMLABEL was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:51: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::TOPLABEL
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:50: warning: previous definition of TOPLABEL was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc2396_parser.rb:53: warning: already initialized constant URI::RFC2396_REGEXP::PATTERN::HOSTNAME
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc2396_parser.rb:52: warning: previous definition of HOSTNAME was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc3986_parser.rb:6: warning: already initialized constant URI::RFC3986_Parser::RFC3986_URI
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc3986_parser.rb:6: warning: previous definition of RFC3986_URI was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/rfc3986_parser.rb:7: warning: already initialized constant URI::RFC3986_Parser::RFC3986_relative_ref
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/rfc3986_parser.rb:7: warning: previous definition of RFC3986_relative_ref was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:17: warning: already initialized constant URI::REGEXP
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:16: warning: previous definition of REGEXP was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:18: warning: already initialized constant URI::Parser
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:17: warning: previous definition of Parser was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:19: warning: already initialized constant URI::RFC3986_PARSER
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:18: warning: previous definition of RFC3986_PARSER was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:22: warning: already initialized constant URI::DEFAULT_PARSER
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:21: warning: previous definition of DEFAULT_PARSER was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::ABS_URI
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of ABS_URI was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::REL_URI
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of REL_URI was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::URI_REF
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of URI_REF was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::ABS_URI_REF
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of ABS_URI_REF was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::REL_URI_REF
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of REL_URI_REF was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::ESCAPED
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of ESCAPED was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::UNSAFE
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of UNSAFE was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::SCHEME
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of SCHEME was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::USERINFO
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of USERINFO was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::HOST
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of HOST was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::OPAQUE
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of OPAQUE was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::REGISTRY
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of REGISTRY was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::ABS_PATH
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of ABS_PATH was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::REL_PATH
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of REL_PATH was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::QUERY
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of QUERY was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:29: warning: already initialized constant URI::FRAGMENT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:28: warning: previous definition of FRAGMENT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:340: warning: already initialized constant URI::TBLENCWWWCOMP_
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:277: warning: previous definition of TBLENCWWWCOMP_ was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:346: warning: already initialized constant URI::TBLDECWWWCOMP_
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:283: warning: previous definition of TBLDECWWWCOMP_ was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/common.rb:512: warning: already initialized constant URI::WEB_ENCODINGS_
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/common.rb:449: warning: previous definition of WEB_ENCODINGS_ was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/generic.rb:28: warning: already initialized constant URI::Generic::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/generic.rb:27: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/generic.rb:47: warning: already initialized constant URI::Generic::COMPONENT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/generic.rb:46: warning: previous definition of COMPONENT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/generic.rb:62: warning: already initialized constant URI::Generic::USE_REGISTRY
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/generic.rb:61: warning: previous definition of USE_REGISTRY was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/file.rb:12: warning: already initialized constant URI::File::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/file.rb:12: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/file.rb:17: warning: already initialized constant URI::File::COMPONENT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/file.rb:17: warning: previous definition of COMPONENT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ftp.rb:25: warning: already initialized constant URI::FTP::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ftp.rb:24: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ftp.rb:30: warning: already initialized constant URI::FTP::COMPONENT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ftp.rb:29: warning: previous definition of COMPONENT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ftp.rb:43: warning: already initialized constant URI::FTP::TYPECODE
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ftp.rb:42: warning: previous definition of TYPECODE was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ftp.rb:46: warning: already initialized constant URI::FTP::TYPECODE_PREFIX
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ftp.rb:45: warning: previous definition of TYPECODE_PREFIX was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/http.rb:25: warning: already initialized constant URI::HTTP::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/http.rb:24: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/http.rb:28: warning: already initialized constant URI::HTTP::COMPONENT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/http.rb:27: warning: previous definition of COMPONENT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/https.rb:20: warning: already initialized constant URI::HTTPS::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/https.rb:19: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ldap.rb:27: warning: already initialized constant URI::LDAP::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ldap.rb:26: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ldap.rb:30: warning: already initialized constant URI::LDAP::COMPONENT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ldap.rb:29: warning: previous definition of COMPONENT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ldap.rb:48: warning: already initialized constant URI::LDAP::SCOPE_ONE
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ldap.rb:47: warning: previous definition of SCOPE_ONE was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ldap.rb:49: warning: already initialized constant URI::LDAP::SCOPE_SUB
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ldap.rb:48: warning: previous definition of SCOPE_SUB was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ldap.rb:50: warning: already initialized constant URI::LDAP::SCOPE_BASE
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ldap.rb:49: warning: previous definition of SCOPE_BASE was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ldap.rb:47: warning: already initialized constant URI::LDAP::SCOPE
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ldap.rb:46: warning: previous definition of SCOPE was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/ldaps.rb:18: warning: already initialized constant URI::LDAPS::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/ldaps.rb:18: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/mailto.rb:22: warning: already initialized constant URI::MailTo::DEFAULT_PORT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/mailto.rb:21: warning: previous definition of DEFAULT_PORT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/mailto.rb:25: warning: already initialized constant URI::MailTo::COMPONENT
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/mailto.rb:24: warning: previous definition of COMPONENT was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/mailto.rb:53: warning: already initialized constant URI::MailTo::HEADER_REGEXP
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/mailto.rb:52: warning: previous definition of HEADER_REGEXP was here
~/.rbenv/versions/2.7.2/lib/ruby/2.7.0/uri/mailto.rb:56: warning: already initialized constant URI::MailTo::EMAIL_REGEXP
~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/uri-0.10.1/lib/uri/mailto.rb:55: warning: previous definition of EMAIL_REGEXP was here

以下を行ったら警告が消えた。
refs https://github.com/rbenv/ruby-build/issues/1401#issuecomment-607947883

$ bundle clean --force

最終的には、force_encoding('UTF-8') する形でログに吐かれるようになった。

# before
logger.send level, msg

# after (frozen_string_literal:true なので dupしてから force_endcoding で encoding を指定) 
logger.send level, msg.dup.force_encoding('UTF-8')
このスクラップは3ヶ月前にクローズされました
作成者以外のコメントは許可されていません