Skip to content

Commit

Permalink
defer all logging to gunicorn if gunicorn is in use
Browse files Browse the repository at this point in the history
  • Loading branch information
deargle committed Mar 27, 2021
1 parent 8d45b18 commit 79f16bd
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions psiturk/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,30 @@
CONFIG = PsiturkConfig()
CONFIG.load_config()

# Setup logging
# taken from: https://stackoverflow.com/a/44760039/2714651
handlers = []
if 'ON_CLOUD' in os.environ:
stream_handler = logging.StreamHandler(sys.stderr)
handlers += [stream_handler]
else:
file_path = os.path.join(os.getcwd(), CONFIG.get("Server Parameters", "logfile"))
file_handler = logging.FileHandler(filename=file_path)
handlers += [file_handler]

LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
logging.CRITICAL]
LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')]
logging.basicConfig(handlers=handlers, format='%(asctime)s %(message)s',
level=LOG_LEVEL)


# Status codes

# Let's start
# ===========

app = Flask("Experiment_Server")

# experiment server logging
if 'gunicorn' in os.environ.get('SERVER_SOFTWARE', ''):
gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
else:
errorlog = CONFIG.get("Server Parameters", "errorlog")
if errorlog == '-':
handler = logging.StreamHandler(sys.stderr)
else:
file_path = os.path.join(os.getcwd(), errorlog)
handler = logging.FileHandler(filename=file_path)

LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
logging.CRITICAL]
LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')]

logging.basicConfig(handlers=[handler], format='%(asctime)s %(message)s',
level=LOG_LEVEL)

# Set cache timeout to 10 seconds for static files
app.config.update(SEND_FILE_MAX_AGE_DEFAULT=10)
Expand Down

0 comments on commit 79f16bd

Please sign in to comment.