Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging error #949

Closed
encodingl opened this issue Sep 19, 2019 · 3 comments
Closed

Logging error #949

encodingl opened this issue Sep 19, 2019 · 3 comments

Comments

@encodingl
Copy link

when I updated the python from 2.7.16 to 3.7.4 on my project,the django console show logging error as follows:

--- 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'

But It works fine when i used python2.7

My Env:windows 10 python3.7.4 django2.2.5 eclipse+pydev7.3.0.201908161924
the django settings.py about the logging part

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'],
            '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,
        },
    }
}

the full error log

--- 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')

I can see that the django request log has been recorded in skstack-info.log

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

I tried the following, but it didn't work:

  1. reinstall the python virtual machine environment;
  2. use django 1.11.x version;

@encodingl
Copy link
Author

when i modify File "c:\python37\Lib\logging_init_.py", line 1029, in emit

stream.write(msg + self.terminator)

to

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

it also show me the another error as following:

--- 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

@carltongibson
Copy link
Member

Hi. Sorry, this isn't the right place for support issues.

I'd normally suggest django-users, but this particular question looks perfect for Stack Overflow. Be sure to include your full traceback and if you can, the values at the call site. (It's worth searching too: these Py2 to Py3 issues have come up many times.)

Sorry I can't help you here. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants