{"title":"ロギング","version":"4.2","locale":"ja","docname":"topics/logging","url":"/ja/4.2/topics/logging/","canonical":"https://djangodocs.dev/ja/4.2/topics/logging/","summary":"参考 ロギングの設定と利用 Django logging リファレンス Python programmers will often use print() in their code as a quick and convenient debugging tool. Using the logging framework is…","html":"<span id=\"logging-explanation\"></span><h1>ロギング<a class=\"heading-anchor\" href=\"#logging\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">参考</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/ja/4.2/howto/logging/#logging-how-to\"><span class=\"std std-ref\">ロギングの設定と利用</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/4.2/ref/logging/#logging-ref\"><span class=\"std std-ref\">Django logging リファレンス</span></a></p></li>\n</ul>\n</aside>\n<p>Python programmers will often use <code class=\"docutils literal notranslate\"><span class=\"pre\">print()</span></code> in their code as a quick and\nconvenient debugging tool. Using the logging framework is only a little more\neffort than that, but it's much more elegant and flexible. As well as being\nuseful for debugging, logging can also provide you with more - and better\nstructured - information about the state and health of your application.</p>\n<section id=\"overview\">\n<h2>概要<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django uses and extends Python's builtin <a class=\"reference external\" href=\"https://docs.python.org/3/library/logging.html#module-logging\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">logging</span></code></a> module to perform\nsystem logging. This module is discussed in detail in Python's own\ndocumentation; this section provides a quick overview.</p>\n<section id=\"the-cast-of-players\">\n<h3>登場人物<a class=\"heading-anchor\" href=\"#the-cast-of-players\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Pythonのロギングは4つの部分で構成されます。</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#topic-logging-parts-loggers\"><span class=\"std std-ref\">ロガー</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#topic-logging-parts-handlers\"><span class=\"std std-ref\">ハンドラ</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#topic-logging-parts-filters\"><span class=\"std std-ref\">フィルタ</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#topic-logging-parts-formatters\"><span class=\"std std-ref\">フォーマッタ</span></a></p></li>\n</ul>\n<section id=\"loggers\">\n<span id=\"topic-logging-parts-loggers\"></span><h4>ロガー<a class=\"heading-anchor\" href=\"#loggers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a href=\"#id1\"><span class=\"problematic\" id=\"id2\">*</span></a>ロガー*は、ロギングシステムのエントリーポイントです。各ロガーは処理が記述されたメッセージの名前付けされたバケツです。</p>\n<p>ロガーには <em>ログレベル</em> が設定されています。このログレベルはハンドリングされたログメッセージの深刻さを表しています。Pythonは下記のログレベルを定義しています。</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code>: デバッグのための低レベルシステム情報</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">INFO</span></code>: 一般的なシステム情報</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">WARNING</span></code>: 重要度の小さい問題が発生したことを示す情報</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code>: 大きな問題が発生したことを示す情報</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">CRITICAL</span></code>: 重大な問題が発生したことを示す情報</p></li>\n</ul>\n<p>ロガーに記載された各メッセージは <em>ログレコード</em> です。各ログレコードは、特定のメッセージの重要性を表す <em>ログレベル</em> を持ちます。ログレコードには、記録されたイベントを説明するメタデータを含むことができます。これには、スタックトレースやエラーコードと言った詳細を含めることができます。</p>\n<p>ロガーにメッセージが渡されたとき、メッセージのログレベルはロガーのログレベルと比較されます。メッセージのログレベルがロガー自体のログレベルと同等以上の場合、メッセージは次の処理に進みます。それ以外の場合、メッセージは無視されます。</p>\n<p>ロガーによってメッセージの処理が必要だと判断された場合、メッセージは <em>ハンドラ</em> に渡されます。</p>\n</section>\n<section id=\"handlers\">\n<span id=\"topic-logging-parts-handlers\"></span><h4>ハンドラ<a class=\"heading-anchor\" href=\"#handlers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a href=\"#id1\"><span class=\"problematic\" id=\"id2\">*</span></a>ハンドラ*は、ロガー上で書くメッセージに何をするかを決定するための原動力です。ここで、メッセージを表示する、ファイルに書き込む、ネットワークソケットに送信する、といった特定のロギング動作を記述します。</p>\n<p>ロガーと同様に、ハンドラもログレベルを持ちます。ログレコードのログレベルがハンドラのレベルに満たない場合、ハンドラはメッセージを無視します。</p>\n<p>ロガーには複数のハンドラを設定でき、また各ハンドラは異なるログレベルを持つことができます。この方法により、メッセージの重要性に応じて通知方法を変えることができます。たとえば、<code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code> と <code class=\"docutils literal notranslate\"><span class=\"pre\">CRITICAL</span></code> メッセージを呼び出しサービスに転送するハンドラを設定する一方で、後で分析するために (<code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code> や``CRITICAL`` も含む) すべてのメッセージをファイルに記録する 2 つめのハンドラをセットすることができます。</p>\n</section>\n<section id=\"filters\">\n<span id=\"topic-logging-parts-filters\"></span><h4>フィルタ<a class=\"heading-anchor\" href=\"#filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a href=\"#id1\"><span class=\"problematic\" id=\"id2\">*</span></a>フィルター*は、どのログレコードがロガーからハンドラへ渡されるかについて、追加的なコントロールを提供します。</p>\n<p>デフォルトでは、ログレベル要件を満たしたすべてのログメッセージが処理されます。しかし、フィルタを使用することで、ロギングのプロセスに追加的な要件を設定できます。たとえば、特定のソースからの <code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code> のみを処理するようにフィルタを設定することができます。</p>\n<p>フィルタは、処理前にログレコードを修正するためにも使えます。たとえば、特定の条件を満たした場合に <code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code> ログレコードを <code class=\"docutils literal notranslate\"><span class=\"pre\">WARNING</span></code> レコードに格下げさせるようなフィルタを設定できます。</p>\n<p>フィルタは、ロガーもしくはハンドラに対して設定できます; 複数のフィルタを使って複数のフィルタ動作を連鎖させることも可能です。</p>\n</section>\n<section id=\"formatters\">\n<span id=\"topic-logging-parts-formatters\"></span><h4>フォーマッタ<a class=\"heading-anchor\" href=\"#formatters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>最終的には、ログレコードはテキストとして表現される必要があります。<a href=\"#id1\"><span class=\"problematic\" id=\"id2\">*</span></a>フォーマッタ*はこのテキストの書式を定義します。フォーマッタは、通常は Python の文字列フォーマットで、<a class=\"reference external\" href=\"https://docs.python.org/3/library/logging.html#logrecord-attributes\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">LogRecord 属性</span></a> を含みます; しかし、特定の書式設定を行うような独自のフォーマッタを記述することもできます。</p>\n</section>\n</section>\n</section>\n<section id=\"security-implications\">\n<span id=\"logging-security-implications\"></span><h2>セキュリティへの影響<a class=\"heading-anchor\" href=\"#security-implications\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>ロギングシステムは潜在的にセンシティブな情報を取り扱うことになります。たとえば、ログレコードにはウェブリクエストやスタックトレースに関する情報が含まれることがありますが、ロガーで収集されたデータの一部にはセキュリティへの影響もあるかもしれません。あなたは次のことを理解しておく必要があります。</p>\n<ul class=\"simple\">\n<li><p>どの情報を収集するか</p></li>\n<li><p>最終的にその情報はどこに保存されることになるか</p></li>\n<li><p>その情報の転送はどのように行われるか</p></li>\n<li><p>その情報にアクセスする可能性があるのは誰か</p></li>\n</ul>\n<p>To help control the collection of sensitive information, you can explicitly\ndesignate certain sensitive information to be filtered out of error reports --\nread more about how to <a class=\"reference internal\" href=\"/ja/4.2/howto/error-reporting/#filtering-error-reports\"><span class=\"std std-ref\">filter error reports</span></a>.</p>\n<section id=\"adminemailhandler\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">AdminEmailHandler</span></code><a class=\"heading-anchor\" href=\"#adminemailhandler\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The built-in <a class=\"reference internal\" href=\"/ja/4.2/ref/logging/#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> deserves a mention in\nthe context of security. If its <code class=\"docutils literal notranslate\"><span class=\"pre\">include_html</span></code> option is enabled, the email\nmessage it sends will contain a full traceback, with names and values of local\nvariables at each level of the stack, plus the values of your Django settings\n(in other words, the same level of detail that is exposed in a web page when\n<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#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>\n<p>It's generally not considered a good idea to send such potentially sensitive\ninformation over email. Consider instead using one of the many third-party\nservices to which detailed logs can be sent to get the best of multiple worlds\n-- the rich information of full tracebacks, clear management of who is notified\nand has access to the information, and so on.</p>\n</section>\n</section>\n<section id=\"configuring-logging\">\n<span id=\"id1\"></span><h2>ロギングを設定する<a class=\"heading-anchor\" href=\"#configuring-logging\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Python のロギングライブラリは、実用的なインターフェースから設定ファイルまで、ロギング設定のテクニックを提供しています。デフォルトでは、Django は <a class=\"reference external\" href=\"https://docs.python.org/3/library/logging.config.html#logging-config-dictschema\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">dictConfig フォーマット</span></a> を使用します。</p>\n<p>In order to configure logging, you use <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING</span></code></a> to define a\ndictionary of logging settings. These settings describe the loggers,\nhandlers, filters and formatters that you want in your logging setup,\nand the log levels and other properties that you want those components\nto have.</p>\n<p>デフォルトでは、<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING</span></code></a> 設定は、以下のスキームを使って <a class=\"reference internal\" href=\"/ja/4.2/ref/logging/#default-logging-configuration\"><span class=\"std std-ref\">Django のデフォルトロギング設定</span></a> に統合されています。</p>\n<p><a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING</span></code></a> の <code class=\"docutils literal notranslate\"><span class=\"pre\">disable_existing_loggers</span></code> キーの値を <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> にすると、全てのデフォルトの設定が無効になります（キーが存在しない場合は <code class=\"docutils literal notranslate\"><span class=\"pre\">dictConfig</span></code> のデフォルトになります）。このため、 <code class=\"docutils literal notranslate\"><span class=\"pre\">'disable_existing_loggers':</span> <span class=\"pre\">True</span></code> を使う場合は注意してください。 <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> を設定する必要は殆どないでしょう。 <code class=\"docutils literal notranslate\"><span class=\"pre\">disable_existing_loggers</span></code> を <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> に設定して、デフォルトのロガーの一部、または全てを定義しなおすこともできます。あるいは、 <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING_CONFIG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING_CONFIG</span></code></a> を <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> に設定して、 <a class=\"reference internal\" href=\"#disabling-logging-configuration\"><span class=\"std std-ref\">ロギングの設定を自分で行うこと</span></a> も出来ます。</p>\n<p>ロギングは、一般的な Django の <code class=\"docutils literal notranslate\"><span class=\"pre\">setup()</span></code> 関数の一部として設定されます。したがって、ロガーが常にプロジェクトコード内で使用準備ができていることが保証されています。</p>\n<section id=\"examples\">\n<h3>例<a class=\"heading-anchor\" href=\"#examples\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"https://docs.python.org/3/library/logging.config.html#logging-config-dictschema\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">dictConfig フォーマット</span></a> に関する完全なドキュメントが、ロギング設定ディクショナリの最高の教材です。とはいえ、どんなことが可能なのか知ってもらうため、以下にいくつかの例を示します。</p>\n<p>最初に、全てのログメッセージをコンソールに出力するための最低限の設定がこちらです。</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">os</span>\n\n<span class=\"n\">LOGGING</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;version&quot;</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;disable_existing_loggers&quot;</span><span class=\"p\">:</span> <span class=\"kc\">False</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;console&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;logging.StreamHandler&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;root&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;console&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;WARNING&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></figure>\n<p>この設定では、 <code class=\"docutils literal notranslate\"><span class=\"pre\">root</span></code> という名前の親のロガーがあり、 <code class=\"docutils literal notranslate\"><span class=\"pre\">WARNING</span></code> レベル以上のメッセージを受け取ったら、 console ハンドラに送るようになっています。 level を <code class=\"docutils literal notranslate\"><span class=\"pre\">INFO</span></code> または <code class=\"docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code> にすると、より詳細なメッセージも表示されるようになります。この設定は開発中に役に立ちます。</p>\n<p>Next we can add more fine-grained logging. Here's an example of how to make the\nlogging system print more messages from just the <a class=\"reference internal\" href=\"/ja/4.2/ref/logging/#django-logger\"><span class=\"std std-ref\">django</span></a> named\nlogger:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">os</span>\n\n<span class=\"n\">LOGGING</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;version&quot;</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;disable_existing_loggers&quot;</span><span class=\"p\">:</span> <span class=\"kc\">False</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;console&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;logging.StreamHandler&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;root&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;console&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;WARNING&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;loggers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;django&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;console&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;level&quot;</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=\"s2\">&quot;DJANGO_LOG_LEVEL&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;INFO&quot;</span><span class=\"p\">),</span>\n            <span class=\"s2\">&quot;propagate&quot;</span><span class=\"p\">:</span> <span class=\"kc\">False</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></figure>\n<p>By default, this config sends messages from the <code class=\"docutils literal notranslate\"><span class=\"pre\">django</span></code> logger of level\n<code class=\"docutils literal notranslate\"><span class=\"pre\">INFO</span></code> or higher to the console. This is the same level as Django's default\nlogging config, except that the default config only displays log records when\n<code class=\"docutils literal notranslate\"><span class=\"pre\">DEBUG=True</span></code>. Django does not log many such <code class=\"docutils literal notranslate\"><span class=\"pre\">INFO</span></code> level messages. With\nthis config, however, you can also set the environment variable\n<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\nverbose as it includes all database queries.</p>\n<p>必ずしもコンソールに出力する必要はありません。 <a class=\"reference internal\" href=\"/ja/4.2/ref/logging/#django-logger\"><span class=\"std std-ref\">django</span></a> という名前のロガーをファイルに書き込むにはこのように設定します。</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"n\">LOGGING</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;version&quot;</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;disable_existing_loggers&quot;</span><span class=\"p\">:</span> <span class=\"kc\">False</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;file&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;DEBUG&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;logging.FileHandler&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;filename&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;/path/to/django/debug.log&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;loggers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;django&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;file&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;DEBUG&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;propagate&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></figure>\n<p>この例を使用するときは、<code class=\"docutils literal notranslate\"><span class=\"pre\">'filename'</span></code> のパスを Django アプリケーションを実行しているユーザーが書き込み可能な場所に変更してください。</p>\n<p>最後に、かなり複雑なロギングの設定の例です。</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"n\">LOGGING</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;version&quot;</span><span class=\"p\">:</span> <span class=\"mi\">1</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;disable_existing_loggers&quot;</span><span class=\"p\">:</span> <span class=\"kc\">False</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;formatters&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;verbose&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;format&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;</span><span class=\"si\">{levelname}</span><span class=\"s2\"> </span><span class=\"si\">{asctime}</span><span class=\"s2\"> </span><span class=\"si\">{module}</span><span class=\"s2\"> </span><span class=\"si\">{process:d}</span><span class=\"s2\"> </span><span class=\"si\">{thread:d}</span><span class=\"s2\"> </span><span class=\"si\">{message}</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;style&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;{&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n        <span class=\"s2\">&quot;simple&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;format&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;</span><span class=\"si\">{levelname}</span><span class=\"s2\"> </span><span class=\"si\">{message}</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;style&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;{&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;filters&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;special&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;()&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;project.logging.SpecialFilter&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n        <span class=\"s2\">&quot;require_debug_true&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;()&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.utils.log.RequireDebugTrue&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;console&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;INFO&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;filters&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;require_debug_true&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;logging.StreamHandler&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;formatter&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;simple&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n        <span class=\"s2\">&quot;mail_admins&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;ERROR&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.utils.log.AdminEmailHandler&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;filters&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;special&quot;</span><span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;loggers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;django&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;console&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;propagate&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n        <span class=\"s2\">&quot;django.request&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;mail_admins&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;ERROR&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;propagate&quot;</span><span class=\"p\">:</span> <span class=\"kc\">False</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n        <span class=\"s2\">&quot;myproject.custom&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;console&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;mail_admins&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;level&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;INFO&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;filters&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;special&quot;</span><span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></figure>\n<p>このロギング設定は、以下のことを行います:</p>\n<ul>\n<li><p>設定が「dictConfig バージョン 1」形式であることを明示します。 現在、これが唯一の dictConfig 形式のバージョンです。</p></li>\n<li><p>2 つのフォーマッタを定義します:</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">simple</span></code>, that outputs the log level name (e.g., <code class=\"docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code>) and the log\nmessage.</p>\n<p>この <code class=\"docutils literal notranslate\"><span class=\"pre\">format</span></code> 文字列は標準の Python 文字列フォーマットで、各ログ行で出力される詳細を定義します。出力可能な詳細の全リストは <a class=\"reference external\" href=\"https://docs.python.org/3/library/logging.html#formatter-objects\" title=\"(in Python v3.14)\"><span>Formatter Objects</span></a> に記載されています。</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">verbose</span></code> は、ログレベルの名称とログメッセージに加えて、時刻、プロセス、スレッド、ログメッセージを生成したモジュールを出力します。</p></li>\n</ul>\n</li>\n<li><p>2 つのフィルタを定義します:</p>\n<ul class=\"simple\">\n<li><p><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\nfilter required additional arguments, they can be provided as additional\nkeys in the filter configuration dictionary. In this case, the argument\n<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\n<code class=\"docutils literal notranslate\"><span class=\"pre\">SpecialFilter</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.log.RequireDebugTrue</span></code> は、<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> が <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> のときにレコードの処理を進めます。</p></li>\n</ul>\n</li>\n<li><p>2 つのハンドラを定義します:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">console</span></code> は、すべての <code class=\"docutils literal notranslate\"><span class=\"pre\">INFO</span></code> 以上のメッセージを <code class=\"docutils literal notranslate\"><span class=\"pre\">sys.stderr</span></code> に表示 (print) する <a class=\"reference external\" href=\"https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamHandler</span></code></a> クラスです。このハンドラは <code class=\"docutils literal notranslate\"><span class=\"pre\">simple</span></code> 出力フォーマットを使用します。</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">mail_admins</span></code>, an <a class=\"reference internal\" href=\"/ja/4.2/ref/logging/#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>, which\nemails any <code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code> (or higher) message to the site <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-ADMINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ADMINS</span></code></a>.\nThis handler uses the <code class=\"docutils literal notranslate\"><span class=\"pre\">special</span></code> filter.</p></li>\n</ul>\n</li>\n<li><p>3 つのロガーを設定します:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django</span></code> は、すべてのメッセージを <code class=\"docutils literal notranslate\"><span class=\"pre\">console</span></code> ハンドラに渡します。</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.request</span></code> は、すべての <code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code> メッセージを <code class=\"docutils literal notranslate\"><span class=\"pre\">mail_admins</span></code> ハンドラに渡します。加えて、このロガーはメッセージを親に <em>伝えない</em> よう設定されています。これは、<code class=\"docutils literal notranslate\"><span class=\"pre\">django.request</span></code> 内に記述されたログメッセージは <code class=\"docutils literal notranslate\"><span class=\"pre\">django</span></code> ロガーで処理されないことを意味します。</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">myproject.custom</span></code> は <code class=\"docutils literal notranslate\"><span class=\"pre\">INFO</span></code> もしくはそれ以上のすべてのメッセージを渡し、<code class=\"docutils literal notranslate\"><span class=\"pre\">special</span></code> フィルタを 2 つのハンドラに引き渡します -- <code class=\"docutils literal notranslate\"><span class=\"pre\">console</span></code> と <code class=\"docutils literal notranslate\"><span class=\"pre\">mail_admins</span></code> です。これは、<code class=\"docutils literal notranslate\"><span class=\"pre\">INFO</span></code> 以上のレベルのメッセージがコンソールで表示され、さらに <code class=\"docutils literal notranslate\"><span class=\"pre\">ERROR</span></code> と <code class=\"docutils literal notranslate\"><span class=\"pre\">CRITICAL</span></code> のメッセージは E メールでも出力されることを意味します。</p></li>\n</ul>\n</li>\n</ul>\n</section>\n<section id=\"custom-logging-configuration\">\n<h3>カスタムのロギング設定<a class=\"heading-anchor\" href=\"#custom-logging-configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>ロガーの設定に Python の dictConfig フォーマットを使用したくない場合は、あなた自身の設定スキームを定義することができます。</p>\n<p>The <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#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\nbe used to configure Django's loggers. By default, it points at\nPython's <a class=\"reference external\" href=\"https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">logging.config.dictConfig()</span></code></a> function. However, if you want to\nuse a different configuration process, you can use any other callable\nthat takes a single argument. The contents of <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING</span></code></a> will\nbe provided as the value of that argument when logging is configured.</p>\n</section>\n<section id=\"disabling-logging-configuration\">\n<span id=\"id2\"></span><h3>ロギング設定を無効化する<a class=\"heading-anchor\" href=\"#disabling-logging-configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>ロギングを全く設定したくない（あるいは、自分でロギングをやりたい）なら、 <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING_CONFIG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING_CONFIG</span></code></a> を <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> に設定することもできます。これにより、 <a class=\"reference internal\" href=\"/ja/4.2/ref/logging/#default-logging-configuration\"><span class=\"std std-ref\">Django のデフォルトのロギング設定</span></a> を無効化できます。</p>\n<p><a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING_CONFIG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING_CONFIG</span></code></a> を <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> に設定すると、自動でロギングの設定が行われなくなるだけです。ロギング自体が使えなくなるわけではありません。自動でのロギング設定を無効化しても、 Django はロギングのメソッドを呼び、もしログ設定がされていれば、そこに出力されます。</p>\n<p>Django の自動でのログ設定を無効化し、手動で設定する場合はこのようにします。</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"n\">LOGGING_CONFIG</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n\n<span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">logging.config</span>\n\n<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>\n</code></pre></figure>\n<p>Note that the default configuration process only calls\n<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGGING_CONFIG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">LOGGING_CONFIG</span></code></a> once settings are fully-loaded. In contrast, manually\nconfiguring the logging in your settings file will load your logging config\nimmediately. As such, your logging config must appear <em>after</em> any settings on\nwhich it depends.</p>\n</section>\n</section>","rootId":"logging","toc":[{"title":"概要","anchor":"overview","children":[{"title":"登場人物","anchor":"the-cast-of-players","children":[{"title":"ロガー","anchor":"loggers","children":[]},{"title":"ハンドラ","anchor":"handlers","children":[]},{"title":"フィルタ","anchor":"filters","children":[]},{"title":"フォーマッタ","anchor":"formatters","children":[]}]}]},{"title":"セキュリティへの影響","anchor":"security-implications","children":[{"title":"AdminEmailHandler","anchor":"adminemailhandler","children":[]}]},{"title":"ロギングを設定する","anchor":"configuring-logging","children":[{"title":"例","anchor":"examples","children":[]},{"title":"カスタムのロギング設定","anchor":"custom-logging-configuration","children":[]},{"title":"ロギング設定を無効化する","anchor":"disabling-logging-configuration","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Django を使う","url":"/ja/4.2/topics/"}],"prev":{"docname":"topics/i18n/timezones","title":"タイムゾーン","url":"/ja/4.2/topics/i18n/timezones/"},"next":{"docname":"topics/pagination","title":"ページネーション","url":"/ja/4.2/topics/pagination/"},"formats":{"html":"/ja/4.2/topics/logging/","markdown":"/ja/4.2/topics/logging.md","json":"/ja/4.2/topics/logging.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/topics/logging.txt","official":"https://docs.djangoproject.com/ja/4.2/topics/logging/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}