---
title: "How to manage error reporting"
version: 4.0
locale: id
source: https://docs.djangoproject.com/id/4.0/howto/error-reporting/
canonical: https://djangodocs.dev/id/4.0/howto/error-reporting/
---
# How to manage error reporting

Ketika anda sedang menjalankan situs umum anda harus selalu mematikan pengaturan [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG). Itu akan membuat peladen anda berjalan lebih cepat, dan juga akan mencegah pengguna hahat dari melihat rincian dari aplikasi anda yang dapat diungkap dengan kesalahan halaman.

However, running with [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG) set to `False` means you'll never see
errors generated by your site -- everyone will instead see your public error
pages. You need to keep track of errors that occur in deployed sites, so Django
can be configured to create reports with details about those errors.

## Surel laporan

### Kesalahan peladen

When [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG) is `False`, Django will email the users listed in the
[`ADMINS`](/id/4.0/ref/settings/#std-setting-ADMINS) setting whenever your code raises an unhandled exception and
results in an internal server error (strictly speaking, for any response with
an HTTP status code of 500 or greater). This gives the administrators immediate
notification of any errors. The [`ADMINS`](/id/4.0/ref/settings/#std-setting-ADMINS) will get a description of the
error, a complete Python traceback, and details about the HTTP request that
caused the error.

> **Note**
>
> Agar mengirim surel, Django membutuhkan beberapa pengaturan mengatakannya bagaimana terhubung ke peladen surat anda. Setidaknya, anda akan butuh menentukan [`EMAIL_HOST`](/id/4.0/ref/settings/#std-setting-EMAIL_HOST) dan kemungkinan [`EMAIL_HOST_USER`](/id/4.0/ref/settings/#std-setting-EMAIL_HOST_USER) dan [`EMAIL_HOST_PASSWORD`](/id/4.0/ref/settings/#std-setting-EMAIL_HOST_PASSWORD), meskipun pengaturan lainnya mungkin tidak juga dibutuhkan tergantung pada konfigurasi peladen surat anda. Rundingkan [the Django settings documentation](/id/4.0/ref/settings/) untuk daftar penuh dari pengaturan terkait-surel.

Secara awal, Django akan mengirim surel dari [root@localhost](mailto:root@localhost). Bagaimanapun, beberapa penyedia surat menolak semua surel dari alamat ini. Untuk menggunakan alamat pengirim berbeda, rubah pengaturan [`SERVER_EMAIL`](/id/4.0/ref/settings/#std-setting-SERVER_EMAIL).

Untuk mengaktifkan perilaku ini, masukkan alamat surel dari penerima di bagian [`ADMINS`](/id/4.0/ref/settings/#std-setting-ADMINS).

> **See also**
>
> Surel kesalahan peladen dikirim menggunakan kerangka pencatatan, jadi anda dapat menyesuaikan perilaku ini dengan [customizing your logging configuration](/id/4.0/topics/logging/).

### kesalahan 404

Django juga dapat dikonfirgasi untuk mengirimkan surel kesalahan tentang tautan yang tidak ditemukan (404 "page not found" errors). Django mengirim surel tentang error 404 ketika:

- [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG) adalah `False`;
- Pengaturan [`MIDDLEWARE`](/id/4.0/ref/settings/#std-setting-MIDDLEWARE) anda termasuk [`django.middleware.common.BrokenLinkEmailsMiddleware`](/id/4.0/ref/middleware/#django.middleware.common.BrokenLinkEmailsMiddleware).

If those conditions are met, Django will email the users listed in the
[`MANAGERS`](/id/4.0/ref/settings/#std-setting-MANAGERS) setting whenever your code raises a 404 and the request has
a referer. It doesn't bother to email for 404s that don't have a referer --
those are usually people typing in broken URLs or broken web bots. It also
ignores 404s when the referer is equal to the requested URL, since this
behavior is from broken web bots too.

> **Note**
>
> [`BrokenLinkEmailsMiddleware`](/id/4.0/ref/middleware/#django.middleware.common.BrokenLinkEmailsMiddleware) must appear
> before other middleware that intercepts 404 errors, such as
> [`LocaleMiddleware`](/id/4.0/ref/middleware/#django.middleware.locale.LocaleMiddleware) or
> [`FlatpageFallbackMiddleware`](/id/4.0/ref/contrib/flatpages/#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware).
> Put it toward the top of your [`MIDDLEWARE`](/id/4.0/ref/settings/#std-setting-MIDDLEWARE) setting.

Anda dapat memberitahu Django untuk menghentikan pelaporan tertentu 404 dengan merubah pengaturan [`IGNORABLE_404_URLS`](/id/4.0/ref/settings/#std-setting-IGNORABLE_404_URLS). Dia harus menjadi daftar dari obyek ungkapan biasa tersusun. Sebagai contoh:

```
import re
IGNORABLE_404_URLS = [
    re.compile(r'\.(php|cgi)$'),
    re.compile(r'^/phpmyadmin/'),
]
```

Dalam contoh ini, sebuah 404 pada setiap URL berakhiran dengan `.php` atau `.cgi` akan *tidak* dilaporkan. Juga tidak akan URL apapun dimulai dengan `/phpmyadmin/`.

Contoh berikut menunjukkan bagaimana mengeluarkan beberapa URL biasa yang perambah dan penjilat sering diminta:

```
import re
IGNORABLE_404_URLS = [
    re.compile(r'^/apple-touch-icon.*\.png$'),
    re.compile(r'^/favicon\.ico$'),
    re.compile(r'^/robots\.txt$'),
]
```

(Perhatikan bahwa ini adalah regular expression, jadi kita tuliskan garis miring terbalik di depan titik untuk melepaskannya)

Jika anda suka menyesuaikan perilaku dari [`django.middleware.common.BrokenLinkEmailsMiddleware`](/id/4.0/ref/middleware/#django.middleware.common.BrokenLinkEmailsMiddleware) lebih lanjut (sebagai contoh untuk mengabaikan permintaan datang dari penjilat jaringan), anda harus mensubkelaskannya dan menimpa caranya.

> **See also**
>
> Kesalahan 404 dicatat menggunakan kerangka pencatatan. Secara awal, rekaman catatan ini diabaikan, tetapi anda dapat menggunakan mereka untuk kesalahan pelaporan dengan menulis sebuah penanganan [configuring logging](/id/4.0/topics/logging/) dengan benar.

## Menyaring laporan kesalahan

> **Warning**
>
> Filtering sensitive data is a hard problem, and it's nearly impossible to
> guarantee that sensitive data won't leak into an error report. Therefore,
> error reports should only be available to trusted team members and you
> should avoid transmitting error reports unencrypted over the internet
> (such as through email).

### Menyaring informasi rahasia

Laporan kesalahan sangat membantu untuk memeriksa kesalahan, jadi dia umumnya berguna untuk merekam informasi terkait tentang kesalahan-kesalahan tersebut sebanyak mungkin. Sebagai contoh, secara awal rekaman Django [full traceback](https://en.wikipedia.org/wiki/Stack_trace) untuk dimunculkan pengecualian, setiap variabel lokal [full traceback](https://en.wikipedia.org/wiki/Stack_trace), dan [attributes](/id/4.0/ref/request-response/#httprequest-attributes) [`HttpRequest`](/id/4.0/ref/request-response/#django.http.HttpRequest).

Bagaimanapun, terkadang jenis-jenis informasi tertentu mungkin terlalu sensitif dan dengan demikian mungkin tidak sesuai untuk terus melacak, sebagai contoh, sandi pengguna atau angka kartu kredit. Jadi di tambahaan untuk menyaring pengaturan yang muncul menjadi sensitif seperti digambarkan dalam dokumentasi [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG), Django menawarkan sekumpulan fungsi penghias untuk membantu anda mengendalikan informasi mana harus disaring dari laporan kesalahan dalam lingkungan produksi (yaitu, dimana [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG) disetel ke `False`): [`sensitive_variables()`](#django.views.decorators.debug.sensitive_variables) dan [`sensitive_post_parameters()`](#django.views.decorators.debug.sensitive_post_parameters).

#### `sensitive_variables(*variables)`

Jika sebuah fungsi (baik sebuah tampilan atau callback umum apapun) dalam kode anda menggunakan variabel lokal rentan mengandung informasi sensitif, anda mungkin mencegah nilai-nilai variabel tersebut dari menjadi disertakan dalam laporan kesalahan menggunakan penghias `sensitive_variables`:

```
from django.views.decorators.debug import sensitive_variables

@sensitive_variables('user', 'pw', 'cc')
def process_info(user):
    pw = user.pass_word
    cc = user.credit_card_number
    name = user.name
    ...
```

In the above example, the values for the `user`, `pw` and `cc`
variables will be hidden and replaced with stars (`**********`)
in the error reports, whereas the value of the `name` variable will be
disclosed.

Untuk secara sistematis menyembunyikan semua variabel lokal dari sebuah fungsi dari laporan kesalahan, jangan menyediakan argumen apapun pada decorator `sensitive_variables`:

```
@sensitive_variables()
def my_function():
    ...
```

> **Ketika menggunakan sejumlah dekorator**
>
> Jika variabel anda ingin sembunyikan adalah juga argumen fungsi (sebagai contoh '`user`’ dalam contoh berikut), dan jika fungsi dihiasi mempunyai banyak decorator, kemudian pastikan menempatkan `@sensitive_variables` pda atas dari rantai decorator. Cara ini dia akan juga menyembunyikan argumen fungsi ketika dia mendapatkan dilewati melalui decorator lain:
>
> ```
> @sensitive_variables('user', 'pw', 'cc')
> @some_decorator
> @another_decorator
> def process_info(user):
>     ...
> ```

#### `sensitive_post_parameters(*parameters)`

Jika satu dari tampilan anda menerima sebuah obyek [`HttpRequest`](/id/4.0/ref/request-response/#django.http.HttpRequest) dengan [`POST parameters`](/id/4.0/ref/request-response/#django.http.HttpRequest.POST) rentan untuk mengandung informasi sensitif, anda mungkin mencegah nilai-nilai dari parameter tersebut dari menjadi disertakan dalam laporan kesalahan menggunakan penghias `sensitive_post_parameters`:

```
from django.views.decorators.debug import sensitive_post_parameters

@sensitive_post_parameters('pass_word', 'credit_card_number')
def record_user_profile(request):
    UserProfile.create(
        user=request.user,
        password=request.POST['pass_word'],
        credit_card=request.POST['credit_card_number'],
        name=request.POST['name'],
    )
    ...
```

In the above example, the values for the `pass_word` and
`credit_card_number` POST parameters will be hidden and replaced with
stars (`**********`) in the request's representation inside the
error reports, whereas the value of the `name` parameter will be
disclosed.

Untuk secara sistematis menyembunyikan semua parameter POST dari sebuah permintaan dalam laporan kesalahan, jangan menyediakan argumen apapun pada decorator `sensitive_variables`:

```
@sensitive_post_parameters()
def my_view(request):
    ...
```

Semua parameter POST adalah sistematis disaring keluar dari laporan kesalahan untuk tampilan [`django.contrib.auth.views`](/id/4.0/topics/auth/default/#module-django.contrib.auth.views) tertentu (`login`, `password_reset_confirm`, `password_change`, dan `add_view` dan `user_change_password` dalam admin `auth`) untuk mencegah dari pembocoran dari informasi sensitif seperti sandi pengguna.

### Penyesuaian laporan kesalahan

All [`sensitive_variables()`](#django.views.decorators.debug.sensitive_variables) and [`sensitive_post_parameters()`](#django.views.decorators.debug.sensitive_post_parameters) do is,
respectively, annotate the decorated function with the names of sensitive
variables and annotate the `HttpRequest` object with the names of sensitive
POST parameters, so that this sensitive information can later be filtered out
of reports when an error occurs. The actual filtering is done by Django's
default error reporter filter:
[`django.views.debug.SafeExceptionReporterFilter`](#django.views.debug.SafeExceptionReporterFilter). This filter uses the
decorators' annotations to replace the corresponding values with stars
(`**********`) when the error reports are produced. If you wish to
override or customize this default behavior for your entire site, you need to
define your own filter class and tell Django to use it via the
[`DEFAULT_EXCEPTION_REPORTER_FILTER`](/id/4.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER_FILTER) setting:

```
DEFAULT_EXCEPTION_REPORTER_FILTER = 'path.to.your.CustomExceptionReporterFilter'
```

Anda dapat juga mengendalikan cara lebih kecil penyaring mana untuk digunakan dalam tampilan yang diberikan oleh pengaturan atribut `exception_reporter_filter` `HttpRequest`:

```
def my_view(request):
    if request.user.is_authenticated:
        request.exception_reporter_filter = CustomExceptionReporterFilter()
    ...
```

Your custom filter class needs to inherit from
[`django.views.debug.SafeExceptionReporterFilter`](#django.views.debug.SafeExceptionReporterFilter) and may override the
following attributes and methods:

#### `class SafeExceptionReporterFilter`

#### `cleansed_substitute`

The string value to replace sensitive value with. By default it
replaces the values of sensitive variables with stars
(`**********`).

#### `hidden_settings`

A compiled regular expression object used to match settings and
`request.META` values considered as sensitive. By default equivalent
to:

```
import re

re.compile(r'API|TOKEN|KEY|SECRET|PASS|SIGNATURE', flags=re.IGNORECASE)
```

#### `is_active(request)`

Returns `True` to activate the filtering in
[`get_post_parameters()`](#django.views.debug.SafeExceptionReporterFilter.get_post_parameters) and [`get_traceback_frame_variables()`](#django.views.debug.SafeExceptionReporterFilter.get_traceback_frame_variables).
By default the filter is active if [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG) is `False`. Note
that sensitive `request.META` values are always filtered along with
sensitive setting values, as described in the [`DEBUG`](/id/4.0/ref/settings/#std-setting-DEBUG)
documentation.

#### `get_post_parameters(request)`

Returns the filtered dictionary of POST parameters. Sensitive values
are replaced with [`cleansed_substitute`](#django.views.debug.SafeExceptionReporterFilter.cleansed_substitute).

#### `get_traceback_frame_variables(request, tb_frame)`

Returns the filtered dictionary of local variables for the given
traceback frame. Sensitive values are replaced with
[`cleansed_substitute`](#django.views.debug.SafeExceptionReporterFilter.cleansed_substitute).

If you need to customize error reports beyond filtering you may specify a
custom error reporter class by defining the
[`DEFAULT_EXCEPTION_REPORTER`](/id/4.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER) setting:

```
DEFAULT_EXCEPTION_REPORTER = 'path.to.your.CustomExceptionReporter'
```

The exception reporter is responsible for compiling the exception report data,
and formatting it as text or HTML appropriately. (The exception reporter uses
[`DEFAULT_EXCEPTION_REPORTER_FILTER`](/id/4.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER_FILTER) when preparing the exception
report data.)

Your custom reporter class needs to inherit from
[`django.views.debug.ExceptionReporter`](#django.views.debug.ExceptionReporter).

#### `class ExceptionReporter`

#### `html_template_path`

> **New in Django 3.2**

Property that returns a [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) representing the absolute
filesystem path to a template for rendering the HTML representation of
the exception. Defaults to the Django provided template.

#### `text_template_path`

> **New in Django 3.2**

Property that returns a [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) representing the absolute
filesystem path to a template for rendering the plain-text
representation of the exception. Defaults to the Django provided
template.

#### `get_traceback_data()`

Return a dictionary containing traceback information.

Ini adalah titik ekstensi utama untuk menyesuaikan laporan pengecualian, sebagai contoh:

```
from django.views.debug import ExceptionReporter

class CustomExceptionReporter(ExceptionReporter):
    def get_traceback_data(self):
        data = super().get_traceback_data()
        # ... remove/add something here ...
        return data
```

#### `get_traceback_html()`

Mengembalikan versi HTML dari laporan pengecualian.

Digunakan untuk versi HTML dari pengawakutu halaman kesalahan HTTP  500.

#### `get_traceback_text()`

Mengembalikan versi teks polis dari laporan pengecualian.

Digunakan untuk versi teks polos dari pengawakutu halaman kesalahan HTTP 500 dan laporan surel.

As with the filter class, you may control which exception reporter class to use
within any given view by setting the `HttpRequest`’s
`exception_reporter_class` attribute:

```
def my_view(request):
    if request.user.is_authenticated:
        request.exception_reporter_class = CustomExceptionReporter()
    ...
```

> **See also**
>
> Anda dapat juga mengatur penyesuaian pelaporan kesalahan dengan menulis sebuah potongan penyesuaian dari [exception middleware](/id/4.0/topics/http/middleware/#exception-middleware). Jika anda melakukan menulis penyesuaian penanganan kesalahan, adalah ide bagus untuk meniru penanganan kesalahan siap pakai Django dan hanya laporan/catatan kesalahan jika setting:DEBUG adalah `False`.
