<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Logging — Django 1.5.9 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.5.9', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="top" title="Django 1.5.9 documentation" href="../index.html" /> <link rel="up" title="Using Django" href="index.html" /> <link rel="next" title="Pagination" href="pagination.html" /> <link rel="prev" title="Time zones" href="i18n/timezones.html" /> <script type="text/javascript" src="../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../index.html">Django 1.5.9 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../index.html">Home</a> | <a title="Table of contents" href="../contents.html">Table of contents</a> | <a title="Global index" href="../genindex.html">Index</a> | <a title="Module index" href="../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="i18n/timezones.html" title="Time zones">previous</a> | <a href="index.html" title="Using Django" accesskey="U">up</a> | <a href="pagination.html" title="Pagination">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-logging"> <div class="section" id="s-module-django.utils.log"> <span id="s-logging"></span><span id="module-django.utils.log"></span><span id="logging"></span><h1>Logging<a class="headerlink" href="#module-django.utils.log" title="Permalink to this headline">¶</a></h1> <div class="section" id="s-a-quick-logging-primer"> <span id="a-quick-logging-primer"></span><h2>A quick logging primer<a class="headerlink" href="#a-quick-logging-primer" title="Permalink to this headline">¶</a></h2> <p>Django uses Python’s builtin <a class="reference external" href="http://docs.python.org/2.7/library/logging.html#logging" title="(in Python v2.7)"><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> module to perform system logging. The usage of this module is discussed in detail in Python’s own documentation. However, if you’ve never used Python’s logging framework (or even if you have), here’s a quick primer.</p> <div class="section" id="s-the-cast-of-players"> <span id="the-cast-of-players"></span><h3>The cast of players<a class="headerlink" href="#the-cast-of-players" title="Permalink to this headline">¶</a></h3> <p>A Python logging configuration consists of four parts:</p> <ul class="simple"> <li><a class="reference internal" href="#topic-logging-parts-loggers"><em>Loggers</em></a></li> <li><a class="reference internal" href="#topic-logging-parts-handlers"><em>Handlers</em></a></li> <li><a class="reference internal" href="#topic-logging-parts-filters"><em>Filters</em></a></li> <li><a class="reference internal" href="#topic-logging-parts-formatters"><em>Formatters</em></a></li> </ul> <div class="section" id="s-loggers"> <span id="s-topic-logging-parts-loggers"></span><span id="loggers"></span><span id="topic-logging-parts-loggers"></span><h4>Loggers<a class="headerlink" href="#loggers" title="Permalink to this headline">¶</a></h4> <p>A logger is the entry point into the logging system. Each logger is a named bucket to which messages can be written for processing.</p> <p>A logger is configured to have a <em>log level</em>. This log level describes the severity of the messages that the logger will handle. Python defines the following log levels:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">DEBUG</span></tt>: Low level system information for debugging purposes</li> <li><tt class="docutils literal"><span class="pre">INFO</span></tt>: General system information</li> <li><tt class="docutils literal"><span class="pre">WARNING</span></tt>: Information describing a minor problem that has occurred.</li> <li><tt class="docutils literal"><span class="pre">ERROR</span></tt>: Information describing a major problem that has occurred.</li> <li><tt class="docutils literal"><span class="pre">CRITICAL</span></tt>: Information describing a critical problem that has occurred.</li> </ul> <p>Each message that is written to the logger is a <em>Log Record</em>. Each log record also has a <em>log level</em> indicating the severity of that specific message. A log record can also contain useful metadata that describes the event that is being logged. This can include details such as a stack trace or an error code.</p> <p>When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.</p> <p>Once a logger has determined that a message needs to be processed, it is passed to a <em>Handler</em>.</p> </div> <div class="section" id="s-handlers"> <span id="s-topic-logging-parts-handlers"></span><span id="handlers"></span><span id="topic-logging-parts-handlers"></span><h4>Handlers<a class="headerlink" href="#handlers" title="Permalink to this headline">¶</a></h4> <p>The handler is the engine that determines what happens to each message in a logger. It describes a particular logging behavior, such as writing a message to the screen, to a file, or to a network socket.</p> <p>Like loggers, handlers also have a log level. If the log level of a log record doesn’t meet or exceed the level of the handler, the handler will ignore the message.</p> <p>A logger can have multiple handlers, and each handler can have a different log level. In this way, it is possible to provide different forms of notification depending on the importance of a message. For example, you could install one handler that forwards <tt class="docutils literal"><span class="pre">ERROR</span></tt> and <tt class="docutils literal"><span class="pre">CRITICAL</span></tt> messages to a paging service, while a second handler logs all messages (including <tt class="docutils literal"><span class="pre">ERROR</span></tt> and <tt class="docutils literal"><span class="pre">CRITICAL</span></tt> messages) to a file for later analysis.</p> </div> <div class="section" id="s-filters"> <span id="s-topic-logging-parts-filters"></span><span id="filters"></span><span id="topic-logging-parts-filters"></span><h4>Filters<a class="headerlink" href="#filters" title="Permalink to this headline">¶</a></h4> <p>A filter is used to provide additional control over which log records are passed from logger to handler.</p> <p>By default, any log message that meets log level requirements will be handled. However, by installing a filter, you can place additional criteria on the logging process. For example, you could install a filter that only allows <tt class="docutils literal"><span class="pre">ERROR</span></tt> messages from a particular source to be emitted.</p> <p>Filters can also be used to modify the logging record prior to being emitted. For example, you could write a filter that downgrades <tt class="docutils literal"><span class="pre">ERROR</span></tt> log records to <tt class="docutils literal"><span class="pre">WARNING</span></tt> records if a particular set of criteria are met.</p> <p>Filters can be installed on loggers or on handlers; multiple filters can be used in a chain to perform multiple filtering actions.</p> </div> <div class="section" id="s-formatters"> <span id="s-topic-logging-parts-formatters"></span><span id="formatters"></span><span id="topic-logging-parts-formatters"></span><h4>Formatters<a class="headerlink" href="#formatters" title="Permalink to this headline">¶</a></h4> <p>Ultimately, a log record needs to be rendered as text. Formatters describe the exact format of that text. A formatter usually consists of a Python formatting string; however, you can also write custom formatters to implement specific formatting behavior.</p> </div> </div> </div> <div class="section" id="s-using-logging"> <span id="using-logging"></span><h2>Using logging<a class="headerlink" href="#using-logging" title="Permalink to this headline">¶</a></h2> <p>Once you have configured your loggers, handlers, filters and formatters, you need to place logging calls into your code. Using the logging framework is very simple. Here’s an example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># import the logging library</span> <span class="kn">import</span> <span class="nn">logging</span> <span class="c"># Get an instance of a logger</span> <span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span> <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span> <span class="o">...</span> <span class="k">if</span> <span class="n">bad_mojo</span><span class="p">:</span> <span class="c"># Log an error message</span> <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">'Something went wrong!'</span><span class="p">)</span> </pre></div> </div> <p>And that’s it! Every time the <tt class="docutils literal"><span class="pre">bad_mojo</span></tt> condition is activated, an error log record will be written.</p> <div class="section" id="s-naming-loggers"> <span id="naming-loggers"></span><h3>Naming loggers<a class="headerlink" href="#naming-loggers" title="Permalink to this headline">¶</a></h3> <p>The call to <a class="reference external" href="http://docs.python.org/2.7/library/logging.html#logging.getLogger" title="(in Python v2.7)"><tt class="xref py py-func docutils literal"><span class="pre">logging.getLogger()</span></tt></a> obtains (creating, if necessary) an instance of a logger. The logger instance is identified by a name. This name is used to identify the logger for configuration purposes.</p> <p>By convention, the logger name is usually <tt class="docutils literal"><span class="pre">__name__</span></tt>, the name of the python module that contains the logger. This allows you to filter and handle logging calls on a per-module basis. However, if you have some other way of organizing your logging messages, you can provide any dot-separated name to identify your logger:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># Get an instance of a specific named logger</span> <span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">'project.interesting.stuff'</span><span class="p">)</span> </pre></div> </div> <p>The dotted paths of logger names define a hierarchy. The <tt class="docutils literal"><span class="pre">project.interesting</span></tt> logger is considered to be a parent of the <tt class="docutils literal"><span class="pre">project.interesting.stuff</span></tt> logger; the <tt class="docutils literal"><span class="pre">project</span></tt> logger is a parent of the <tt class="docutils literal"><span class="pre">project.interesting</span></tt> logger.</p> <p>Why is the hierarchy important? Well, because loggers can be set to <em>propagate</em> their logging calls to their parents. In this way, you can define a single set of handlers at the root of a logger tree, and capture all logging calls in the subtree of loggers. A logging handler defined in the <tt class="docutils literal"><span class="pre">project</span></tt> namespace will catch all logging messages issued on the <tt class="docutils literal"><span class="pre">project.interesting</span></tt> and <tt class="docutils literal"><span class="pre">project.interesting.stuff</span></tt> loggers.</p> <p>This propagation can be controlled on a per-logger basis. If you don’t want a particular logger to propagate to it’s parents, you can turn off this behavior.</p> </div> <div class="section" id="s-making-logging-calls"> <span id="making-logging-calls"></span><h3>Making logging calls<a class="headerlink" href="#making-logging-calls" title="Permalink to this headline">¶</a></h3> <p>The logger instance contains an entry method for each of the default log levels:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">logger.critical()</span></tt></li> <li><tt class="docutils literal"><span class="pre">logger.error()</span></tt></li> <li><tt class="docutils literal"><span class="pre">logger.warning()</span></tt></li> <li><tt class="docutils literal"><span class="pre">logger.info()</span></tt></li> <li><tt class="docutils literal"><span class="pre">logger.debug()</span></tt></li> </ul> <p>There are two other logging calls available:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">logger.log()</span></tt>: Manually emits a logging message with a specific log level.</li> <li><tt class="docutils literal"><span class="pre">logger.exception()</span></tt>: Creates an <tt class="docutils literal"><span class="pre">ERROR</span></tt> level logging message wrapping the current exception stack frame.</li> </ul> </div> </div> <div class="section" id="s-configuring-logging"> <span id="s-id1"></span><span id="configuring-logging"></span><span id="id1"></span><h2>Configuring logging<a class="headerlink" href="#configuring-logging" title="Permalink to this headline">¶</a></h2> <p>Of course, it isn’t enough to just put logging calls into your code. You also need to configure the loggers, handlers, filters and formatters to ensure that logging output is output in a useful way.</p> <p>Python’s logging library provides several techniques to configure logging, ranging from a programmatic interface to configuration files. By default, Django uses the <a class="reference external" href="http://docs.python.org/library/logging.config.html#configuration-dictionary-schema">dictConfig format</a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><tt class="docutils literal"><span class="pre">logging.dictConfig</span></tt> is a builtin library in Python 2.7. In order to make this library available for users of earlier Python versions, Django includes a copy as part of <tt class="docutils literal"><span class="pre">django.utils.log</span></tt>. If you have Python 2.7 or later, the system native library will be used; if you have Python 2.6, Django’s copy will be used.</p> </div> <p>In order to configure logging, you use <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING</span></tt></a> to define a dictionary of logging settings. These settings describes the loggers, handlers, filters and formatters that you want in your logging setup, and the log levels and other properties that you want those components to have.</p> <p>Prior to Django 1.5, the <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING</span></tt></a> setting always overwrote the <a class="reference internal" href="#default-logging-configuration"><em>default Django logging configuration</em></a>. From Django 1.5 forward, it is possible to get the project’s logging configuration merged with Django’s defaults, hence you can decide if you want to add to, or replace the existing configuration.</p> <p>If the <tt class="docutils literal"><span class="pre">disable_existing_loggers</span></tt> key in the <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING</span></tt></a> dictConfig is set to <tt class="docutils literal"><span class="pre">True</span></tt> (which is the default) the default configuration is completely overridden. Alternatively you can redefine some or all of the loggers by setting <tt class="docutils literal"><span class="pre">disable_existing_loggers</span></tt> to <tt class="docutils literal"><span class="pre">False</span></tt>.</p> <p>Logging is configured as soon as settings have been loaded (either manually using <a class="reference internal" href="settings.html#django.conf.settings.configure" title="django.conf.settings.configure"><tt class="xref py py-func docutils literal"><span class="pre">configure()</span></tt></a> or when at least one setting is accessed). Since the loading of settings is one of the first things that Django does, you can be certain that loggers are always ready for use in your project code.</p> <div class="section" id="s-an-example"> <span id="an-example"></span><h3>An example<a class="headerlink" href="#an-example" title="Permalink to this headline">¶</a></h3> <p>The full documentation for <a class="reference external" href="http://docs.python.org/library/logging.config.html#configuration-dictionary-schema">dictConfig format</a> is the best source of information about logging configuration dictionaries. However, to give you a taste of what is possible, here is an example of a fairly complex logging setup, configured using <a class="reference external" href="http://docs.python.org/2.7/library/logging.config.html#logging.config.dictConfig" title="(in Python v2.7)"><tt class="xref py py-func docutils literal"><span class="pre">logging.config.dictConfig()</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">LOGGING</span> <span class="o">=</span> <span class="p">{</span> <span class="s">'version'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'disable_existing_loggers'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="s">'formatters'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'verbose'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'format'</span><span class="p">:</span> <span class="s">'</span><span class="si">%(levelname)s</span><span class="s"> </span><span class="si">%(asctime)s</span><span class="s"> </span><span class="si">%(module)s</span><span class="s"> </span><span class="si">%(process)d</span><span class="s"> </span><span class="si">%(thread)d</span><span class="s"> </span><span class="si">%(message)s</span><span class="s">'</span> <span class="p">},</span> <span class="s">'simple'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'format'</span><span class="p">:</span> <span class="s">'</span><span class="si">%(levelname)s</span><span class="s"> </span><span class="si">%(message)s</span><span class="s">'</span> <span class="p">},</span> <span class="p">},</span> <span class="s">'filters'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'special'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'()'</span><span class="p">:</span> <span class="s">'project.logging.SpecialFilter'</span><span class="p">,</span> <span class="s">'foo'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span> <span class="s">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'null'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'level'</span><span class="p">:</span> <span class="s">'DEBUG'</span><span class="p">,</span> <span class="s">'class'</span><span class="p">:</span> <span class="s">'django.utils.log.NullHandler'</span><span class="p">,</span> <span class="p">},</span> <span class="s">'console'</span><span class="p">:{</span> <span class="s">'level'</span><span class="p">:</span> <span class="s">'DEBUG'</span><span class="p">,</span> <span class="s">'class'</span><span class="p">:</span> <span class="s">'logging.StreamHandler'</span><span class="p">,</span> <span class="s">'formatter'</span><span class="p">:</span> <span class="s">'simple'</span> <span class="p">},</span> <span class="s">'mail_admins'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'level'</span><span class="p">:</span> <span class="s">'ERROR'</span><span class="p">,</span> <span class="s">'class'</span><span class="p">:</span> <span class="s">'django.utils.log.AdminEmailHandler'</span><span class="p">,</span> <span class="s">'filters'</span><span class="p">:</span> <span class="p">[</span><span class="s">'special'</span><span class="p">]</span> <span class="p">}</span> <span class="p">},</span> <span class="s">'loggers'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'django'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s">'null'</span><span class="p">],</span> <span class="s">'propagate'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="s">'level'</span><span class="p">:</span> <span class="s">'INFO'</span><span class="p">,</span> <span class="p">},</span> <span class="s">'django.request'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s">'mail_admins'</span><span class="p">],</span> <span class="s">'level'</span><span class="p">:</span> <span class="s">'ERROR'</span><span class="p">,</span> <span class="s">'propagate'</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="p">},</span> <span class="s">'myproject.custom'</span><span class="p">:</span> <span class="p">{</span> <span class="s">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s">'console'</span><span class="p">,</span> <span class="s">'mail_admins'</span><span class="p">],</span> <span class="s">'level'</span><span class="p">:</span> <span class="s">'INFO'</span><span class="p">,</span> <span class="s">'filters'</span><span class="p">:</span> <span class="p">[</span><span class="s">'special'</span><span class="p">]</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </pre></div> </div> <p>This logging configuration does the following things:</p> <ul> <li><p class="first">Identifies the configuration as being in ‘dictConfig version 1’ format. At present, this is the only dictConfig format version.</p> </li> <li><p class="first">Disables all existing logging configurations.</p> </li> <li><p class="first">Defines two formatters:</p> <ul> <li><p class="first"><tt class="docutils literal"><span class="pre">simple</span></tt>, that just outputs the log level name (e.g., <tt class="docutils literal"><span class="pre">DEBUG</span></tt>) and the log message.</p> <p>The <tt class="docutils literal"><span class="pre">format</span></tt> string is a normal Python formatting string describing the details that are to be output on each logging line. The full list of detail that can be output can be found in the <a class="reference external" href="http://docs.python.org/library/logging.html#formatter-objects">formatter documentation</a>.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">verbose</span></tt>, that outputs the log level name, the log message, plus the time, process, thread and module that generate the log message.</p> </li> </ul> </li> <li><p class="first">Defines one filter – <tt class="docutils literal"><span class="pre">project.logging.SpecialFilter</span></tt>, using the alias <tt class="docutils literal"><span class="pre">special</span></tt>. If this filter required additional arguments at time of construction, they can be provided as additional keys in the filter configuration dictionary. In this case, the argument <tt class="docutils literal"><span class="pre">foo</span></tt> will be given a value of <tt class="docutils literal"><span class="pre">bar</span></tt> when instantiating the <tt class="docutils literal"><span class="pre">SpecialFilter</span></tt>.</p> </li> <li><p class="first">Defines three handlers:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">null</span></tt>, a NullHandler, which will pass any <tt class="docutils literal"><span class="pre">DEBUG</span></tt> (or higher) message to <tt class="docutils literal"><span class="pre">/dev/null</span></tt>.</li> <li><tt class="docutils literal"><span class="pre">console</span></tt>, a StreamHandler, which will print any <tt class="docutils literal"><span class="pre">DEBUG</span></tt> (or higher) message to stderr. This handler uses the <tt class="docutils literal"><span class="pre">simple</span></tt> output format.</li> <li><tt class="docutils literal"><span class="pre">mail_admins</span></tt>, an AdminEmailHandler, which will email any <tt class="docutils literal"><span class="pre">ERROR</span></tt> (or higher) message to the site admins. This handler uses the <tt class="docutils literal"><span class="pre">special</span></tt> filter.</li> </ul> </li> <li><p class="first">Configures three loggers:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">django</span></tt>, which passes all messages at <tt class="docutils literal"><span class="pre">INFO</span></tt> or higher to the <tt class="docutils literal"><span class="pre">null</span></tt> handler.</li> <li><tt class="docutils literal"><span class="pre">django.request</span></tt>, which passes all <tt class="docutils literal"><span class="pre">ERROR</span></tt> messages to the <tt class="docutils literal"><span class="pre">mail_admins</span></tt> handler. In addition, this logger is marked to <em>not</em> propagate messages. This means that log messages written to <tt class="docutils literal"><span class="pre">django.request</span></tt> will not be handled by the <tt class="docutils literal"><span class="pre">django</span></tt> logger.</li> <li><tt class="docutils literal"><span class="pre">myproject.custom</span></tt>, which passes all messages at <tt class="docutils literal"><span class="pre">INFO</span></tt> or higher that also pass the <tt class="docutils literal"><span class="pre">special</span></tt> filter to two handlers – the <tt class="docutils literal"><span class="pre">console</span></tt>, and <tt class="docutils literal"><span class="pre">mail_admins</span></tt>. This means that all <tt class="docutils literal"><span class="pre">INFO</span></tt> level messages (or higher) will be printed to the console; <tt class="docutils literal"><span class="pre">ERROR</span></tt> and <tt class="docutils literal"><span class="pre">CRITICAL</span></tt> messages will also be output via email.</li> </ul> </li> </ul> </div> <div class="section" id="s-custom-logging-configuration"> <span id="custom-logging-configuration"></span><h3>Custom logging configuration<a class="headerlink" href="#custom-logging-configuration" title="Permalink to this headline">¶</a></h3> <p>If you don’t want to use Python’s dictConfig format to configure your logger, you can specify your own configuration scheme.</p> <p>The <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING_CONFIG"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING_CONFIG</span></tt></a> setting defines the callable that will be used to configure Django’s loggers. By default, it points at Python’s <a class="reference external" href="http://docs.python.org/2.7/library/logging.config.html#logging.config.dictConfig" title="(in Python v2.7)"><tt class="xref py py-func docutils literal"><span class="pre">logging.config.dictConfig()</span></tt></a> function. However, if you want to use a different configuration process, you can use any other callable that takes a single argument. The contents of <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING</span></tt></a> will be provided as the value of that argument when logging is configured.</p> </div> <div class="section" id="s-disabling-logging-configuration"> <span id="disabling-logging-configuration"></span><h3>Disabling logging configuration<a class="headerlink" href="#disabling-logging-configuration" title="Permalink to this headline">¶</a></h3> <p>If you don’t want to configure logging at all (or you want to manually configure logging using your own approach), you can set <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING_CONFIG"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING_CONFIG</span></tt></a> to <tt class="docutils literal"><span class="pre">None</span></tt>. This will disable the configuration process.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Setting <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING_CONFIG"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING_CONFIG</span></tt></a> to <tt class="docutils literal"><span class="pre">None</span></tt> only means that the configuration process is disabled, not logging itself. If you disable the configuration process, Django will still make logging calls, falling back to whatever default logging behavior is defined.</p> </div> </div> </div> <div class="section" id="s-django-s-logging-extensions"> <span id="django-s-logging-extensions"></span><h2>Django’s logging extensions<a class="headerlink" href="#django-s-logging-extensions" title="Permalink to this headline">¶</a></h2> <p>Django provides a number of utilities to handle the unique requirements of logging in Web server environment.</p> <div class="section" id="s-id2"> <span id="id2"></span><h3>Loggers<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3> <p>Django provides three built-in loggers.</p> <div class="section" id="s-django"> <span id="django"></span><h4><tt class="docutils literal"><span class="pre">django</span></tt><a class="headerlink" href="#django" title="Permalink to this headline">¶</a></h4> <p><tt class="docutils literal"><span class="pre">django</span></tt> is the catch-all logger. No messages are posted directly to this logger.</p> </div> <div class="section" id="s-django-request"> <span id="django-request"></span><h4><tt class="docutils literal"><span class="pre">django.request</span></tt><a class="headerlink" href="#django-request" title="Permalink to this headline">¶</a></h4> <p>Log messages related to the handling of requests. 5XX responses are raised as <tt class="docutils literal"><span class="pre">ERROR</span></tt> messages; 4XX responses are raised as <tt class="docutils literal"><span class="pre">WARNING</span></tt> messages.</p> <p>Messages to this logger have the following extra context:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">status_code</span></tt>: The HTTP response code associated with the request.</li> <li><tt class="docutils literal"><span class="pre">request</span></tt>: The request object that generated the logging message.</li> </ul> </div> <div class="section" id="s-django-db-backends"> <span id="django-db-backends"></span><h4><tt class="docutils literal"><span class="pre">django.db.backends</span></tt><a class="headerlink" href="#django-db-backends" title="Permalink to this headline">¶</a></h4> <p>Messages relating to the interaction of code with the database. For example, every SQL statement executed by a request is logged at the <tt class="docutils literal"><span class="pre">DEBUG</span></tt> level to this logger.</p> <p>Messages to this logger have the following extra context:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">duration</span></tt>: The time taken to execute the SQL statement.</li> <li><tt class="docutils literal"><span class="pre">sql</span></tt>: The SQL statement that was executed.</li> <li><tt class="docutils literal"><span class="pre">params</span></tt>: The parameters that were used in the SQL call.</li> </ul> <p>For performance reasons, SQL logging is only enabled when <tt class="docutils literal"><span class="pre">settings.DEBUG</span></tt> is set to <tt class="docutils literal"><span class="pre">True</span></tt>, regardless of the logging level or handlers that are installed.</p> </div> </div> <div class="section" id="s-id3"> <span id="id3"></span><h3>Handlers<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3> <p>Django provides one log handler in addition to those provided by the Python logging module.</p> <dl class="class"> <dt id="django.utils.log.AdminEmailHandler"> <em class="property">class </em><tt class="descname">AdminEmailHandler</tt>(<span class="optional">[</span><em>include_html=False</em><span class="optional">]</span>)<a class="headerlink" href="#django.utils.log.AdminEmailHandler" title="Permalink to this definition">¶</a></dt> <dd><p>This handler sends an email to the site admins for each log message it receives.</p> <p>If the log record contains a <tt class="docutils literal"><span class="pre">request</span></tt> attribute, the full details of the request will be included in the email.</p> <p>If the log record contains stack trace information, that stack trace will be included in the email.</p> <p>The <tt class="docutils literal"><span class="pre">include_html</span></tt> argument of <tt class="docutils literal"><span class="pre">AdminEmailHandler</span></tt> is used to control whether the traceback email includes an HTML attachment containing the full content of the debug Web page that would have been produced if <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> were <tt class="docutils literal"><span class="pre">True</span></tt>. To set this value in your configuration, include it in the handler definition for <tt class="docutils literal"><span class="pre">django.utils.log.AdminEmailHandler</span></tt>, like this:</p> <div class="highlight-python"><pre>'handlers': { 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', 'include_html': True, } },</pre> </div> <p>Note that this HTML version of the email contains a full traceback, with names and values of local variables at each level of the stack, plus the values of your Django settings. This information is potentially very sensitive, and you may not want to send it over email. Consider using something such as <a class="reference external" href="http://pypi.python.org/pypi/sentry">Sentry</a> to get the best of both worlds – the rich information of full tracebacks plus the security of <em>not</em> sending the information over email. You may also explicitly designate certain sensitive information to be filtered out of error reports – learn more on <a class="reference internal" href="../howto/error-reporting.html#filtering-error-reports"><em>Filtering error reports</em></a>.</p> </dd></dl> </div> <div class="section" id="s-id4"> <span id="id4"></span><h3>Filters<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h3> <p>Django provides two log filters in addition to those provided by the Python logging module.</p> <dl class="class"> <dt id="django.utils.log.CallbackFilter"> <em class="property">class </em><tt class="descname">CallbackFilter</tt>(<em>callback</em>)<a class="headerlink" href="#django.utils.log.CallbackFilter" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.4.</span> </div> <p>This filter accepts a callback function (which should accept a single argument, the record to be logged), and calls it for each record that passes through the filter. Handling of that record will not proceed if the callback returns False.</p> <p>For instance, to filter out <a class="reference internal" href="../ref/exceptions.html#django.http.UnreadablePostError" title="django.http.UnreadablePostError"><tt class="xref py py-exc docutils literal"><span class="pre">UnreadablePostError</span></tt></a> (raised when a user cancels an upload) from the admin emails, you would create a filter function:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">UnreadablePostError</span> <span class="k">def</span> <span class="nf">skip_unreadable_post</span><span class="p">(</span><span class="n">record</span><span class="p">):</span> <span class="k">if</span> <span class="n">record</span><span class="o">.</span><span class="n">exc_info</span><span class="p">:</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span> <span class="o">=</span> <span class="n">record</span><span class="o">.</span><span class="n">exc_info</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">exc_value</span><span class="p">,</span> <span class="n">UnreadablePostError</span><span class="p">):</span> <span class="k">return</span> <span class="bp">False</span> <span class="k">return</span> <span class="bp">True</span> </pre></div> </div> <p>and then add it to your logging config:</p> <div class="highlight-python"><pre>'filters': { 'skip_unreadable_posts': { '()': 'django.utils.log.CallbackFilter', 'callback': skip_unreadable_post, } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['skip_unreadable_posts'], 'class': 'django.utils.log.AdminEmailHandler' } },</pre> </div> </dd></dl> <dl class="class"> <dt id="django.utils.log.RequireDebugFalse"> <em class="property">class </em><tt class="descname">RequireDebugFalse</tt><a class="headerlink" href="#django.utils.log.RequireDebugFalse" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.4.</span> </div> <p>This filter will only pass on records when settings.DEBUG is False.</p> <p>This filter is used as follows in the default <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><tt class="xref std std-setting docutils literal"><span class="pre">LOGGING</span></tt></a> configuration to ensure that the <a class="reference internal" href="#django.utils.log.AdminEmailHandler" title="django.utils.log.AdminEmailHandler"><tt class="xref py py-class docutils literal"><span class="pre">AdminEmailHandler</span></tt></a> only sends error emails to admins when <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> is <tt class="docutils literal"><span class="pre">False</span></tt>:</p> <div class="highlight-python"><pre>'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' } },</pre> </div> </dd></dl> <dl class="class"> <dt id="django.utils.log.RequireDebugTrue"> <em class="property">class </em><tt class="descname">RequireDebugTrue</tt><a class="headerlink" href="#django.utils.log.RequireDebugTrue" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.5.</span> </div> <p>This filter is similar to <a class="reference internal" href="#django.utils.log.RequireDebugFalse" title="django.utils.log.RequireDebugFalse"><tt class="xref py py-class docutils literal"><span class="pre">RequireDebugFalse</span></tt></a>, except that records are passed only when <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> is <tt class="docutils literal"><span class="pre">True</span></tt>.</p> </dd></dl> </div> </div> <div class="section" id="s-django-s-default-logging-configuration"> <span id="s-default-logging-configuration"></span><span id="django-s-default-logging-configuration"></span><span id="default-logging-configuration"></span><h2>Django’s default logging configuration<a class="headerlink" href="#django-s-default-logging-configuration" title="Permalink to this headline">¶</a></h2> <p>By default, Django configures the <tt class="docutils literal"><span class="pre">django.request</span></tt> logger so that all messages with <tt class="docutils literal"><span class="pre">ERROR</span></tt> or <tt class="docutils literal"><span class="pre">CRITICAL</span></tt> level are sent to <a class="reference internal" href="#django.utils.log.AdminEmailHandler" title="django.utils.log.AdminEmailHandler"><tt class="xref py py-class docutils literal"><span class="pre">AdminEmailHandler</span></tt></a>, as long as the <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> setting is set to <tt class="docutils literal"><span class="pre">False</span></tt>.</p> <p>All messages reaching the <tt class="docutils literal"><span class="pre">django</span></tt> catch-all logger when <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> is <tt class="docutils literal"><span class="pre">True</span></tt> are sent to the console. They are simply discarded (sent to <tt class="docutils literal"><span class="pre">NullHandler</span></tt>) when <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> is <tt class="docutils literal"><span class="pre">False</span></tt>.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.5.</span> </div> <p>See also <a class="reference internal" href="#configuring-logging"><em>Configuring logging</em></a> to learn how you can complement or replace this default logging configuration.</p> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Logging</a><ul> <li><a class="reference internal" href="#a-quick-logging-primer">A quick logging primer</a><ul> <li><a class="reference internal" href="#the-cast-of-players">The cast of players</a><ul> <li><a class="reference internal" href="#loggers">Loggers</a></li> <li><a class="reference internal" href="#handlers">Handlers</a></li> <li><a class="reference internal" href="#filters">Filters</a></li> <li><a class="reference internal" href="#formatters">Formatters</a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#using-logging">Using logging</a><ul> <li><a class="reference internal" href="#naming-loggers">Naming loggers</a></li> <li><a class="reference internal" href="#making-logging-calls">Making logging calls</a></li> </ul> </li> <li><a class="reference internal" href="#configuring-logging">Configuring logging</a><ul> <li><a class="reference internal" href="#an-example">An example</a></li> <li><a class="reference internal" href="#custom-logging-configuration">Custom logging configuration</a></li> <li><a class="reference internal" href="#disabling-logging-configuration">Disabling logging configuration</a></li> </ul> </li> <li><a class="reference internal" href="#django-s-logging-extensions">Django’s logging extensions</a><ul> <li><a class="reference internal" href="#id2">Loggers</a><ul> <li><a class="reference internal" href="#django"><tt class="docutils literal"><span class="pre">django</span></tt></a></li> <li><a class="reference internal" href="#django-request"><tt class="docutils literal"><span class="pre">django.request</span></tt></a></li> <li><a class="reference internal" href="#django-db-backends"><tt class="docutils literal"><span class="pre">django.db.backends</span></tt></a></li> </ul> </li> <li><a class="reference internal" href="#id3">Handlers</a></li> <li><a class="reference internal" href="#id4">Filters</a></li> </ul> </li> <li><a class="reference internal" href="#django-s-default-logging-configuration">Django’s default logging configuration</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="i18n/timezones.html">Time zones</a></li> <li>Next: <a href="pagination.html">Pagination</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../index.html">Django 1.5.9 documentation</a> <ul><li><a href="index.html">Using Django</a> <ul><li>Logging</li></ul> </li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/topics/logging.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Aug 21, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="i18n/timezones.html" title="Time zones">previous</a> | <a href="index.html" title="Using Django" accesskey="U">up</a> | <a href="pagination.html" title="Pagination">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>