记录错误:TypeError:需要一个类似字节的对象,而不是'str'

时间:2019-09-19 07:12:05

标签: python-3.7 django-2.2

当我在项目上将python从2.7.16更新到3.7.4时,django控制台显示如下记录错误:

--- Logging error ---
Traceback (most recent call last):
  File "c:\python37\Lib\logging\__init__.py", line 1028, in emit
    stream.write(msg + self.terminator)
TypeError: a bytes-like object is required, not 'str'

但是当我使用python2.7时,效果很好

我的环境:Windows 10 python3.7.4 django2.2.5 eclipse + pydev7.3.0.201908161924 有关记录部分的django settings.py

log_path = config.get('log', 'log_path')  #windows path

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
    },
    'formatters': {
        'standard': {
            'format': '%(asctime)s|%(levelname)s|%(process)d|%(funcName)s|%(lineno)d|msg:%(message)s'
        },
        'verbose': {
            'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
            'style': '{',
        },
        'simple': {
            'format': '{levelname} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['require_debug_false'],  #  DEBUG = False sendemail
            'include_html':True,
            'formatter': 'verbose',
        },
        'default': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(log_path,'skstack.log'),  
            'maxBytes': 1024 * 1024 * 5,  
            'backupCount': 5,  
            'formatter': 'verbose',  
        },
        'error_file': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(log_path,'skstack-error.log'),
            'formatter': 'verbose'
        },
        'info_file': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(log_path,'skstack-info.log'),
            'formatter': 'verbose'
        },
        'debug': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(log_path,'skstack-debug.log'),
            'maxBytes': 1024 * 1024 * 5,  
            'backupCount': 5,  
            'formatter': 'verbose',  
        },

        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_false'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
         'skworkorders_log': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(log_path,'skworkorders.log'),
            'formatter': 'verbose'
        },

    },
    'loggers': {
        'django': {
            'handlers': ['console', 'info_file'],
            'propagate': True,
        },
        'django.request': {
            'handlers': ['console', 'info_file'],
            'level': 'ERROR',
            'propagate': False,
        },
        'skstack': {
            'handlers': ['console', 'info_file'],
            'level': 'DEBUG',
            'propagate': False,
        },
         'skworkorders': {
            'handlers': ['skworkorders_log','console'],
            'level': 'INFO',
            'propagate': False,
        },
    }
}

完整的错误日志

--- Logging error ---
Traceback (most recent call last):
  File "c:\python37\Lib\logging\__init__.py", line 1028, in emit
    stream.write(msg + self.terminator)
TypeError: a bytes-like object is required, not 'str'
Call stack:
  File "c:\python37\Lib\threading.py", line 890, in _bootstrap
    self._bootstrap_inner()
  File "c:\python37\Lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "c:\python37\Lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "c:\python37\Lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "c:\python37\Lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\python37\Lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\virtualEnv\sk-stack\lib\site-packages\django\core\servers\basehttp.py", line 169, in handle
    self.handle_one_request()
  File "C:\virtualEnv\sk-stack\lib\site-packages\django\core\servers\basehttp.py", line 194, in handle_one_request
    handler.run(self.server.get_app())
  File "c:\python37\Lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "c:\python37\Lib\wsgiref\handlers.py", line 196, in finish_response
    self.close()
  File "C:\virtualEnv\sk-stack\lib\site-packages\django\core\servers\basehttp.py", line 111, in close
    super().close()
  File "c:\python37\Lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
  File "c:\python37\Lib\http\server.py", line 544, in log_request
    self.requestline, str(code), str(size))
  File "C:\virtualEnv\sk-stack\lib\site-packages\django\core\servers\basehttp.py", line 154, in log_message
    level(format, *args, extra=extra)
Message: '"%s" %s %s'
Arguments: ('GET /skworkorders/WorkOrderCommit/ HTTP/1.1', '200', '35764')

我可以看到Django请求日志已记录在skstack-info.log中

2019-09-19 11:44:40,515|INFO|60880|log_message|154|msg:"GET 
/skworkorders/WorkOrderCommit/ HTTP/1.1" 200 35764

当我在发射中修改文件“ c:\ python37 \ Lib \ logging__init __。py”,行1029时

stream.write(msg + self.terminator)

stream.write(str.encode(msg + self.terminator))

它还向我显示了另一个错误,如下所示:

--- Logging error ---
Traceback (most recent call last):
  File "c:\python37\Lib\logging\__init__.py", line 1029, in emit
    stream.write(str.encode(msg + self.terminator))
TypeError: write() argument must be str, not bytes

3 个答案:

答案 0 :(得分:0)

当我无法像下面那样登录logging.loggers.django时,它工作正常,但我在IDE控制台上再也看不到django日志了

    'loggers': {
#         'django': {
#             'handlers': ['console'],
#             'propagate': True,
#         },

答案 1 :(得分:0)

我通过以下方法解决了这个问题

1。我使用“ logging”作为关键字搜索我的代码,发现我已经在另一个地方设置了logging.basicConfig
 2.我将文件模式从filemode ='ab +'更改为filemode ='a +'然后工作正常
 3.完整的设置如下

AfterConstruction

答案 2 :(得分:0)

为避免该错误,正如您提到的那样,足以使用

filemode='a+'

我正在使用bw,并更改为a +后,它立即开始在Python3.7上运行。

希望可以提供更多帮助。 谢谢并恭祝安康, 马可