Skip to content

Instantly share code, notes, and snippets.

@mymtw
Last active July 30, 2018 14:28
Show Gist options
  • Save mymtw/50332a26c16348ce03e77aa340cb43c9 to your computer and use it in GitHub Desktop.
Save mymtw/50332a26c16348ce03e77aa340cb43c9 to your computer and use it in GitHub Desktop.
[Unit]
Description=Backend flask webapp gunicorn daemon
Requires=backend.socket
After=network.target
[Service]
Environment="LANG=en_US.UTF-8"
PIDFile=/run/backend/pid
User=mtw
Group=mtw
RuntimeDirectory=backend
WorkingDirectory=/var/www/backend
ExecStart=/home/mtw/.virtualenvs/backend/bin/gunicorn --access-logfile /var/log/backend/systemd/gunicorn_access.log --error-logfile /var/log/backend/systemd/gunicorn_access_err.log --pid /run/backend/pid --bind unix:/run/backend/socket server:flask_app -c gunicorn_settings.py
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
Restart=on-failure
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
Description=backend gunicorn socket
[Socket]
ListenStream=/run/backend/socket
[Install]
WantedBy=sockets.target
import logging
from logging.config import dictConfig
LOG_FILENAME = '/tmp/profstandartbackend'
LOG_TO_CONSOLE = True
LOG_TO_FILE = True
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'console': {
'format': '[%(asctime)s][%(levelname)s] %(name)s %(filename)s:%(funcName)s:%(lineno)d | %(message)s',
},
'generic': {
'format': '%(asctime)s [%(process)d] [%(levelname)s] %(message)s',
},
'access': {
'format': '%(message)s',
},
},
'handlers': {
'console': {
'level': logging.WARNING,
'class': 'logging.StreamHandler',
'formatter': 'console',
},
'file': {
'level': logging.INFO,
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'console',
'when': 'D',
'interval': 1,
'utc': True,
'filename': '%s_%s.log' % (LOG_FILENAME, getpass.getuser()),
'backupCount': 15,
'encoding': 'utf-8',
},
'celery': {
'level': 'ERROR',
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'console',
'when': 'D',
'interval': 1,
'utc': True,
'filename': '%s_celery.log' % LOG_FILENAME,
'backupCount': 1,
},
'error_file': {
'class': 'logging.FileHandler',
'formatter': 'console',
'filename': '/tmp/gunicorn.error.log',
},
'access_file': {
'class': 'logging.FileHandler',
'formatter': 'console',
'filename': '/tmp/gunicorn.access.log',
},
},
'loggers': {
'': {
'handlers': [],
# 'handlers': ['console', 'file', 'sentry'],
'level': 'DEBUG',
'propagate': True,
},
'gunicorn.error': {
'level': 'INFO',
'handlers': ['error_file'],
'propagate': True,
},
'gunicorn.access': {
'level': 'INFO',
'handlers': ['access_file'],
'propagate': False,
},
}
}
def switch_handler(used, handler_name):
if used:
if handler_name not in LOGGING['loggers']['']['handlers']:
LOGGING['loggers']['']['handlers'].append(handler_name)
else:
if handler_name in LOGGING['loggers']['']['handlers']:
LOGGING['loggers']['']['handlers'].remove(handler_name)
switch_handler(used=LOG_TO_CONSOLE, handler_name='console')
switch_handler(used=LOG_TO_FILE, handler_name='file')
switch_handler(used=LOG_TO_FILE, handler_name='celery')
switch_handler(used=LOG_TO_FILE, handler_name='error_file')
switch_handler(used=LOG_TO_FILE, handler_name='access_file')
dictConfig(LOGGING)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment