<!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" lang=""> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Logging — Django 1.11.20 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" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></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> <script type="text/javascript" src="../_static/language_data.js"></script> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.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.11.20 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 <code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> 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"><span class="std std-ref">Loggers</span></a></li> <li><a class="reference internal" href="#topic-logging-parts-handlers"><span class="std std-ref">Handlers</span></a></li> <li><a class="reference internal" href="#topic-logging-parts-filters"><span class="std std-ref">Filters</span></a></li> <li><a class="reference internal" href="#topic-logging-parts-formatters"><span class="std std-ref">Formatters</span></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><code class="docutils literal notranslate"><span class="pre">DEBUG</span></code>: Low level system information for debugging purposes</li> <li><code class="docutils literal notranslate"><span class="pre">INFO</span></code>: General system information</li> <li><code class="docutils literal notranslate"><span class="pre">WARNING</span></code>: Information describing a minor problem that has occurred.</li> <li><code class="docutils literal notranslate"><span class="pre">ERROR</span></code>: Information describing a major problem that has occurred.</li> <li><code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code>: 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 <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> and <code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code> messages to a paging service, while a second handler logs all messages (including <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> and <code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code> 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 <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> 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 <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> log records to <code class="docutils literal notranslate"><span class="pre">WARNING</span></code> 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 containing <span class="xref std std-ref">LogRecord attributes</span>; 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-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># import the logging library</span> <span class="kn">import</span> <span class="nn">logging</span> <span class="c1"># 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="vm">__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="c1"># 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="s1">'Something went wrong!'</span><span class="p">)</span> </pre></div> </div> <p>And that’s it! Every time the <code class="docutils literal notranslate"><span class="pre">bad_mojo</span></code> 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 <code class="xref py py-func docutils literal notranslate"><span class="pre">logging.getLogger()</span></code> 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 <code class="docutils literal notranslate"><span class="pre">__name__</span></code>, 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-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># 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="s1">'project.interesting.stuff'</span><span class="p">)</span> </pre></div> </div> <p>The dotted paths of logger names define a hierarchy. The <code class="docutils literal notranslate"><span class="pre">project.interesting</span></code> logger is considered to be a parent of the <code class="docutils literal notranslate"><span class="pre">project.interesting.stuff</span></code> logger; the <code class="docutils literal notranslate"><span class="pre">project</span></code> logger is a parent of the <code class="docutils literal notranslate"><span class="pre">project.interesting</span></code> 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 <code class="docutils literal notranslate"><span class="pre">project</span></code> namespace will catch all logging messages issued on the <code class="docutils literal notranslate"><span class="pre">project.interesting</span></code> and <code class="docutils literal notranslate"><span class="pre">project.interesting.stuff</span></code> loggers.</p> <p>This propagation can be controlled on a per-logger basis. If you don’t want a particular logger to propagate to its 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><code class="docutils literal notranslate"><span class="pre">logger.debug()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">logger.info()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">logger.warning()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">logger.error()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">logger.critical()</span></code></li> </ul> <p>There are two other logging calls available:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">logger.log()</span></code>: Manually emits a logging message with a specific log level.</li> <li><code class="docutils literal notranslate"><span class="pre">logger.exception()</span></code>: Creates an <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> 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 <span class="xref std std-ref">dictConfig format</span>.</p> <p>In order to configure logging, you use <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING</span></code></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>By default, the <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING</span></code></a> setting is merged with <a class="reference internal" href="#default-logging-configuration"><span class="std std-ref">Django’s default logging configuration</span></a> using the following scheme.</p> <p>If the <code class="docutils literal notranslate"><span class="pre">disable_existing_loggers</span></code> key in the <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING</span></code></a> dictConfig is set to <code class="docutils literal notranslate"><span class="pre">True</span></code> (which is the default) then all loggers from the default configuration will be disabled. Disabled loggers are not the same as removed; the logger will still exist, but will silently discard anything logged to it, not even propagating entries to a parent logger. Thus you should be very careful using <code class="docutils literal notranslate"><span class="pre">'disable_existing_loggers':</span> <span class="pre">True</span></code>; it’s probably not what you want. Instead, you can set <code class="docutils literal notranslate"><span class="pre">disable_existing_loggers</span></code> to <code class="docutils literal notranslate"><span class="pre">False</span></code> and redefine some or all of the default loggers; or you can set <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING_CONFIG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING_CONFIG</span></code></a> to <code class="docutils literal notranslate"><span class="pre">None</span></code> and <a class="reference internal" href="#disabling-logging-configuration"><span class="std std-ref">handle logging config yourself</span></a>.</p> <p>Logging is configured as part of the general Django <code class="docutils literal notranslate"><span class="pre">setup()</span></code> function. Therefore, you can be certain that loggers are always ready for use in your project code.</p> <div class="section" id="s-examples"> <span id="examples"></span><h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3> <p>The full documentation for <span class="xref std std-ref">dictConfig format</span> is the best source of information about logging configuration dictionaries. However, to give you a taste of what is possible, here are several examples.</p> <p>First, here’s a simple configuration which writes all logging from the <a class="reference internal" href="#django-logger"><span class="std std-ref">django</span></a> logger to a local file:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">LOGGING</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">'version'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'disable_existing_loggers'</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'file'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'DEBUG'</span><span class="p">,</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'logging.FileHandler'</span><span class="p">,</span> <span class="s1">'filename'</span><span class="p">:</span> <span class="s1">'/path/to/django/debug.log'</span><span class="p">,</span> <span class="p">},</span> <span class="p">},</span> <span class="s1">'loggers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'django'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'file'</span><span class="p">],</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'DEBUG'</span><span class="p">,</span> <span class="s1">'propagate'</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="p">},</span> <span class="p">},</span> <span class="p">}</span> </pre></div> </div> <p>If you use this example, be sure to change the <code class="docutils literal notranslate"><span class="pre">'filename'</span></code> path to a location that’s writable by the user that’s running the Django application.</p> <p>Second, here’s an example of how to make the logging system print Django’s logging to the console. It may be useful during local development.</p> <p>By default, this config only sends messages of level <code class="docutils literal notranslate"><span class="pre">INFO</span></code> or higher to the console (same as Django’s default logging config, except that the default only displays log records when <code class="docutils literal notranslate"><span class="pre">DEBUG=True</span></code>). Django does not log many such messages. With this config, however, you can also set the environment variable <code class="docutils literal notranslate"><span class="pre">DJANGO_LOG_LEVEL=DEBUG</span></code> to see all of Django’s debug logging which is very verbose as it includes all database queries:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span> <span class="n">LOGGING</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">'version'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'disable_existing_loggers'</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'console'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'logging.StreamHandler'</span><span class="p">,</span> <span class="p">},</span> <span class="p">},</span> <span class="s1">'loggers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'django'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'console'</span><span class="p">],</span> <span class="s1">'level'</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s1">'DJANGO_LOG_LEVEL'</span><span class="p">,</span> <span class="s1">'INFO'</span><span class="p">),</span> <span class="p">},</span> <span class="p">},</span> <span class="p">}</span> </pre></div> </div> <p>Finally, here’s an example of a fairly complex logging setup:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">LOGGING</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">'version'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'disable_existing_loggers'</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> <span class="s1">'formatters'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'verbose'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'format'</span><span class="p">:</span> <span class="s1">'</span><span class="si">%(levelname)s</span><span class="s1"> </span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(module)s</span><span class="s1"> </span><span class="si">%(process)d</span><span class="s1"> </span><span class="si">%(thread)d</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span> <span class="p">},</span> <span class="s1">'simple'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'format'</span><span class="p">:</span> <span class="s1">'</span><span class="si">%(levelname)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span> <span class="p">},</span> <span class="p">},</span> <span class="s1">'filters'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'special'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'()'</span><span class="p">:</span> <span class="s1">'project.logging.SpecialFilter'</span><span class="p">,</span> <span class="s1">'foo'</span><span class="p">:</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="p">},</span> <span class="s1">'require_debug_true'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'()'</span><span class="p">:</span> <span class="s1">'django.utils.log.RequireDebugTrue'</span><span class="p">,</span> <span class="p">},</span> <span class="p">},</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'console'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'INFO'</span><span class="p">,</span> <span class="s1">'filters'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'require_debug_true'</span><span class="p">],</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'logging.StreamHandler'</span><span class="p">,</span> <span class="s1">'formatter'</span><span class="p">:</span> <span class="s1">'simple'</span> <span class="p">},</span> <span class="s1">'mail_admins'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'ERROR'</span><span class="p">,</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'django.utils.log.AdminEmailHandler'</span><span class="p">,</span> <span class="s1">'filters'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'special'</span><span class="p">]</span> <span class="p">}</span> <span class="p">},</span> <span class="s1">'loggers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'django'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'console'</span><span class="p">],</span> <span class="s1">'propagate'</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="p">},</span> <span class="s1">'django.request'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'mail_admins'</span><span class="p">],</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'ERROR'</span><span class="p">,</span> <span class="s1">'propagate'</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> <span class="p">},</span> <span class="s1">'myproject.custom'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'console'</span><span class="p">,</span> <span class="s1">'mail_admins'</span><span class="p">],</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'INFO'</span><span class="p">,</span> <span class="s1">'filters'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'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">Defines two formatters:</p> <ul> <li><p class="first"><code class="docutils literal notranslate"><span class="pre">simple</span></code>, that just outputs the log level name (e.g., <code class="docutils literal notranslate"><span class="pre">DEBUG</span></code>) and the log message.</p> <p>The <code class="docutils literal notranslate"><span class="pre">format</span></code> 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 <span class="xref std std-ref">formatter-objects</span>.</p> </li> <li><p class="first"><code class="docutils literal notranslate"><span class="pre">verbose</span></code>, 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 two filters:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">project.logging.SpecialFilter</span></code>, using the alias <code class="docutils literal notranslate"><span class="pre">special</span></code>. If this filter required additional arguments, they can be provided as additional keys in the filter configuration dictionary. In this case, the argument <code class="docutils literal notranslate"><span class="pre">foo</span></code> will be given a value of <code class="docutils literal notranslate"><span class="pre">bar</span></code> when instantiating <code class="docutils literal notranslate"><span class="pre">SpecialFilter</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">django.utils.log.RequireDebugTrue</span></code>, which passes on records when <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</li> </ul> </li> <li><p class="first">Defines two handlers:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">console</span></code>, a StreamHandler, which will print any <code class="docutils literal notranslate"><span class="pre">INFO</span></code> (or higher) message to stderr. This handler uses the <code class="docutils literal notranslate"><span class="pre">simple</span></code> output format.</li> <li><code class="docutils literal notranslate"><span class="pre">mail_admins</span></code>, an AdminEmailHandler, which will email any <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> (or higher) message to the site admins. This handler uses the <code class="docutils literal notranslate"><span class="pre">special</span></code> filter.</li> </ul> </li> <li><p class="first">Configures three loggers:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">django</span></code>, which passes all messages to the <code class="docutils literal notranslate"><span class="pre">console</span></code> handler.</li> <li><code class="docutils literal notranslate"><span class="pre">django.request</span></code>, which passes all <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> messages to the <code class="docutils literal notranslate"><span class="pre">mail_admins</span></code> handler. In addition, this logger is marked to <em>not</em> propagate messages. This means that log messages written to <code class="docutils literal notranslate"><span class="pre">django.request</span></code> will not be handled by the <code class="docutils literal notranslate"><span class="pre">django</span></code> logger.</li> <li><code class="docutils literal notranslate"><span class="pre">myproject.custom</span></code>, which passes all messages at <code class="docutils literal notranslate"><span class="pre">INFO</span></code> or higher that also pass the <code class="docutils literal notranslate"><span class="pre">special</span></code> filter to two handlers – the <code class="docutils literal notranslate"><span class="pre">console</span></code>, and <code class="docutils literal notranslate"><span class="pre">mail_admins</span></code>. This means that all <code class="docutils literal notranslate"><span class="pre">INFO</span></code> level messages (or higher) will be printed to the console; <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> and <code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code> 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"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING_CONFIG</span></code></a> setting defines the callable that will be used to configure Django’s loggers. By default, it points at Python’s <code class="xref py py-func docutils literal notranslate"><span class="pre">logging.config.dictConfig()</span></code> 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"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING</span></code></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="s-id2"></span><span id="disabling-logging-configuration"></span><span id="id2"></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"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING_CONFIG</span></code></a> to <code class="docutils literal notranslate"><span class="pre">None</span></code>. This will disable the configuration process for <a class="reference internal" href="#default-logging-configuration"><span class="std std-ref">Django’s default logging</span></a>. Here’s an example that disables Django’s logging configuration and then manually configures logging:</p> <div class="highlight-default snippet"><div class="snippet-filename">settings.py</div> <div class="highlight"><pre><span></span><span class="n">LOGGING_CONFIG</span> <span class="o">=</span> <span class="kc">None</span> <span class="kn">import</span> <span class="nn">logging.config</span> <span class="n">logging</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">dictConfig</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> </pre></div> </div> <p>Setting <a class="reference internal" href="../ref/settings.html#std:setting-LOGGING_CONFIG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING_CONFIG</span></code></a> to <code class="docutils literal notranslate"><span class="pre">None</span></code> only means that the automatic 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 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-id3"> <span id="id3"></span><h3>Loggers<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3> <p>Django provides several built-in loggers.</p> <div class="section" id="s-django"> <span id="s-django-logger"></span><span id="django"></span><span id="django-logger"></span><h4><code class="docutils literal notranslate"><span class="pre">django</span></code><a class="headerlink" href="#django" title="Permalink to this headline">¶</a></h4> <p>The catch-all logger for messages in the <code class="docutils literal notranslate"><span class="pre">django</span></code> hierarchy. No messages are posted using this name but instead using one of the loggers below.</p> </div> <div class="section" id="s-django-request"> <span id="s-django-request-logger"></span><span id="django-request"></span><span id="django-request-logger"></span><h4><code class="docutils literal notranslate"><span class="pre">django.request</span></code><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 <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> messages; 4XX responses are raised as <code class="docutils literal notranslate"><span class="pre">WARNING</span></code> messages.</p> <p>Messages to this logger have the following extra context:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">status_code</span></code>: The HTTP response code associated with the request.</li> <li><code class="docutils literal notranslate"><span class="pre">request</span></code>: The request object that generated the logging message.</li> </ul> </div> <div class="section" id="s-django-server"> <span id="s-django-server-logger"></span><span id="django-server"></span><span id="django-server-logger"></span><h4><code class="docutils literal notranslate"><span class="pre">django.server</span></code><a class="headerlink" href="#django-server" title="Permalink to this headline">¶</a></h4> <div class="versionadded"> <span class="title">New in Django 1.10.</span> </div> <p>Log messages related to the handling of requests received by the server invoked by the <a class="reference internal" href="../ref/django-admin.html#django-admin-runserver"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">runserver</span></code></a> command. HTTP 5XX responses are logged as <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> messages, 4XX responses are logged as <code class="docutils literal notranslate"><span class="pre">WARNING</span></code> messages, and everything else is logged as <code class="docutils literal notranslate"><span class="pre">INFO</span></code>.</p> <p>Messages to this logger have the following extra context:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">status_code</span></code>: The HTTP response code associated with the request.</li> <li><code class="docutils literal notranslate"><span class="pre">request</span></code>: The request object that generated the logging message.</li> </ul> </div> <div class="section" id="s-django-template"> <span id="s-django-template-logger"></span><span id="django-template"></span><span id="django-template-logger"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template</span></code><a class="headerlink" href="#django-template" title="Permalink to this headline">¶</a></h4> <p>Log messages related to the rendering of templates.</p> <ul class="simple"> <li>Missing context variables are logged as <code class="docutils literal notranslate"><span class="pre">DEBUG</span></code> messages.</li> <li>Uncaught exceptions raised during the rendering of an <a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-include"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">{%</span> <span class="pre">include</span> <span class="pre">%}</span></code></a> are logged as <code class="docutils literal notranslate"><span class="pre">WARNING</span></code> messages when debug mode is off (helpful since <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">include</span> <span class="pre">%}</span></code> silences the exception and returns an empty string in that case).</li> </ul> </div> <div class="section" id="s-django-db-backends"> <span id="s-django-db-logger"></span><span id="django-db-backends"></span><span id="django-db-logger"></span><h4><code class="docutils literal notranslate"><span class="pre">django.db.backends</span></code><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 application-level SQL statement executed by a request is logged at the <code class="docutils literal notranslate"><span class="pre">DEBUG</span></code> level to this logger.</p> <p>Messages to this logger have the following extra context:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">duration</span></code>: The time taken to execute the SQL statement.</li> <li><code class="docutils literal notranslate"><span class="pre">sql</span></code>: The SQL statement that was executed.</li> <li><code class="docutils literal notranslate"><span class="pre">params</span></code>: The parameters that were used in the SQL call.</li> </ul> <p>For performance reasons, SQL logging is only enabled when <code class="docutils literal notranslate"><span class="pre">settings.DEBUG</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, regardless of the logging level or handlers that are installed.</p> <p>This logging does not include framework-level initialization (e.g. <code class="docutils literal notranslate"><span class="pre">SET</span> <span class="pre">TIMEZONE</span></code>) or transaction management queries (e.g. <code class="docutils literal notranslate"><span class="pre">BEGIN</span></code>, <code class="docutils literal notranslate"><span class="pre">COMMIT</span></code>, and <code class="docutils literal notranslate"><span class="pre">ROLLBACK</span></code>). Turn on query logging in your database if you wish to view all database queries.</p> </div> <div class="section" id="s-django-security"> <span id="s-django-security-logger"></span><span id="django-security"></span><span id="django-security-logger"></span><h4><code class="docutils literal notranslate"><span class="pre">django.security.*</span></code><a class="headerlink" href="#django-security" title="Permalink to this headline">¶</a></h4> <p>The security loggers will receive messages on any occurrence of <a class="reference internal" href="../ref/exceptions.html#django.core.exceptions.SuspiciousOperation" title="django.core.exceptions.SuspiciousOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code></a> and other security-related errors. There is a sub-logger for each subtype of security error, including all <code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code>s. The level of the log event depends on where the exception is handled. Most occurrences are logged as a warning, while any <code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code> that reaches the WSGI handler will be logged as an error. For example, when an HTTP <code class="docutils literal notranslate"><span class="pre">Host</span></code> header is included in a request from a client that does not match <a class="reference internal" href="../ref/settings.html#std:setting-ALLOWED_HOSTS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">ALLOWED_HOSTS</span></code></a>, Django will return a 400 response, and an error message will be logged to the <code class="docutils literal notranslate"><span class="pre">django.security.DisallowedHost</span></code> logger.</p> <p>These log events will reach the <code class="docutils literal notranslate"><span class="pre">django</span></code> logger by default, which mails error events to admins when <code class="docutils literal notranslate"><span class="pre">DEBUG=False</span></code>. Requests resulting in a 400 response due to a <code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code> will not be logged to the <code class="docutils literal notranslate"><span class="pre">django.request</span></code> logger, but only to the <code class="docutils literal notranslate"><span class="pre">django.security</span></code> logger.</p> <p>To silence a particular type of <code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code>, you can override that specific logger following this example:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'null'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'logging.NullHandler'</span><span class="p">,</span> <span class="p">},</span> <span class="p">},</span> <span class="s1">'loggers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'django.security.DisallowedHost'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'null'</span><span class="p">],</span> <span class="s1">'propagate'</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="p">},</span> <span class="p">},</span> </pre></div> </div> <p>Other <code class="docutils literal notranslate"><span class="pre">django.security</span></code> loggers not based on <code class="docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code> are:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">django.security.csrf</span></code>: For <a class="reference internal" href="../ref/csrf.html#csrf-rejected-requests"><span class="std std-ref">CSRF failures</span></a>.</li> </ul> </div> <div class="section" id="s-django-db-backends-schema"> <span id="django-db-backends-schema"></span><h4><code class="docutils literal notranslate"><span class="pre">django.db.backends.schema</span></code><a class="headerlink" href="#django-db-backends-schema" title="Permalink to this headline">¶</a></h4> <p>Logs the SQL queries that are executed during schema changes to the database by the <a class="reference internal" href="migrations.html"><span class="doc">migrations framework</span></a>. Note that it won’t log the queries executed by <a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal notranslate"><span class="pre">RunPython</span></code></a>. Messages to this logger have <code class="docutils literal notranslate"><span class="pre">params</span></code> and <code class="docutils literal notranslate"><span class="pre">sql</span></code> in their extra context (but unlike <code class="docutils literal notranslate"><span class="pre">django.db.backends</span></code>, not duration). The values have the same meaning as explained in <a class="reference internal" href="#django-db-logger"><span class="std std-ref">django.db.backends</span></a>.</p> <div class="versionadded"> <span class="title">New in Django 1.10:</span> <p>The <code class="docutils literal notranslate"><span class="pre">extra</span></code> context was added.</p> </div> </div> </div> <div class="section" id="s-id4"> <span id="id4"></span><h3>Handlers<a class="headerlink" href="#id4" 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><code class="descname">AdminEmailHandler</code>(<em>include_html=False</em>, <em>email_backend=None</em>)<a class="reference internal" href="../_modules/django/utils/log.html#AdminEmailHandler"><span class="viewcode-link">[source]</span></a><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 <code class="docutils literal notranslate"><span class="pre">request</span></code> attribute, the full details of the request will be included in the email. The email subject will include the phrase “internal IP” if the client’s IP address is in the <a class="reference internal" href="../ref/settings.html#std:setting-INTERNAL_IPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INTERNAL_IPS</span></code></a> setting; if not, it will include “EXTERNAL IP”.</p> <p>If the log record contains stack trace information, that stack trace will be included in the email.</p> <p>The <code class="docutils literal notranslate"><span class="pre">include_html</span></code> argument of <code class="docutils literal notranslate"><span class="pre">AdminEmailHandler</span></code> 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"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> were <code class="docutils literal notranslate"><span class="pre">True</span></code>. To set this value in your configuration, include it in the handler definition for <code class="docutils literal notranslate"><span class="pre">django.utils.log.AdminEmailHandler</span></code>, like this:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'mail_admins'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'ERROR'</span><span class="p">,</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'django.utils.log.AdminEmailHandler'</span><span class="p">,</span> <span class="s1">'include_html'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span> </pre></div> </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="https://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"><span class="std std-ref">Filtering error reports</span></a>.</p> <p>By setting the <code class="docutils literal notranslate"><span class="pre">email_backend</span></code> argument of <code class="docutils literal notranslate"><span class="pre">AdminEmailHandler</span></code>, the <a class="reference internal" href="email.html#topic-email-backends"><span class="std std-ref">email backend</span></a> that is being used by the handler can be overridden, like this:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'mail_admins'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'ERROR'</span><span class="p">,</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'django.utils.log.AdminEmailHandler'</span><span class="p">,</span> <span class="s1">'email_backend'</span><span class="p">:</span> <span class="s1">'django.core.mail.backends.filebased.EmailBackend'</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span> </pre></div> </div> <p>By default, an instance of the email backend specified in <a class="reference internal" href="../ref/settings.html#std:setting-EMAIL_BACKEND"><code class="xref std std-setting docutils literal notranslate"><span class="pre">EMAIL_BACKEND</span></code></a> will be used.</p> <dl class="method"> <dt id="django.utils.log.AdminEmailHandler.send_mail"> <code class="descname">send_mail</code>(<em>subject</em>, <em>message</em>, <em>*args</em>, <em>**kwargs</em>)<a class="reference internal" href="../_modules/django/utils/log.html#AdminEmailHandler.send_mail"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.log.AdminEmailHandler.send_mail" title="Permalink to this definition">¶</a></dt> <dd><p>Sends emails to admin users. To customize this behavior, you can subclass the <a class="reference internal" href="#django.utils.log.AdminEmailHandler" title="django.utils.log.AdminEmailHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminEmailHandler</span></code></a> class and override this method.</p> </dd></dl> </dd></dl> </div> <div class="section" id="s-id5"> <span id="id5"></span><h3>Filters<a class="headerlink" href="#id5" 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><code class="descname">CallbackFilter</code>(<em>callback</em>)<a class="reference internal" href="../_modules/django/utils/log.html#CallbackFilter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.log.CallbackFilter" title="Permalink to this definition">¶</a></dt> <dd><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"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnreadablePostError</span></code></a> (raised when a user cancels an upload) from the admin emails, you would create a filter function:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">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="kc">False</span> <span class="k">return</span> <span class="kc">True</span> </pre></div> </div> <p>and then add it to your logging config:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'filters'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'skip_unreadable_posts'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'()'</span><span class="p">:</span> <span class="s1">'django.utils.log.CallbackFilter'</span><span class="p">,</span> <span class="s1">'callback'</span><span class="p">:</span> <span class="n">skip_unreadable_post</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'mail_admins'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'ERROR'</span><span class="p">,</span> <span class="s1">'filters'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'skip_unreadable_posts'</span><span class="p">],</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'django.utils.log.AdminEmailHandler'</span> <span class="p">}</span> <span class="p">},</span> </pre></div> </div> </dd></dl> <dl class="class"> <dt id="django.utils.log.RequireDebugFalse"> <em class="property">class </em><code class="descname">RequireDebugFalse</code><a class="reference internal" href="../_modules/django/utils/log.html#RequireDebugFalse"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.log.RequireDebugFalse" title="Permalink to this definition">¶</a></dt> <dd><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"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOGGING</span></code></a> configuration to ensure that the <a class="reference internal" href="#django.utils.log.AdminEmailHandler" title="django.utils.log.AdminEmailHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminEmailHandler</span></code></a> only sends error emails to admins when <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'filters'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'require_debug_false'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'()'</span><span class="p">:</span> <span class="s1">'django.utils.log.RequireDebugFalse'</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span> <span class="s1">'handlers'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'mail_admins'</span><span class="p">:</span> <span class="p">{</span> <span class="s1">'level'</span><span class="p">:</span> <span class="s1">'ERROR'</span><span class="p">,</span> <span class="s1">'filters'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'require_debug_false'</span><span class="p">],</span> <span class="s1">'class'</span><span class="p">:</span> <span class="s1">'django.utils.log.AdminEmailHandler'</span> <span class="p">}</span> <span class="p">},</span> </pre></div> </div> </dd></dl> <dl class="class"> <dt id="django.utils.log.RequireDebugTrue"> <em class="property">class </em><code class="descname">RequireDebugTrue</code><a class="reference internal" href="../_modules/django/utils/log.html#RequireDebugTrue"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.log.RequireDebugTrue" title="Permalink to this definition">¶</a></dt> <dd><p>This filter is similar to <a class="reference internal" href="#django.utils.log.RequireDebugFalse" title="django.utils.log.RequireDebugFalse"><code class="xref py py-class docutils literal notranslate"><span class="pre">RequireDebugFalse</span></code></a>, except that records are passed only when <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</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 following logging:</p> <p>When <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>:</p> <ul class="simple"> <li>The <code class="docutils literal notranslate"><span class="pre">django</span></code> logger sends messages in the <code class="docutils literal notranslate"><span class="pre">django</span></code> hierarchy (except <code class="docutils literal notranslate"><span class="pre">django.server</span></code>) at the <code class="docutils literal notranslate"><span class="pre">INFO</span></code> level or higher to the console.</li> </ul> <p>When <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>:</p> <ul class="simple"> <li>The <code class="docutils literal notranslate"><span class="pre">django</span></code> logger sends messages in the <code class="docutils literal notranslate"><span class="pre">django</span></code> hierarchy (except <code class="docutils literal notranslate"><span class="pre">django.server</span></code>) with <code class="docutils literal notranslate"><span class="pre">ERROR</span></code> or <code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code> level to <a class="reference internal" href="#django.utils.log.AdminEmailHandler" title="django.utils.log.AdminEmailHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminEmailHandler</span></code></a>.</li> </ul> <p>Independent of the value of <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a>:</p> <ul class="simple"> <li>The <a class="reference internal" href="#django-server-logger"><span class="std std-ref">django.server</span></a> logger sends messages at the <code class="docutils literal notranslate"><span class="pre">INFO</span></code> level or higher to the console.</li> </ul> <p>See also <a class="reference internal" href="#configuring-logging"><span class="std std-ref">Configuring logging</span></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" role="navigation" aria-label="main navigation"> <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="#examples">Examples</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="#id3">Loggers</a><ul> <li><a class="reference internal" href="#django"><code class="docutils literal notranslate"><span class="pre">django</span></code></a></li> <li><a class="reference internal" href="#django-request"><code class="docutils literal notranslate"><span class="pre">django.request</span></code></a></li> <li><a class="reference internal" href="#django-server"><code class="docutils literal notranslate"><span class="pre">django.server</span></code></a></li> <li><a class="reference internal" href="#django-template"><code class="docutils literal notranslate"><span class="pre">django.template</span></code></a></li> <li><a class="reference internal" href="#django-db-backends"><code class="docutils literal notranslate"><span class="pre">django.db.backends</span></code></a></li> <li><a class="reference internal" href="#django-security"><code class="docutils literal notranslate"><span class="pre">django.security.*</span></code></a></li> <li><a class="reference internal" href="#django-db-backends-schema"><code class="docutils literal notranslate"><span class="pre">django.db.backends.schema</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#id4">Handlers</a></li> <li><a class="reference internal" href="#id5">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> <h4>Previous topic</h4> <p class="topless"><a href="i18n/timezones.html" title="previous chapter">Time zones</a></p> <h4>Next topic</h4> <p class="topless"><a href="pagination.html" title="next chapter">Pagination</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/topics/logging.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <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> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Feb 11, 2019</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>