开发项目,使用uwsgi,遇到个头痛的问题,如果指定了uwsgi打印日志,当日志中写入中文时,直接会导致报错
代码语言:txt复制Traceback (most recent call last):
File "/usr/Python-3.6.1/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/Python-3.6.1/lib/python3.6/site-packages/django/utils/deprecation.py", line 91, in __call__
response = response or self.get_response(request)
File "/usr/Python-3.6.1/lib/python3.6/site-packages/django/core/handlers/exception.py", line 36, in inner
response = response_for_exception(request, exc)
File "/usr/Python-3.6.1/lib/python3.6/site-packages/django/core/handlers/exception.py", line 95, in response_for_exception
exc_info=sys.exc_info(),
File "/usr/Python-3.6.1/lib/python3.6/site-packages/django/utils/log.py", line 228, in log_response
exc_info=exc_info,
File "/usr/Python-3.6.1/lib/python3.6/logging/__init__.py", line 1335, in error
self._log(ERROR, msg, args, **kwargs)
File "./CapMockWeb/settings/log.py", line 132, in _log
super()._log(level, msg, args, exc_info, extra, stack_info)
File "/usr/Python-3.6.1/lib/python3.6/logging/__init__.py", line 1442, in _log
self.handle(record)
File "/usr/Python-3.6.1/lib/python3.6/logging/__init__.py", line 1452, in handle
self.callHandlers(record)
File "/usr/Python-3.6.1/lib/python3.6/logging/__init__.py", line 1514, in callHandlers
hdlr.handle(record)
File "/usr/Python-3.6.1/lib/python3.6/logging/__init__.py", line 863, in handle
self.emit(record)
File "/usr/Python-3.6.1/lib/python3.6/logging/handlers.py", line 75, in emit
self.handleError(record)
File "/usr/Python-3.6.1/lib/python3.6/logging/__init__.py", line 916, in handleError
traceback.print_exception(t, v, tb, None, sys.stderr)
File "/usr/Python-3.6.1/lib/python3.6/traceback.py", line 101, in print_exception
print(line, file=file, end="")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 96-97: ordinal not in range(128)
解决该问题从2个方面着手
- 指定日志handler时候可以使用encoding来指定编码格式
- 修改文件系统的编码格式
但是查了半天文档并未找到uwsgi日志的指定编码格式的方法,看来只能从第二点着手
先调用python方法查看下系统的编码格式
代码语言:txt复制>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>> sys.getfilesystemencoding()
'ascii'
发现主要是因为文件系统编码格式是ascii导致的
查找文档,可以使用下面命令修改文件系统编码格式
代码语言:txt复制localedef -c -f UTF-8 -i en_US en_US.UTF-8
export LC_ALL=en_US.UTF-8
其实只用第二句命令就可以了。
再次尝试已经修改成功
代码语言:txt复制>>> import sys
>>> sys.getfilesystemencoding()
'utf-8'
>>>
问题解决!!
ps:
好多pip 报错ascii的问题,也是因为这个原因,一并解决了!!!