I have a Django app that I'm hosting on Heroku and logging to Papertrail via the Papertrail Heroku add-on. There are numerous places where I'm logging information to Papertrail directly, currently by:
logger = logging.getLogger('papertrail')
logger.info('important text')
I set up the logging configuration according to this link:
import sys
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'ERROR',
},
'papertrail': {
'handlers': ['console'],
'level': 'INFO',
},
},
}
Together, this logs to Papertrail like so:
Feb 07 06:10:56 app_name app/worker.1: important text
However, I noticed that using print('important text')
accomplishes the same thing here. Is there any benefit to continuing to use logging
over print
to log the "important text" in this situation? I read through this article and none of it seemed to apply here.
I find myself asking that question sometimes, and I usually conclude that print
is useful for debugging, but logging is more powerful for everything else.
For instance, logging has log levels, which show severity. And also, logging allows to write logs and output, errors, etc to files.
Any application in production should have a comprehensive logging system in place for these reasons and more.
Take a read through this and it should become clearer:
https://docs.djangoproject.com/en/1.10/topics/logging/
If you aren't convinced by this answer and the article you cited, I'd recommend doing more research. Otherwise, continue to use print
and maybe you'll convince yourself eventually.