Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do things go when I ‘print’ them from my Django app?

Tags:

python

django

I have a Django app on a Linux server. In one of the views, some form of print command is executed, and some string gets printed. How can I find out what the printed string was? Is there some log in which these things are kept?

like image 622
Ram Rachum Avatar asked Jul 05 '10 21:07

Ram Rachum


People also ask

Where does Python print go?

In this tutorial, we will learn about the Python print() function with the help of examples. The print() function prints the given object to the standard output device (screen) or to the text stream file.

Where does Django app deploy?

For hosting web applications built on Django, you will need to use a platform that lets you deploy the app. One of these platforms is Heroku. Heroku is a cloud platform on which users can build and deploy applications.

Where are Django logs stored?

In this script, formatters, handlers, and loggers parts of Django logging are defined in the config. dictConfig() method. DEBUG level logging information will be stored in a log file named djangoapp. log and will be printed in the console when the Django app will be executed.


3 Answers

The output should be in the terminal, where django was started. (if you don't started it directly, I don't believe there's a way to read it)

As linkedlinked pointed out, it's the best to not use print, because this can cause Exceptions! But that's not the only reason: There are modules (like logging) made for such purposes and they have a lot more options.

This site (even when it's from 2008) confirm my statements:

If you want to know what’s going on inside a view, the quickest way is to drop in a print statement. The development server outputs any print statements directly to the terminal; it’s the server-side alternative to a JavaScript alert().

If you want to be a bit more sophisticated with your logging, it’s worth turning to Python’s logging module (part of the standard library). You can configure it in your settings.py: here he describes, what to do (look on the site)

For debugging-purposes you could also enable the debug-mode or use the django-debug-toolbar.

Hope it helps! :)

like image 168
Joschua Avatar answered Oct 03 '22 02:10

Joschua


Never use print, as once you deploy, it will print to stdout and WGSI will break.

Use the logging. For development purposes, is really easy to setup. On your project __init__.py:

import logging
from django.conf import settings

fmt = getattr(settings, 'LOG_FORMAT', None)
lvl = getattr(settings, 'LOG_LEVEL', logging.DEBUG)

logging.basicConfig(format=fmt, level=lvl)
logging.debug("Logging started on %s for %s" % (logging.root.name, logging.getLevelName(lvl)))

Now everything you log goes to stderr, in this case, your terminal.

logging.debug("Oh hai!")

Plus you can control the verbosity on your settings.py with a LOG_LEVEL setting.

like image 39
hcalves Avatar answered Oct 03 '22 02:10

hcalves


The print shows up fine with "./manage.py runserver" or other variations - like Joschua mentions, it shows up in the terminal where you started it. If you're running FCGI from cron or such, that just gets dumped into nothingness and you lose it entirely.

For places where I want "print" like warnings or notices to come out, I use an instance of python's logger that pushes to syslog to capture the output and put it someplace. I instantiate an instance of logging in one of the modules as it gets loaded - models.py was the place I picked, just for its convenience and I knew it would always get evaluated before requests came rolling in.

import logging, logging.handlers

logger = logging.getLogger("djangosyslog")
hdlr = logging.handlers.SysLogHandler(facility=logging.handlers.SysLogHandler.LOG_DAEMON)
formatter = logging.Formatter('%(filename)s: %(levelname)s: %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)

Then when you want to invoke a message to the logger in your views or whatever:

logger = logging.getLogger("djangosyslog")
logging.warning("Protocol problem: %s", "connection reset", extra=d)

There's .error(), .critical(), and more - check out http://docs.python.org/library/logging.html for the nitty gritty details.

Rob Hudson's debug toolbar is great if you're looking for that debug information - I use it frequently in development myself. It gives you data about the current request and response, including the SQL used to generate any given page. You can inject into that data like a print by shoving the strings you're interested into the context/response - but I found that to be a bit difficult to deal with.

like image 32
heckj Avatar answered Oct 03 '22 02:10

heckj