когда я обновил 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
My Env: 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, вemit
stream.write(msg + self.terminator)
to
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