hupili / snsapi

Cross platform middleware for Social Networking Services: Twitter, Facebook, SinaWeibo, Renren, RSS, Email, Sqlite, ... (more coming)
http://snsapi.ie.cuhk.edu.hk
160 stars 53 forks source link

logger error #85

Open hupili opened 11 years ago

hupili commented 11 years ago
[WARNING][20130712-203020][renren.py][renren_request][196]服务临时不可用
Traceback (most recent call last):
  File "/usr/lib64/python2.7/logging/__init__.py", line 838, in emit
    msg = self.format(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 715, in format
    return fmt.format(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 464, in format
    record.message = record.getMessage()
  File "/usr/lib64/python2.7/logging/__init__.py", line 328, in getMessage
    msg = msg % self.args
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128)
Logged from file snslog.py, line 95
hupili commented 11 years ago

Not sure whether the following line works:

-                logger.warning(response["error_msg"])
+                logger.warning("%s", response["error_msg"])
hupili commented 11 years ago

Tried to fix the following, in https://github.com/hupili/snsapi/commit/24c478ae1da69c398b07cb0619826c46071ed697

$grep -onRP "logger.(debug|info|warning)\([^\'\"].+\)" .
./snspocket.py:102:logger.debug(json.dumps(jsonconf))
./plugin/renren.py:171:logger.warning(response["error_msg"])
./plugin/renren.py:196:logger.warning(response["error_msg"])
./plugin/renren.py:369:logger.debug(json.dumps(dct))
./plugin_trial/facebook.py:30:logger.debug(str(dct))
./plugin_trial/sina_wap.py:177:logger.warning(err_msg)

This error is obscure. We need more investigation. It seems like logging facilities can not handle non-ascii chars in the format string under some environment.

hupili commented 11 years ago

More info.

This is related with encoding of unicode characters.

Similar problem is observed at other places and some folks solve it by:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

There are also many reasons not to do this.