forked from liangliangyy/DjangoBlog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgunicorn.py
120 lines (104 loc) · 3.34 KB
/
gunicorn.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import logging
import logging.config
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
from DjangoBlog.settings import BASE_DIR
# user = 'www-data'
# group = 'www-data'
# bind ip and port
bind = '0.0.0.0:8000'
# bind = '127.0.0.1:8000'
# The maximum number of pending connections. [2048]
backlog = 512
# work directory
chdir = '/code'
# chdir = '/Users/pointone/Documents/ws'
# timeout
timeout = 30
# Timeout for graceful workers restart. [30]
graceful_timeout = 10
# model: default sync, we use gevent
worker_class = 'gevent'
# process / worker num
workers = multiprocessing.cpu_count() * 2 + 1
# thread num of per process / worker
threads = 2
worker_connections = 1000
# access log format
access_log_format = '%(t)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
# access log path
accesslog = '/code/logs/gunicorn/gunicorn.access.log'
# error log level, access log cannot set level
loglevel = 'info'
# errorlog = '/code/logs/gunicorn_error.log'
pidfile = './logs/.gunicorn.pid'
# daemon = True
## LOG
BASE_LOG_DIR = os.path.join(BASE_DIR, 'logs')
os.makedirs(BASE_LOG_DIR, exist_ok=True)
os.makedirs(BASE_LOG_DIR + '/gunicorn', exist_ok=True)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'loggers': {
'gunicorn.error': {
'handlers': ['error_file'],
'level': 'INFO',
'propagate': False,
"qualname": "gunicorn.error",
},
"gunicorn.access": {
"level": "DEBUG",
"handlers": ["access_file"],
"propagate": True,
"qualname": "gunicorn.access"
},
},
'handlers':{
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'key_value',
'stream': 'ext://sys.stdout'
},
"error_file": {
"class": "logging.handlers.RotatingFileHandler",
"maxBytes": 1024*1024*1024, # 1G
"backupCount": 1,
"formatter": "generic",
# 'mode': 'w+',
"filename": os.path.join(BASE_LOG_DIR, 'gunicorn', 'gunicorn.error.log'),
},
"access_file": {
'class': 'logging.handlers.TimedRotatingFileHandler',
"backupCount": 30,
'when': 'D',
"formatter": "generic",
'encoding': 'utf-8',
"filename": os.path.join(BASE_LOG_DIR, 'gunicorn', 'gunicorn.access.log'),
},
},
'formatters':{
'key_value': {
'format': 'timestamp=%(asctime)s pid=%(process)d loglevel=%(levelname)s msg=%(message)s'
},
"generic": {
"format": "'[%(process)d] [%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s'",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter"
},
"access": {
"format": "'[%(process)d] [%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s'",
"class": "logging.Formatter"
}
}
}
logging.config.dictConfig(LOGGING)
# acclog = logging.getLogger('gunicorn.access')
# acclog.addHandler(WatchedFileHandler('/code/logs/gunicorn_access.log'))
# acclog.propagate = False
# errlog = logging.getLogger('gunicorn.error')
# errlog.addHandler(WatchedFileHandler('/code/logs/gunicorn_error.log'))
# errlog.propagate = False