Is there variant to redirect stdout to errror.log or output.log without using linux file redirection like > logfile 2>&1 ? It may be usefull for Windows users and users that are comming from uwsgi. Uwsgi outputs to error.log stdout and stderr.
Welcome! If I remember correctly, gunicorn uses stderr by default. You can use --log-file foo.log
to customize it.
See also http://docs.gunicorn.org/en/19.3/settings.html#errorlog
...then programmist in simple debug enviroment will never see output of print("Hallo world") in logfile. Only print("Hallo world", file=sys.stderr) will be seen. I think, gunicorn needs option to redirect stdout to logfile to simplify this. Option by default must be turned off, because we are not need new file descriptor in production enviroment.
@netinsideout don't use "print". Use the logging
module.
@tilgovi "Simple is better than complex." Logging module is not simple, because it must be configured before using. Sometimes this is not need at all, because project is too simple. As i can see print statment used in gunicorn project 20 times and logging module used 17 times.
It is already configured for you if you have started your application with gunicorn. import logging; logging.{warn,error,info}
. Nothing else is needed. It is not complex.
The print statements are used in gunicorn only as we bootstrap the application through the CLI, before logging is set up.
Application code should always use logging
rather than print
.
@tilgovi Explain this to all python devellopers and you will have success. Gunicorn documentation does not have clear explanation currently about not using stdout and preconfigured logging. This topic will have continued, because others devs will have same questions. By the way, you are really always use logging and never use print, pprint or sys.stdout for making pipe with other application or hardware? Ask yourself, why other projects (and alternatives to gunicorn) use stdout and logging together? Sometimes bad practice is very convenient and flexible.
It might be possible for us to redirect the stdout for workers to the error log.
I think catching stdout/stderr should be turned off by default, because systemd already catches service output and saves in journal and forwarding to file is unexpected.
I think this issue is not actionable and I am closing it. We could re-open sys.stdout
but that feels a little messy to me and references might exist already as local symbols if we don't do it soon enough.
I suggest that shell redirection is the supported solution to this. It should work on windows, too.
Most helpful comment
It is already configured for you if you have started your application with gunicorn.
import logging; logging.{warn,error,info}
. Nothing else is needed. It is not complex.The print statements are used in gunicorn only as we bootstrap the application through the CLI, before logging is set up.
Application code should always use
logging
rather thanprint
.