Membuat log dengan PHP Codeigniter

Membuat log dengan PHP Codeigniter

Langsung aja bro apa itu log, gunanya buat apa??, gini deh bro, misal admin sistem pingin tau user siapa aja yang baru login,logut dari aplikasi kita, terus ada yang add,edit ,delete data itu siapa ajja user pelakunya … hehehe

jadi butuh namanya log buat di program aplikasi kita, ane bikin yang simpel ajja ya bro di codeigniter, terserah nanti mau di kembangin lagi, tinggal pakai logikanya, bisa juga buat tracking, buat di php native, asp atau bahasa pmograman lainya ….

sebelum mulai definisiin dulu kita mau bikin log buat apa ajja, contoh nih ya bro ane bikin log pada saat
0 = login
1 = logout
2 = add data
3 = edit data
4 = delete data

#DATABASE

pertama bikin table namain table_log

DROP TABLE IF EXISTS `tabel_log`;
CREATE TABLE `tabel_log` (
  `log_id` int(11) NOT NULL AUTO_INCREMENT,
  `log_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `log_user` varchar(255) DEFAULT NULL,
  `log_tipe` int(11) DEFAULT NULL,
  `log_desc` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`log_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

#HELPER

bikin helper, biar ga ngoding diulang-ulang, menuh-menuhin script ajja, tinggal di load,terus tinggal panggil … bikin file namanya log_helper.php save di dalem folder helper

function helper_log($tipe = "", $str = ""){
    $CI =& get_instance();

    if (strtolower($tipe) == "login"){
        $log_tipe   = 0;
    }
    elseif(strtolower($tipe) == "logout")
    {
        $log_tipe   = 1;
    }
    elseif(strtolower($tipe) == "add"){
        $log_tipe   = 2;
    }
    elseif(strtolower($tipe) == "edit"){
        $log_tipe  = 3;
    }
    else{
        $log_tipe  = 4;
    }

    // paramter
    $param['log_user']      = $CI->session->userdata('username');
    $param['log_tipe']      = $log_tipe;
    $param['log_desc']      = $str;

    //load model log
    $CI->load->model('m_log');

    //save to database
    $CI->m_log->save_log($param);

}

#AUTOLOAD

jangan lupa load dulu file autoload.php di folder config

$autoload['helper'] = array('url','form','log');

#MODEL

buat file baru di model namain m_log.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_log extends CI_Model {

	public function save_log($param)
    {
        $sql        = $this->db->insert_string('tabel_log',$param);
        $ex         = $this->db->query($sql);
        return $this->db->affected_rows($sql);
    }
}

#CONTROLLER

tinggal panggil helpernya aja dari controller dari setiap function yang ngejalanin perintah  login,logout,add,edit,delete dan ane kasih contoh cara manggilnya, tinggal di ganti kata-katanya ajja bro, dibawah ane kasih contoh panggil helpernya pada saat user menambahkan data barang

helper_log("add", "menambahkan data");

contoh ane gunain di controller funtion save barang

function save_barang()
    {

            $nama       =   $this->input->post('nama_barang');
            $kategori   =   $this->input->post('kategori');
            $harga      =   $this->input->post('harga');
            $data       =   array('nama_barang'=>$nama,'kategori_id'=>$kategori,'harga'=>$harga);
            $this->model_barang->save_barang($data);

            //contoh panggil helper log
            helper_log("add", "menambahkan data");
            //silahkan di ganti2 aja kalimatnya

            redirect('view_barang');

    }
Muhammad Imron

Related Posts
Comments ( 35 )
  1. PAk
    December 31, 2015 at 8:41 am
    Reply

    Boleh minta hasil/resultnya gan??

    • Muhammad Imron
      Muhammad Imron
      December 13, 2016 at 4:28 am
      Reply

      kalo mau lihat result/lognya kan bisa lihat di database gan

  2. Aziz hardaya
    January 19, 2016 at 7:50 am
    Reply

    mantaaaaaaaaaaaaaapppp gaaaaaaaann sangat membantu

  3. doni el rezen
    February 17, 2016 at 8:45 am
    Reply

    bg kok pesannya error gini katanya ya

    Fatal error: Call to undefined function helper_log() in /var/www/html/CIPOS/application/controllers/Category.php on line 30

    • Muhammad Imron
      Muhammad Imron
      February 17, 2016 at 1:13 pm
      Reply

      itu belum keload helper_log(); nya gan
      jangan lupa load dulu, file autoload.php di folder config
      $autoload[‘helper’] = array(‘url’,’form’,’log’);

  4. slamet raharjo
    March 3, 2016 at 8:28 am
    Reply

    Yang nulisnya kurang ganteng dan yang pasti jomblo sepertinya

    • Muhammad Imron
      Muhammad Imron
      March 3, 2016 at 11:59 am
      Reply

      Koq tu gan 😀

  5. handi
    December 13, 2016 at 4:18 am
    Reply

    gan mau tanya, kalo buat nampilin hasil resultnya ke view gimana ya? tq

    • Muhammad Imron
      Muhammad Imron
      December 13, 2016 at 4:31 am
      Reply

      kalo mau lihat result atau hasil lognya kan bisa lihat di database gan

  6. IT-jurnal
    December 22, 2016 at 8:50 am
    Reply

    keren banggg , thanks banget

  7. Gilang Januar
    January 24, 2017 at 2:44 am
    Reply

    Hasilnya kaya gimana gan?

  8. danny
    February 6, 2017 at 6:01 am
    Reply

    kalau hasilnya disimpan kedalam log.txt (file) gimana gan?

    • Muhammad Imron
      Muhammad Imron
      February 6, 2017 at 7:48 am
      Reply

      tinngal di save ke txt aja gan, jangan ke database … kira2 seperti ini gan ..
      $this->load->helper(‘file’);
      $data = ‘Some file data’;
      if ( ! write_file(‘./path/to/file.php’, $data))
      {
      echo ‘Unable to write the file’;
      }
      else
      {
      echo ‘File written!’;
      }

      • mochammad danny
        February 6, 2017 at 9:03 am
        Reply

        maaf, ditaruh dimana gan? maaf saya newbie

        • mochammad danny
          February 6, 2017 at 10:10 am
          Reply

          gan, maaf udah bisa kok. tapi kok write nya nggak bisa ngulang ya. jadi saya akses 3 halaman yang ditulis itu klikan halaman terakhir. itu gimana solusinya? script saya gini :

          // paramter
          $param_user = $CI->session->userdata(‘username’);
          $param_tipe = $log_tipe;
          $param_desc = $str;
          $param_date = date(‘Y-m-d H:i:s’);

          //load model log
          //$CI->load->model(‘log_activity’);
          //$CI->log_activity->save_log($param);
          $data = ‘tanggal :’ .$param_date. ‘, username :’.$param_user. ‘, tipe akses :’ .$param_tipe. ‘, aktifitas :’ .$param_desc;
          if ( ! write_file(‘log.txt’, $data))
          {
          echo ‘Unable to write the file’;
          }

  9. mochammad danny
    February 6, 2017 at 9:13 am
    Reply

    ane kena ini gan : Unable to load the requested file: helpers/log_helper.php

  10. SANTI
    March 16, 2017 at 9:22 pm
    Reply

    MAF MAU NANYA GAN .. KLO MAU NAMBAH IN DATA ID TABEL YANG DILAKUKAN PERUBAHAN KE TABEL LOG NYA GIMANA CARANYA YA GAN ?

  11. Adie
    November 22, 2017 at 7:57 am
    Reply

    Gan gak bisa muncul perintah gini

    An uncaught Exception was encountered

    Type: RuntimeException

    Message: Unable to locate the model you have specified: M_log

    Filename: D:\xampp\htdocs\kintacrud\system\core\Loader.php

    Line Number: 344

    fix nya gimana ya?

  12. Septian Adi Putra
    May 11, 2018 at 9:15 am
    Reply

    sip gan, sukses

  13. Mafatikhul Habibi
    May 29, 2018 at 10:02 am
    Reply

    Jika update biar ada data lama dan data baru gimana gan???

  14. yudo
    June 15, 2018 at 4:06 pm
    Reply

    Kalo ngga bisa pake bisa php gmn yah ?

  15. Arya Bayu
    July 4, 2018 at 6:53 am
    Reply

    Gan boleh lihat isi tabel_log nya
    field2 nya

  16. Dewi maliana
    August 2, 2018 at 4:28 am
    Reply

    Terimkasih untuk informasinya sangat membantu , sukses selalu

  17. danilrafiqi
    September 23, 2018 at 2:55 pm
    Reply

    thankyou gan, saya kira buat logging itu mainnya di database, ternyata lewat aplikasi bisa juga

    • Lia
      January 20, 2020 at 3:03 am
      Reply

      klo lewat aplikasi bagaimana caranya?

  18. Agung muliawan
    January 28, 2019 at 4:52 am
    Reply

    gan gue gak bisa ngambil session username. selalu waktu di run, values field null (tidak masuk database) yang lain bisa semua gan selain username

  19. Iwit
    February 21, 2019 at 4:01 pm
    Reply

    Gan kalo bikin add/edit/delete kebaca id record nya gmn ya?jd tau record mana yg dieksekusi

  20. awan
    June 13, 2019 at 12:33 am
    Reply

    Gan, kalo mau simpan log data sebelum di edit gimana caranya, biar kita tau perubahan yang dilakukan apa

  21. Jual Lakban kertas Indonesia
    November 3, 2019 at 3:20 pm
    Reply

    Thanks for every other informative site. Where else may I get that kind of info written in such an ideal method?
    I’ve a mission that I am just now working on, and I have been at the look
    out for such info.

  22. besi stainless
    November 4, 2019 at 5:22 am
    Reply

    It’s great that you are getting thoughts from this paragraph as well
    as from our dialogue made here.

  23. sulis
    November 7, 2019 at 3:36 am
    Reply

    gAN, KOK log username nya ga masuk ya, null nih db knapa ya?

  24. TAKDIR ALISYAHBANA
    January 2, 2020 at 4:41 am
    Reply

    bang bisa dikirim souce kode nya gak ke email saya… terima kasih

  25. jaenudin
    February 12, 2020 at 5:01 am
    Reply

    tks gan.. sangat bermanfaat

  26. jaenudin
    February 12, 2020 at 5:05 am
    Reply

    alhamdulillah… sangat bermanfaat

  27. decya
    September 16, 2020 at 8:15 am
    Reply

    CARA GET USERNAMENYA GIMANA YA ?

Leave a reply