使用gunicorn和命令行脚本配置django日志

q1qsirdb  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(153)

到目前为止,此配置

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(asctime)s [%(levelname)s] %(name)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    },
}

对我来说已经足够好了,所有的东西都是控制台,然后使用PYTHONUNBUFFERED=TRUE和gunicorn配置

bind = "0.0.0.0:50000"
workers = 1
worker_class = "gthread"
threads = 2
worker_connections = 1000
timeout = 60
keepalive = 5
accesslog = "/var/log/gunicorn/myapp.com-access.log"
errorlog = "/var/log/gunicorn/myapp.com-error.log"
loglevel = "debug"
capture_output = True

一切都转到accesslog或errorLog。
现在我需要使用poetry run python manage.py sqlflush,我遇到了麻烦。所有我的logger.info()日志也输出到控制台,所以我得到

Logging in
Logged in
Logging in
Already logged in
BEGIN;
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE `myapp_imagemodel`;
TRUNCATE `auth_user_groups`;
TRUNCATE `auth_user_user_permissions`;
TRUNCATE `auth_permission`;
TRUNCATE `auth_group_permissions`;
TRUNCATE `auth_user`;
TRUNCATE `auth_group`;
TRUNCATE `django_content_type`;
TRUNCATE `myapp_game`;
TRUNCATE `myapp_game_screenshots`;
TRUNCATE `django_session`;
TRUNCATE `django_admin_log`;
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;

我应该如何配置日志记录,使其不会与manage.py脚本的输出混淆?我还能把这些都放在一个文件夹里吗?

l5tcr1uw

l5tcr1uw1#

如果你不想让SQL打印到你的终端,你可以禁用数据库后端的日志记录

'handlers': {
     'null': {
            'level': 'DEBUG',
            'class':'logging.NullHandler',
      },
},
'loggers': {
     'django.db.backends': { # disable database logging
            'handlers': ['null'],
            'propagate': False,
            'level':'DEBUG',
      },
 }

相关问题