Cara menggunakan add error log php

Saat membangun sebuah website, seringkali web developer menjumpai error. Cara yang paling baik adalah memulai debugging atau mencari sumber masalah dengan membaca error log. Pada tutorial kali ini kita akan belajar dua hal, memunculkan error langsung di web (display error) dan mengaktifkan error log di cPanel melalui MultiPHP INI editor, berikut langkahnya:

  1. Login ke akun cPanel, dan cari menu MultiPHP
    Cara menggunakan add error log php

    💡 Tips: Bila hanya ingin mengaktifkan Display Error saja, pilih tab Basic Mode dan aktifkan opsi display_errors. Bila display_error aktif, maka pesan error akan ditampilkan di halaman website dimana error terjadi.

    Cara menggunakan add error log php

  2. Pada halaman selanjutnya, pilih tab Editor Mode. Pilih Home Directory untuk menyimpan log semua error yang terjadi di akun cPanel anda.
    Cara menggunakan add error log php
  3. Pada bagian Text editor, ada beberapa opsi yang harus dimasukkan agar error log aktif.
    Opsi php.ini yang diperlukan
    error_reportingTipe laporan error yang hendak dicatat.
    display_errorsApakah error akan ditampilkan di website. Isikan Off bila situs sudah live dan ada pengunjung
    log_errorsApakah semua error akan disimpan. Isikan On
    error_logLokasi penyimpanan file error log, tulis lokasi Home Directory anda.

    *Notes: Secara default error log akan tersimpan di masing-masing folder dimana error itu terjadi, agar file error log tidak terpisah-pisah, maka fungsi error_log untuk mengumpulkan log pada satu tempat.
    Contoh :

    error_reporting=E_ALL&~E_NOTICE

    display_errors=Off

    log_errors=On

    error_log=/home/usernamecpanel/error_log

    Ubah bagian usernamecpanel sesuai dengan user cPanel anda.

  4. Klik Save. Success, error logging telah aktif.

Setelah menyelesaikan langkah diatas, anda bisa melihat file error dengan nama error_log di direktori utama (home directory). Namun perlu diperhatikan bahwa setelah selesai memperbaiki error, ada baiknya opsi ini dikembalikan lagi menjadi Off, karena seiring waktu ukuran error_log akan terus membesar. Untuk menonaktifkannya Anda hanya perlu mengganti value log_errors ke Off.

Selamat berburu error!

Bermanfaatkah Artikel Ini?

Klik bintang 5 untuk rating!

Rata rata rating 5 / 5. Jumlah rate 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In PHP, errors and warnings can be logged into a file by using a php script and changing configuration of php.ini file. Two such approaches are mentioned below:

    Approach 1: The error_log() function can be used to send error messages to a given file. First argument to the function is the error message to be sent. Second argument tells where to send/log the error message. In this case, second argument is set to 3, used to redirect error message to a file. Third argument is used to specify the file path of the error logging file.

    Below is the implementation of the above approach:

    $error_message = "This is an error message!";

    $log_file = "./my-errors.log";

    error_log($error_message, 3, $log_file);

    ?>

    Output:

    [20-Dec-2018 17:32:00 UTC] This is an error message!

    Approach 2:

    • The init_set() function allows a user to programmatically update configuration of the php.ini file.
    • The ini_set(“log_errors”, TRUE) command can be added to the php script to enable error logging in php.
    • The ini_set(‘error_log’, $log_file) command can be added to the php script to set the error logging file.
    • Further error_log($error_message) function call can be used to log error message to the given file.

    Below is the implementation of the above approach:

    $error_message = "This is an error message!";

    $log_file = "./my-errors.log";

    ini_set("log_errors", TRUE); 

    ini_set('error_log', $log_file);

    error_log($error_message);

    ?>

    Output:

    [20-Dec-2018 17:30:35 UTC] This is an error message!

    Similar Approach: Following lines can also be added directly to php.ini to make the configuration changes permanent for every php script that logs errors and warnings.

    log_errors = on
    error_log = ./errors.log
    

    Note:: This approach is not highly reliable as compared to other approaches. Its better to use approach 1 as it gives flexibility of choosing different files for logging at same time without changing configuration of php.ini file.