在尝试了很多方法之后,它最终使用了下面的配置
设置.py
import sys
from google.cloud import logging as google_cloud_logging
log_client = google_cloud_logging.Client()
if not DEBUG:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'stackdriver_logging': {
'class': 'google.cloud.logging.handlers.CloudLoggingHandler',
'client': log_client
},
'stackdriver_error_reporting': {
'level': 'ERROR',
'class': 'gcp_utils.stackdriver_logging.StackdriverErrorHandler',
}
},
'loggers': {
'django': {
'handlers': ['console', 'stackdriver_logging'],
'level': 'DEBUG',
'propagate': True,
},
'django.request': {
'handlers': [
'stackdriver_logging',
'mail_admins'
],
'level': 'ERROR',
}
},
}
gunicorn.conf.py型
from google.cloud import logging as google_cloud_logging
log_client = google_cloud_logging.Client()
log_client.setup_logging()
bind = "127.0.0.1:8000"
workers = 3
loglevel = "debug"
proc_name = "django_app"
daemon = False
pythonpath = "/path/to/python/"
timeout = 90
accesslog = '/home/user/logs/debug.log'
logconfig_dict = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'stackdriver_logging': {
'class': 'google.cloud.logging.handlers.CloudLoggingHandler',
'client': log_client
},
},
'loggers': {
'gunicorn.access': {
'handlers': ['stackdriver_logging'],
'level': 'INFO',
'propagate': True,
},
'django.request': {
'handlers': [
'stackdriver_logging',
'mail_admins'
],
'level': 'ERROR',
}
},
}
运行gunicorn进程
$ gunicorn -c gunicorn.conf.py wsgi:application
我们必须定义
logconfig_dict
为了让它与google stackdriver日志一起工作。