Django “./manage.py runserver” log to file instead of console
运行
我需要在
Django日志记录文档很好,但我似乎无法将日志记录配置为与
问题:如何将
这是一个简单的linux重定向,所以看起来应该是这样的:
1 | python manage.py runserver 0.0.0.0:8080 >> log.log 2>&1 |
请注意,我已将8080设置为本地端口,您应根据项目进行更改。
PS:此方法(manage runserver)应仅用于开发,而不用于部署。
这些是一些示例设置,应确保将日志写入控制台和文件。 您可以在开发设置中添加/修改它:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'standard': { 'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s' }, }, 'handlers': { # this is what you see in runserver console 'console': { 'class': 'logging.StreamHandler', 'formatter': 'standard', }, # this handler logs to file #▼▼▼▼ this is just a name so loggers can reference it 'file': { 'class': 'logging.FileHandler', # choose file location of your liking 'filename': os.path.normpath(os.path.join(BASE_DIR, '../../logs/django.log')), 'formatter': 'standard' }, }, 'loggers': { # django logger 'django': { # log to console and file handlers 'handlers': ['console', 'file'], 'level': os.getenv('DJANGO_LOG_LEVEL', 'ERROR'), # choose verbosity }, }, } |